#!/usr/bin/env ruby # # Prints out the schedules for NFL team from espn.com to an output # dir. See this file for exact options. # # Examples: # # % ./nflschedules # Writes the schedules to 'out' # % ./nflschedules -d schedules # Writes the schedules to 'schedules' # % ./nflschedules -v # Tells you what the hell we're doing # % ./nflschedules -i # Download the icons only # require 'open-uri' require 'ics' # -------------------------------------------------- # Types # -------------------------------------------------- class NflSchedules attr_writer :verbose, :outdir, :icons def initialize @verbose = true @outdir = nil @icons = false end def start stations2events, teams2events, televised2events = collect_events return if @icons Dir.mkdir @outdir if !File.exists?(@outdir) log 'Wrote stations to ' + output_events('stations' , stations2events) log 'Wrote teams to ' + output_events('teams' , teams2events) log 'Wrote televised games to ' + output_events('televised' , teams2events) end private # Outputs all the events for each id in a subdir of 'outdir' def output_events(subdir,ids2events) dir = File.join @outdir, subdir Dir.mkdir dir if !File.exists?(dir) ids2events.each do |id,events| outfile = File.join dir, canonical_name(id) + ".ics" File.open outfile, "w" do |out| out.print ICS.header(id.to_s + ' Schedule: ' + Time.now.year.to_s) events.map { |e| out.print e.to_ics } out.print ICS.footer end end return dir end # Returns a canonical name for 'team' def canonical_name(team) team.gsub /\W/, '_' end # Returns a tuple of hashes # 1. stations2events # 2. teams2events # 3. televised2events # From crawling the ESPN page def collect_events # Collect the events into two maps # 1. stations to events # 2. teams to events # 3. televised games to events stations2events = {} teams2events = {} televised2events = {} date = nil inside = false team_home = nil team_away = nil month = nil day_of_month = nil day = nil hours = nil minutes = nil station = nil open('http://sports.espn.go.com/nfl/schedule').read.each "\n" do |line| # Find the abbreviations # option value="http://sports.espn.go.com/nfl/teams/schedule?team=ari&year=2008">Arizona if @icons line.scan /option value="http:[^"]+teams\/schedule\?team=(\w+)[^"]*">(.*)/ do |res| abbrev,team = res team = team.strip url = 'http://assets.espn.go.com/i/teamlogos/nfl/med/trans/' + abbrev + '.gif' file = File.basename url log 'Downloading ' + url system 'download -v ' + url newfile = File.join @outdir, canonical_name(team) + '.gif' log 'Moving ' + file + ' -> ' + newfile system 'mv ' + file + ' ' + newfile end next end # THU, SEP 4 line.scan /]*>(\w+), (\w+) (\d+)<\/td>/ do |res| day,month,day_of_month = res end # if /<\/tr>/ =~ line inside = false end if inside # Chicago at Atlanta line.scan /([^<]+)<\/a> at ([^<]+)<\/a><\/td>/ do |res| team_away,team_home = res end # Georgia Dome if station line.scan /([^<]+)<\/td>/ do |res| location = res[0] e = ICS::Event.new team_away + ' at ' + team_home e.location = location e.start_time = Time.local Time.now.year, month, day_of_month, hours, minutes e.end_time = Time.local Time.now.year, month, day_of_month, hours+3, minutes if station e.add_to_summary ' (' + station + ')' end # Save this for later teams2events = add_to_list team_home, e, teams2events teams2events = add_to_list team_away, e, teams2events if station stations2events = add_to_list station , e, stations2events televised2events = add_to_list team_home, e, televised2events televised2events = add_to_list team_away, e, televised2events end # Reset everything team_home = nil team_away = nil hours = nil minutes = nil station = nil end end # FOX if hours line.scan /([^<]+)<\/td>/ do |res| station = res[0] end end # 1:00 PM line.scan /(\d+):(\d+) ([AP]M)<\/td>/ do |res| hours,minutes,am_pm = res hours = hours.to_i minutes = minutes.to_i if am_pm == 'PM' if hours != 12 hours += 12 end else if hours == 0 hours = 12 end end end end # if // =~ line inside = true end end return [stations2events, teams2events, televised2events] end def log(msg) STDERR.puts(msg) if @verbose end # Returns a new list with def add_to_list(key,val,hash) lst = hash[key] lst = [] if !lst lst.push val hash[key] = lst return hash end end # -------------------------------------------------- # Main # -------------------------------------------------- def print_help def e(*rest) STDERR.puts rest end e 'Usage ' + File.basename($0) + ' *' e 'where options include' e ' -h || --help print this message' e ' -v || --verbose be loud' e ' -i || --icons download the icons only' e ' -d || --outdir put results in , default to \'out\'' end def main(argv) args = Array.new argv outdir = 'out' s = NflSchedules.new while !args.empty? arg = args.shift if '-h' == arg || /-help/ =~ arg print_help return end if '-v' == arg || /-verbose/ =~ arg s.verbose = true next end if '-d' == arg || /-outdir/ =~ arg outdir = args.shift next end if '-i' == arg || /-icons/ =~ arg s.icons = true next end end s.outdir = outdir s.start end main ARGV