Module: Fetch

Defined in:
lib/pitch_fx_scraper/fetch.rb

Constant Summary collapse

@@host_url_base =
"http://gd2.mlb.com"

Class Method Summary collapse

Class Method Details

.get_game_home_teams(date, league = :mlb) ⇒ Object

Return a list of home games for date



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object

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



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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