Skip to content

Instantly share code, notes, and snippets.

@mfilej
Created October 2, 2009 14:23
Show Gist options
  • Save mfilej/199786 to your computer and use it in GitHub Desktop.
Save mfilej/199786 to your computer and use it in GitHub Desktop.
fetch next episode info from tvrage.com
Lie to Me episode 15: 2x02 -- Truth or Consequences is airing on 2009-10-05
Mad Men episode 34: 3x08 -- Souvenir is airing on 2009-10-04
The Big Bang Theory episode 43: 3x03 -- The Gothowitz Deviation is airing on 2009-10-05
require 'open-uri'
require 'hpricot'
BASE_URI = 'http://www.tvrage.com/'
NEXT_EPISODE_SELECTOR = '#iconn2 > td:first-child > table > tr + tr > td + td > span'
def show_uri(name)
"#{BASE_URI}#{name}"
end
%w(Lie_to_Me Mad_Men The_Big_Bang_Theory).each do |show|
doc = Hpricot(open(show_uri(show)))
next_episode = doc.at(NEXT_EPISODE_SELECTOR)
episode_name = next_episode.at('a').inner_html
date = next_episode.inner_html.match(/\(.+\)/)[0]
month_name, day, year = date[1..-2].split('/')
month = Date::ABBR_MONTHNAMES.index(month_name)
episode_date = Date.civil(year.to_i, month, day.to_i)
air_date = case episode_date - Date.today
when 0
"today"
when 1
"tomorrow"
else
"on #{episode_date}"
end
puts "#{show.tr('_', ' ')} episode #{episode_name} is airing #{air_date}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment