Triple J Twitter

August 7th, 2007

To scratch an itch of mine earlier this week I setup a new twitter account that broadcasts what program is currently on on Triple J

The account is at twitter.com/whatsonjjj

The code looks like this, it’s obviously specific to JJJ but could be easily modified to any other static HTML source. It gets run from cron on my server at one and 31 minutes past the hour.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'rubygems'
require 'hpricot'
require 'open-uri'

now = Time.now
hour = now.hour.to_s.rjust(2,"0")
minute = now.min < 30 ? "00" : "30"
day = now.strftime("%A")
search =  hour + ":" + minute
url = "http://www.abc.net.au/triplej/guide/#{day}.htm"
doc = Hpricot(open(url))
doc.search("strong").each do |ele|
  re = Regexp.new("^" + search+"-")
  if ele.inner_text.match(re)
    time = ele.inner_text
    program = ele.parent.parent.parent.search("a").first.inner_text.gsub("'","&apos;")
    puts [day,time,program].join(" ")
    system("curl --basic --user 'whatsonjjj:xxxx' \
                --data-ascii \"status=#{[day,time,program].join(" ")}\" \
               'http://twitter.com/statuses/update.json'")
  end
end

Simple walk-though:

Grab the time now, figure out if we’re on the hour or the half-hour. Grab today’s guide. Parse it, look for a string tag that starts with HH:[00|30], grab the name of the program, send that over to twitter via their API

There is probably a more elegant way to get the name of the program than that, but that works.

I plan to grabbing out some of the other data too, host name and the like.

1 Response to “Triple J Twitter”

  1. maxm Says:
    Nice work. You can make the regex creation and matching slightly more elegant: if ele.inner_text =~ /^#{search}-/

Sorry, comments are closed for this article.