Skip to content

Instantly share code, notes, and snippets.

@robrasmussen
Created March 26, 2009 20:54
Show Gist options
  • Save robrasmussen/86326 to your computer and use it in GitHub Desktop.
Save robrasmussen/86326 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# sanity.rb, the ruby script that keeps your twitter timeline manageable.
#
# This script is meant to be run every few hours, and will show you
# tweets from the people you tell it to. That guy that retweets every 10 minutes? You can
# leave him off your list.
#
# Create a file in yaml format that looks like this:
# auth:
# username: robrasmussen
# password: sekret
#
# icon_path: /Users/robert/sanity_icons
#
# friends:
# - maximus_prime
# - bumblebee
# - starscream
#
#
# Then set up a cron to run every so often, for example:
#
# 0 9,11,13,15,17,19,21,23 * * * /Users/robert/sanity.rb /Users/robert/sanity.yml 120
# 0 7 * * * /Users/robert/sanity.rb /Users/robert/sanity.yml 480
#
# This cron setup will update you every 2 hours throughout the day, each time retrieving the tweets
# from the previous two hours, and will give you one ovenight update at 7 in the morning.
#
# The tweets will be growl notifications. If you want to do something else, just change the end of the script.
# If your laptop has been asleep, just run it manually.
#
# IF you use growl, you need to make sure you have it configured to "Listen for Incoming Notifications"
# otherwise it will drop messages that have images. If you don't care about seeing people's images, just
# remove the --image argument near the bottom of this file and no messages will be dropped.
#
#
# Usage: sanity.rb path_to_yaml_config maximum_tweet_age_in_minutes
# Example sanity.rb /Users/robert/sanity.yml 120
require 'rubygems'
require 'grackle' # gem install hayesdavis-grackle (you'll need github in your gem sources)
require 'activesupport'
require 'open-uri'
require 'parsedate'
if ARGV.size < 2
puts "Usage: sanity.rb path_to_yaml_config maximum_tweet_age_in_minutes"
puts "e.g. sanity.rb /Users/robert/sanity.yml 120"
exit(1)
end
sanity_file, max_age = ARGV[0], ARGV[1].to_i
sanity_config = YAML.load_file(sanity_file).symbolize_keys
icon_path = sanity_config[:icon_path]
File.mkdir(icon_path) unless File.exists?(icon_path)
client = Grackle::Client.new(sanity_config[:auth].symbolize_keys)
friends = sanity_config[:friends]
page = 1
oldest = Time.now - max_age * 60
timeline = []
loop do
paged = client.statuses.friends_timeline? :since => oldest, :count => 200, :page => page
timeline += paged.select {|entry| friends.include?(entry.user.screen_name)}
break if paged.size < 200
page += 1
end
icon_map = {}
timeline.collect {|entry| entry.user }.uniq.each do |user|
if (image_file = Dir["#{File.join(icon_path, user.screen_name)}.*"]).empty?
image_url = user.profile_image_url
image = open(image_url).read
image_file = File.join(icon_path, "#{user.screen_name}.#{image_url.split(".").last}")
open(image_file, "w") {|o| o.write(image)}
end
icon_map[user.screen_name] = [image_file].flatten.first
end
timeline.each do |entry|
`growlnotify --sticky --image #{icon_map[entry.user.screen_name]} --message #{entry.text.inspect} -H localhost #{entry.user.name}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment