Guest User

Untitled

a guest
Mar 2nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. # sanity.rb, the ruby script that keeps your twitter timeline manageable.
  4. #
  5. # This script is meant to be run every few hours, and will show you
  6. # tweets from the people you tell it to. That guy that retweets every 10 minutes? You can
  7. # leave him off your list.
  8. #
  9. # Create a file in yaml format that looks like this:
  10. # auth:
  11. # username: robrasmussen
  12. # password: sekret
  13. #
  14. # icon_path: /Users/robert/sanity_icons
  15. #
  16. # friends:
  17. # - maximus_prime
  18. # - bumblebee
  19. # - starscream
  20. #
  21. #
  22. # Then set up a cron to run every so often, for example:
  23. #
  24. # 0 9,11,13,15,17,19,21,23 * * * /Users/robert/sanity.rb /Users/robert/sanity.yml 120
  25. # 0 7 * * * /Users/robert/sanity.rb /Users/robert/sanity.yml 480
  26. #
  27. # This cron setup will update you every 2 hours throughout the day, each time retrieving the tweets
  28. # from the previous two hours, and will give you one ovenight update at 7 in the morning.
  29. #
  30. # The tweets will be growl notifications. If you want to do something else, just change the end of the script.
  31. # If your laptop has been asleep, just run it manually.
  32. #
  33. # IF you use growl, you need to make sure you have it configured to "Listen for Incoming Notifications"
  34. # otherwise it will drop messages that have images. If you don't care about seeing people's images, just
  35. # remove the --image argument near the bottom of this file and no messages will be dropped.
  36. #
  37. #
  38. # Usage: sanity.rb path_to_yaml_config maximum_tweet_age_in_minutes
  39. # Example sanity.rb /Users/robert/sanity.yml 120
  40.  
  41. require 'rubygems'
  42. require 'grackle' # gem install hayesdavis-grackle (you'll need github in your gem sources)
  43. require 'activesupport'
  44. require 'open-uri'
  45. require 'parsedate'
  46.  
  47. if ARGV.size < 2
  48. puts "Usage: sanity.rb path_to_yaml_config maximum_tweet_age_in_minutes"
  49. puts "e.g. sanity.rb /Users/robert/sanity.yml 120"
  50. exit(1)
  51. end
  52.  
  53. sanity_file, max_age = ARGV[0], ARGV[1].to_i
  54. sanity_config = YAML.load_file(sanity_file).symbolize_keys
  55. icon_path = sanity_config[:icon_path]
  56. File.mkdir(icon_path) unless File.exists?(icon_path)
  57.  
  58. client = Grackle::Client.new(sanity_config[:auth].symbolize_keys)
  59. friends = sanity_config[:friends]
  60.  
  61. page = 1
  62. oldest = Time.now - max_age * 60
  63. timeline = []
  64.  
  65. loop do
  66. paged = client.statuses.friends_timeline? :since => oldest, :count => 200, :page => page
  67. timeline += paged.select {|entry| friends.include?(entry.user.screen_name)}
  68. break if paged.size < 200
  69. page += 1
  70. end
  71.  
  72. icon_map = {}
  73. timeline.collect {|entry| entry.user }.uniq.each do |user|
  74. if (image_file = Dir["#{File.join(icon_path, user.screen_name)}.*"]).empty?
  75. image_url = user.profile_image_url
  76. image = open(image_url).read
  77. image_file = File.join(icon_path, "#{user.screen_name}.#{image_url.split(".").last}")
  78. open(image_file, "w") {|o| o.write(image)}
  79. end
  80. icon_map[user.screen_name] = [image_file].flatten.first
  81. end
  82.  
  83. timeline.each do |entry|
  84. `growlnotify --sticky --image #{icon_map[entry.user.screen_name]} --message #{entry.text.inspect} -H localhost #{entry.user.name}`
  85. end
Add Comment
Please, Sign In to add comment