Created
January 27, 2010 10:03
-
-
Save sstephenson/287704 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pipe Ars Technica's live Apple event coverage into a Campfire room. | |
# http://arstechnica.com/apple/news/2010/01/tablet-live-event-liveblog.ars | |
# | |
# Usage: | |
# $ gem install -r hpricot tinder | |
# $ ruby -rubygems ars_live.rb -ssl mysubdomain myroomid myapikey | |
require "hpricot" | |
require "open-uri" | |
require "tinder" | |
require "time" | |
module ArsLive | |
class Feed | |
FEED_URL = "http://rss.coveritlive.com/rss.php?altcast_code=%s" | |
def initialize(id) | |
@url = FEED_URL % id | |
@items = [] | |
end | |
def new_items | |
document = Hpricot(open(@url).read) | |
modified = Time.parse(document.search("channel > lastBuildDate").inner_text) | |
if @modified != modified | |
@modified = modified | |
new_items = items_from(document.search("item")) - @items | |
@items += new_items | |
new_items | |
else | |
[] | |
end | |
end | |
def items_from(elements) | |
elements.map { |element| Item.new(element) }.sort_by { |item| item.date }.reverse | |
end | |
end | |
class Item | |
attr_reader :date | |
def initialize(element) | |
@date = Time.parse(element.search("pubdate").inner_text) | |
@enclosure = element.search("enclosure") | |
@description = Hpricot(element.search("description").inner_text) | |
end | |
def has_enclosure? | |
@enclosure.any? | |
end | |
def image | |
@enclosure.attr("url") | |
end | |
def text | |
cells = @description.search("td") | |
cells.last.inner_text.strip if cells.any? | |
end | |
def body | |
@body ||= has_enclosure? ? image : text | |
end | |
def hash | |
@hash ||= [date, body].join.hash | |
end | |
def eql?(other) | |
hash == other.hash | |
end | |
end | |
end | |
if __FILE__ == $0 | |
ssl = ARGV.first == "-ssl" && ARGV.shift | |
if ARGV.length < 3 | |
$stderr.puts "usage: #$0 [-ssl] subdomain roomid apikey" | |
exit 1 | |
end | |
subdomain, room_id, api_key = ARGV[0, 3] | |
campfire = Tinder::Campfire.new(subdomain, :ssl => ssl) | |
campfire.login(api_key, nil) | |
room = Tinder::Room.new(campfire, "id" => room_id) | |
feed = ArsLive::Feed.new("b0eff04045") | |
feed.new_items # don't print existing items, in case you have to restart the script | |
loop do | |
sleep 3 | |
begin | |
feed.new_items.first(10).each do |item| | |
room.speak(item.body) | |
end | |
rescue Errno::ETIMEDOUT, Timeout::Error | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment