Module: ESPN::Scores
- Defined in:
- lib/espn_scraper/scores.rb
Class Method Summary collapse
- .data_name_from(link) ⇒ Object
-
.home_away_parse(doc, date = nil) ⇒ Object
parsing strategies.
- .markup_from_date(league, date) ⇒ Object
- .markup_from_date_and_conference(league, date, conference_id) ⇒ Object
-
.markup_from_year_and_week(league, year, week, group = nil) ⇒ Object
Get Markup.
- .ncf_parse(doc) ⇒ Object
-
.parse_data_name_from(container) ⇒ Object
parsing helpers.
- .winner_loser_parse(doc, date) ⇒ Object
Class Method Details
.data_name_from(link) ⇒ Object
258 259 260 261 262 263 264 265 266 |
# File 'lib/espn_scraper/scores.rb', line 258 def data_name_from(link) encoded_link = URI::encode(link.strip) query = URI::parse(encoded_link).query if query CGI::parse(query)['team'].first else link.split('/')[-2] end end |
.home_away_parse(doc, date = nil) ⇒ Object
parsing strategies
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/espn_scraper/scores.rb', line 155 def home_away_parse(doc, date=nil) scores = [] games = [] espn_regex = /window\.espn\.scoreboardData \t= (\{.*?\});/ doc.xpath("//script").each do |script_section| if script_section.content =~ espn_regex espn_data = JSON.parse(espn_regex.match(script_section.content)[1]) games = espn_data['events'] break end end games.each do |game| # Game must be regular or postseason next unless game['season']['type'] == SEASONS[:regular_season] || game['season']['type'] == SEASONS[:postseason] # Game must not be suspended if it was supposed to start on the query date. # This prevents fetching scores for suspended games which are not yet completed. game_start = DateTime.parse(game['date']).to_time.utc + Time.zone_offset('EDT') next if date && game['competitions'][0]['wasSuspended'] && game_start.to_date == date score = {} competition = game['competitions'].first # Score must be final if competition['status']['type']['detail'] =~ /^Final/ competition['competitors'].each do |competitor| if competitor['homeAway'] == 'home' score[:home_team] = competitor['team']['abbreviation'].downcase score[:home_score] = competitor['score'].to_i else score[:away_team] = competitor['team']['abbreviation'].downcase score[:away_score] = competitor['score'].to_i end end score[:game_date] = DateTime.parse(game['date']) scores << score end end scores end |
.markup_from_date(league, date) ⇒ Object
143 144 145 146 |
# File 'lib/espn_scraper/scores.rb', line 143 def markup_from_date(league, date) day = date.to_s.gsub(/[^\d]+/, '') ESPN.get 'scores', league, "scoreboard?date=#{ day }" end |
.markup_from_date_and_conference(league, date, conference_id) ⇒ Object
148 149 150 151 |
# File 'lib/espn_scraper/scores.rb', line 148 def markup_from_date_and_conference(league, date, conference_id) day = date.to_s.gsub(/[^\d]+/, '') ESPN.get league, 'scoreboard', '_', 'group', conference_id.to_s, 'date', day end |
.markup_from_year_and_week(league, year, week, group = nil) ⇒ Object
Get Markup
135 136 137 138 139 140 141 |
# File 'lib/espn_scraper/scores.rb', line 135 def markup_from_year_and_week(league, year, week, group=nil) if group ESPN.get 'scores', league, "scoreboard/_/group/#{group}/year/#{year}/seasontype/2/week/#{week}" else ESPN.get 'scores', league, "scoreboard/_/year/#{year}/seasontype/2/week/#{week}" end end |
.ncf_parse(doc) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/espn_scraper/scores.rb', line 195 def ncf_parse(doc) scores = [] games = [] espn_regex = /window\.espn\.scoreboardData \t= (\{.*?\});/ doc.xpath("//script").each do |script_section| if script_section.content =~ espn_regex espn_data = JSON.parse(espn_regex.match(script_section.content)[1]) games = espn_data['events'] break end end games.each do |game| score = { league: 'college-football' } competition = game['competitions'].first date = DateTime.parse(competition['startDate']) date = date.new_offset('-06:00') score[:game_date] = date.to_date # Score must be final if competition['status']['type']['detail'] =~ /^Final/ competition['competitors'].each do |competitor| if competitor['homeAway'] == 'home' score[:home_team] = competitor['team']['id'].downcase score[:home_score] = competitor['score'].to_i else score[:away_team] = competitor['team']['id'].downcase score[:away_score] = competitor['score'].to_i end end scores << score end end scores end |
.parse_data_name_from(container) ⇒ Object
parsing helpers
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/espn_scraper/scores.rb', line 242 def parse_data_name_from(container) if container.at_css('a') link = container.at_css('a')['href'] self.data_name_from(link) else if container.at_css('div') name = container.at_css('div text()').content elsif container.at_css('span') name = container.at_css('span text()').content else name = container.at_css('text()').content end ESPN::DATA_NAME_EXCEPTIONS[ ESPN.dasherize(name) ] end end |
.winner_loser_parse(doc, date) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/espn_scraper/scores.rb', line 229 def winner_loser_parse(doc, date) doc.css('.mod-scorebox-final').map do |game| game_info = { game_date: date } teams = game.css('td.team-name:not([colspan])').map { |td| parse_data_name_from(td) } game_info[:away_team], game_info[:home_team] = teams scores = game.css('.team-score').map { |td| td.at_css('span').content.to_i } game_info[:away_score], game_info[:home_score] = scores game_info end end |