module Fetch

Public Class Methods

get_game_home_teams(date, league=:mlb) click to toggle source

Return a list of home games for date

# File lib/pitch_fx_scraper/fetch.rb, line 28
def Fetch.get_game_home_teams(date, league=:mlb)
  if league == :mlb
    master_scoreboard_url = Fetch.url_for_date(date,league) + "/master_scoreboard.xml"
  elsif league == :milb_high
    master_scoreboard_url = Fetch.url_for_date(date,league) + "/mobile_epg_min_high.xml"
  elsif league == :milb_low
    master_scoreboard_url = Fetch.url_for_date(date,league) + "/mobile_epg_min_low.xml"
  end

  scoreboard = Nokogiri::XML(open(master_scoreboard_url))
 
  game_teams = []

  scoreboard.xpath("//game").each do |node|
    if (league == :milb_high || league == :milb_low || league == :mlb)
      home_city = node.attr('home_team_city')
      home_team = node.attr('home_team_name')
    end

    game_teams << home_city+" "+home_team
  end

  return game_teams
end
get_game_urls(date, league=:mlb) click to toggle source

Return a list of hashes containing game data urls with game ids for a given date string

# File lib/pitch_fx_scraper/fetch.rb, line 57
def Fetch.get_game_urls(date, league=:mlb)
  if league == :mlb
    master_scoreboard_url = Fetch.url_for_date(date) + "/master_scoreboard.xml"
  else
    master_scoreboard_url = Fetch.url_for_date(date,league) + "/milb_master_game_file.xml"
  end

  scoreboard = Nokogiri::XML(open(master_scoreboard_url))
  
  game_urls = []

  scoreboard.xpath("//game").each do |node|
    if league == :mlb
      game_path = node.attr('game_data_directory')
      game_id = node.attr('gameday')
      game_url = @@host_url_base + game_path + "/game_events.xml"
    else
      game_id = node.attr('id')
      game_url = node.attr('boxscore')
    end

    game_urls << {:game_id => game_id, :game_url => game_url}
  end

  return game_urls
end
get_milb_schedule(date) click to toggle source

Return a list of hashes containing home team data for a given date string

# File lib/pitch_fx_scraper/fetch.rb, line 86
def Fetch.get_milb_schedule (date)
  game_boxscores = Fetch.get_game_urls(date, :milb) 

  games = []

  game_boxscores.each do |score|
    begin 
      boxscore = Nokogiri::XML(open(score[:game_url]))

      boxscore.xpath("//boxscore").each do |node|
        home_sname = node.attr('home_sname')      
        home_fname = node.attr('home_fname')      
        file_date = node.attr('date')

        games << {:home_sname => home_sname, :home_fname => home_fname, :date => file_date}
      end
    rescue OpenURI::HTTPError => e
      # oops!
    end
  end

  return games
end
url_for_date(date, league=:mlb) click to toggle source
# File lib/pitch_fx_scraper/fetch.rb, line 10
def Fetch.url_for_date(date, league=:mlb)
  date_object = Chronic.parse(date) 
  if league == :mlb
    url_base = @@host_url_base + "/components/game/mlb/"
  elsif league == :milb_high
    url_base = @@host_url_base + "/components/game/min/"
  elsif league == :milb_low
    url_base = @@host_url_base + "/components/game/min/"
  elsif league == :milb
    url_base = @@host_url_base + "/components/game/milb/"
  end
  url_base += "year_#{"%04d" % date_object.year}/"
  url_base += "month_#{"%02d" % date_object.month}/"
  url_base += "day_#{"%02d" % date_object.day}"
end