I wanted a way to automatically tweet the new xkcd comic every day. I have no idea why. But I was able to whip this up in a few minutes.

Update:

So, I figured that if I was setting up a cron job to tweet XKCD, I figured I might as well broadcast to everyone when I put up a new blog post. If you find this annoying, message me and I can turn it off. I swear to god, I only blog every once in a blue moon, so it shouldn't be a big deal...


#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'httparty'
require 'atom'

class XKCD
def initialize
@url = 'http://xkcd.com'
@hp = Hpricot(open('http://xkcd.com'))
end
def comic
info = (@hp/"h3").collect{ |inf|
inf.inner_text.split(': ')[1]
}
return info
end
end
class Twitter
include HTTParty
base_uri 'twitter.com'
basic_auth 'mattrose','password'
def initialize(user, pass)
self.class.basic_auth user, pass
end
def post(text)
self.class.post('/statuses/update.json', :query => {:status => text})
end
end

class Folkwolf
def initialize
@feed = Atom::Feed.load_feed(URI.parse("http://blog.folkwolf.net/xml/atom10/feed.xml")).entries
end
def latest
latest = @feed.entries[0]
return latest
end
end

image_url = XKCD.new.comic
feed_url = Folkwolf.new.latest.links[0].to_s
feed_updated = Folkwolf.new.latest.updated
ffile = '/tmp/.folkwolf_latest'
`touch #{ffile}`
if feed_updated != open(ffile).read
open(ffile, 'w') { |f| f.write("#{feed_url}") }
Twitter.post('/statuses/update.json', :query => {:status => "New Blog post: #{feed_url}"})
end
file = "/tmp/.xkcd_latest"
if RUBY_PLATFORM !~ /mswin32/
`touch #{file}`
end
if open(file).read != image_url[0]
if RUBY_PLATFORM !~ /mswin32/
open(file, 'w') { |f| f.write("#{image_url[0]}") }
end
Twitter.post('/statuses/update.json', :query => {:status => "d mattrose #{image_url[1]}"})
end



Links


Powered by Disqus

Published

05 January 2009