Saturday, March 14, 2009

Read csl_status twitter feeds in ruby (via RSS)

The Cal Poly computer science lab admins started a twitter page with updates about downed servers, etc. The following ruby script grabs the most recent entries (via RSS), and formats the dates purty.


#!/usr/bin/ruby
require 'rss'
require 'open-uri'

$BOLD = "\e[1m"
$NORMAL = "\e[m"

num = ARGV.length > 0 ? ARGV[0].to_i : 1

# Read cls_status twitter RSS feed
source = "http://twitter.com/statuses/user_timeline/19618428.rss"
content = ""
open(source) do |s| content = s.read end
rss = RSS::Parser.parse(content, false)

(0..(num-1)).each do |i|
diff = Time.now - rss.items[i].date
days = diff.to_i / 86400
hrs = (diff % 86400).to_i / 3600
mins,secs = *((diff % 3600).divmod(60))

print "#{$BOLD}"
print "#{days} days " if (days > 0)
print "#{hrs} hrs " if (hrs > 0)
print "#{mins} mins " if (days == 0 and mins > 0)
print "#{mins} secs " if (mins == 0)
puts "ago"

#puts "#{$BOLD}#{rss.items[i].date}"
puts "#{$NORMAL}#{rss.items[i].title}"
#puts rss.items[i].description
#puts rss.items[i].link
end

No comments: