Class: NbaBoxScore
- Inherits:
-
Object
- Object
- NbaBoxScore
- Includes:
- NbaUrls
- Defined in:
- lib/espnscrape/NbaBoxScore.rb
Overview
Access NBA boxscore data
Instance Method Summary collapse
-
#getAwayTeamName ⇒ String
Return away team name.
-
#getAwayTeamPlayers ⇒ [Integer]
Returns array of away team player stats.
-
#getAwayTeamTotals ⇒ [Integer]
Returns array of away team combined stats.
-
#getGameDate ⇒ String
Return game date.
-
#getHomeTeamName ⇒ String
Returns home team name.
-
#getHomeTeamPlayers ⇒ [[Integer]]
Returns array of home team player stats.
-
#getHomeTeamTotals ⇒ [Integer]
Returns array of home team combined stats.
-
#initialize(game_id = 0) ⇒ NbaBoxScore
constructor
Scrape Box Score Data.
-
#readGameDate(d) ⇒ String
Reads the game date from a Nokogiri::Doc.
-
#readTeamNames(d) ⇒ String
Reads the team names from a Nokogiri::Doc.
-
#readTeamStats(d, id) ⇒ String
Reads the team stats from a Nokogiri::Doc.
Methods included from NbaUrls
#boxScoreUrl, #formatTeamUrl, #getTid, #playerUrl, #teamListUrl, #teamRosterUrl, #teamScheduleUrl
Constructor Details
#initialize(game_id = 0) ⇒ NbaBoxScore
Scrape Box Score Data
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 11 def initialize(game_id=0) unless (game_id == 0) url = boxScoreUrl + game_id.to_s doc = Nokogiri::HTML(open(url)) if doc.nil? puts "URL Unreachable: " + url exit 1 end @game_date = readGameDate(doc) @away_team, @home_team = readTeamNames(doc) if(getGameDate.index("00:00:00")) # Only past games have stats @away_players, @away_totals = readTeamStats(doc, 'away') @home_players, @home_totals = readTeamStats(doc, 'home') end end end |
Instance Method Details
#getAwayTeamName ⇒ String
Return away team name
37 38 39 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 37 def getAwayTeamName() return @away_team end |
#getAwayTeamPlayers ⇒ [Integer]
Returns array of away team player stats
43 44 45 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 43 def getAwayTeamPlayers() return @away_players end |
#getAwayTeamTotals ⇒ [Integer]
Returns array of away team combined stats
49 50 51 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 49 def getAwayTeamTotals() return @away_totals end |
#getGameDate ⇒ String
Return game date
31 32 33 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 31 def getGameDate() return @game_date end |
#getHomeTeamName ⇒ String
Returns home team name
61 62 63 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 61 def getHomeTeamName() return @home_team end |
#getHomeTeamPlayers ⇒ [[Integer]]
Returns array of home team player stats
67 68 69 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 67 def getHomeTeamPlayers() return @home_players end |
#getHomeTeamTotals ⇒ [Integer]
Returns array of home team combined stats
55 56 57 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 55 def getHomeTeamTotals() return @home_totals end |
#readGameDate(d) ⇒ String
Times will be Local to the system Timezone
Reads the game date from a Nokogiri::Doc
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 80 def readGameDate(d) # Should be YYYY-MM-DD HH:MM:00 time = d.xpath('//span[contains(@class,"game-time")]')[0].text.strip date = d.title.split('-')[2].gsub(',', "") # Future game time is populated via AJAX, so may not be available at the # time the HTML is processed if(time.empty?) utc_datetime = d.xpath('//span[contains(@class,"game-time")]/parent::span').first.attribute("data-date").text utc_datetime = DateTime.parse(utc_datetime).new_offset(DateTime.now.offset) return utc_datetime.strftime("%Y-%m-%d %H:%M:%S") end # Past games have no displayed time if(time == 'Final') time = "00:00:00" end return DateTime.parse(date + " " + time).strftime("%Y-%m-%d %H:%M:%S") end |
#readTeamNames(d) ⇒ String
Reads the team names from a Nokogiri::Doc
108 109 110 111 112 113 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 108 def readTeamNames(d) names = d.xpath('//div[@class="team-info"]/a/span[@class="long-name" or @class="short-name"]') away = names[0].text + " " + names[1].text home = names[2].text + " " + names[3].text return [away, home] end |
#readTeamStats(d, id) ⇒ String
Reads the team stats from a Nokogiri::Doc
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 194 |
# File 'lib/espnscrape/NbaBoxScore.rb', line 123 def readTeamStats(d, id) #Extract player tables p_tables = d.xpath('//div[@class="sub-module"]/*/table/tbody') if(p_tables.nil? or p_tables.empty?) puts "No Game Data Available" return [],[] end if id == 'away' p_tab = p_tables[0,2] tid = getTid(@away_team) else p_tab = p_tables[2,4] tid = getTid(@home_team) end player_rows = p_tab.xpath('tr[not(@class)]') # Ignore TEAM rows team_row = p_tab.xpath('tr[@class="highlight"]')[0] # Ignore TEAM rows player_stats = [] # Array of processed data row_count = 0 # Starter Tracker player_rows.each do |row| # Process Rows tmp_array = Array.new # Array of data being processed tmp_array << tid # team_id tmp_array << '0' # game_id row.children.each do |cell| # Process Columns c_val = cell.text.strip case cell.attribute("class").text when "name" tmp_array << cell.children[0].attribute("href").text[/id\/(\d+)/, 1] # Player ID tmp_array << cell.children[0].text.strip # Player Short Name (i.e. D. Wade) tmp_array << cell.children[1].text.strip # Position when 'fg', '3pt', 'ft' # Made, Attempts cell.text.split('-').each do |sp| tmp_array << sp.strip.gsub("\'","\\\'") end else tmp_array << c_val end end if(row_count < 5) # Check if Starter tmp_array << "X" end player_stats << tmp_array # Save processed data row_count += 1 # Starter Tracker end # Extract TEAM stats team_totals = Array.new team_row.children.each do |cell| c_val = cell.text.strip case cell.attribute("class").text when 'fg', '3pt', 'ft' # Made, Attempts cell.text.split('-').each do |sp| team_totals << sp.strip.gsub("\'","\\\'") end else if(c_val.empty?) c_val = 0 end team_totals << c_val end end return player_stats, team_totals end |