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_urls(date, milb = false) ⇒ Object

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



26
27
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 26

def Fetch.get_game_urls(date, milb=false)
  if milb == false
    master_scoreboard_url = Fetch.url_for_date(date) + "/master_scoreboard.xml"
  else
    master_scoreboard_url = Fetch.url_for_date(date,milb) + "/milb_master_game_file.xml"
  end

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

  scoreboard.xpath("//game").each do |node|
    if milb == false 
      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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pitch_fx_scraper/fetch.rb', line 55

def Fetch.get_milb_schedule (date)
  game_boxscores = Fetch.get_game_urls(date, true) 

  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, milb = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pitch_fx_scraper/fetch.rb', line 10

def Fetch.url_for_date(date, milb=false)
  date_object = Chronic.parse(date) 
  if milb == false
    url_base = @@host_url_base + "/components/game/mlb/"
  else
    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