Class: NbaSchedule

Inherits:
Object
  • Object
show all
Includes:
NbaUrls
Defined in:
lib/espnscrape/NbaSchedule.rb

Overview

Access NBA team schedule data

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NbaUrls

#boxScoreUrl, #formatTeamUrl, #getTid, #teamListUrl, #teamRosterUrl, #teamScheduleUrl

Constructor Details

#initialize(tid = '', file = '') ⇒ NbaSchedule

Read Schedule data for a given Team

Examples:

t = NbaSchedule.new('', 'test/testData.html')
u = NbaSchedule.new('UTA')

Parameters:

  • tid (String) (defaults to: '')

    Three letter TeamID

  • file (String) (defaults to: '')

    file name for static data



13
14
15
16
17
18
19
20
21
22
23
24
25
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
52
53
54
55
56
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/espnscrape/NbaSchedule.rb', line 13

def initialize(tid='', file='')
	doc = ''
	if file.empty? # Load Live Data
		unless (tid.empty?)
			url = formatTeamUrl(tid, teamScheduleUrl)
			doc = Nokogiri::HTML(open(url))
		end
	else # Load File Data
		doc = Nokogiri::HTML(open(file))
	end

	schedule = doc.xpath('//div/div/table/tr') #Schedule Rows
	year1 = doc.xpath('//div[@id=\'my-teams-table\']/div/div/div/h1').text.split('-')[1].strip.to_i #Season Starting Year

	year2 = year1 + 1 # Season Ending Year
	game_id = 0 # 82-game counter
	gameDate = ""
	game_time = ""

	@game_list = []
	@next_game = 0 # Cursor to start of Future Games

	# Process Schedule lines
	schedule.each do |x|
		if ('a'..'z').include?x.children[0].text[1,1]
			tmp = [] 				# Table of Schedule rows
			tmp << tid			# TeamID
			game_id += 1
			tmp << game_id   # GameID

			cnt = 0
			if (x.children.size < 7)
				# => Future Game
				tmp << false 								#Win
				tmp << 0							  #boxscore_id
				x.children.each do |cell|
					if cnt < 4
						txt = cell.text.chomp
						if cnt == 0 						# Game Date
							gameDate = txt.split(',')[1].strip
							tmp << gameDate
						elsif cnt == 1 				 # Home/Away, Opp tid
							if txt[0,1].include?('@') # Home?
								tmp << false
							else
								tmp << true
							end
							x1 = cell.children.children.children[1].attributes['href'].text.split('/')[-1].split('-').join(' ')
							tmp << getTid(x1) 	# OppID
						elsif cnt == 2 				# Game Time
							game_time = txt + ' ET'
							tmp << game_time
						elsif cnt == 3 				# TV
							if cell.children[0].node_name == 'img' || txt != " "
								tmp << true
							else
								tmp << false
							end
						end
					end
					cnt += 1
				end
			else
				# => Past Game
				game_time = '00:00:00' 		# Game Time (Not shown for past games)
				tmp << game_time
				tmp << false 						  # TV (NS)
				x.children.each do |cell|
					if cnt <= 3
						txt = cell.text.chomp
						if cnt == 0 				 # Game Date
							gameDate = txt.split(',')[1].strip
							tmp << gameDate
						elsif cnt == 1
							if txt[0,1].include?('@') # Home?
								tmp << false
							else
								tmp << true
							end
							x1 = cell.children.children.children[1].attributes['href'].text.split('/')[-1].split('-').join(' ')
							tmp << getTid(x1) 				# OppID
						elsif cnt == 2 						  # Game Result
							win =  (txt[0,1].include?('W') ? true : false)
							tmp << win 								# Win?
							txt2 = txt[1, txt.length].gsub(' OT','')
							if !win
								opp_score, team_score = txt2.split('-')
							else
								team_score, opp_score = txt2.split('-')
							end
							tmp << team_score << opp_score #Team Score, Opp Score

							bs_id = cell.children.children.children[1].attributes['href'].text.split('=')[1]
							tmp << bs_id 									# Boxscore ID
						elsif cnt == 3 								  #Team Record
							wins, losses = txt.split('-')
							tmp << wins << losses
						end
					end
					cnt += 1
					@next_game = game_id
				end
			end
		end
		if !tmp.nil?
			# => Adjust and format dates
			if ['Oct', 'Nov', 'Dec'].include?(gameDate.split[0])
				d = DateTime.parse(game_time + ' , ' + gameDate + "," + year1.to_s)
			else
				d = DateTime.parse(game_time + ' , ' + gameDate + "," + year2.to_s)
			end
			# puts "GameDate = %s" % [d.strftime("%Y-%m-%d %H:%M:%S")]
			gameDate = d.strftime("%Y-%m-%d %H:%M:%S")
			tmp << gameDate # Game DateTime

			# => Skip Postponed Games
			if game_time == 'Postponed'
				game_id -= 1
				next
			else
				game_time = d.strftime('%T')
			end

			# => Store row
			@game_list << tmp
		end
	end
end

Instance Attribute Details

#game_listObject (readonly)

Returns the value of attribute game_list.



5
6
7
# File 'lib/espnscrape/NbaSchedule.rb', line 5

def game_list
  @game_list
end

#next_gameObject (readonly)

Returns the value of attribute next_game.



5
6
7
# File 'lib/espnscrape/NbaSchedule.rb', line 5

def next_game
  @next_game
end

Instance Method Details

#getAllGames[object]

Note:

Future Games Layout: TeamID, GameID, Win?, boxscore_id, Game Date, Home?, OppID, Game Time, TV?, game_datetime

Note:

Past Games Layout: TeamID, GameID, Game Time, TV?, Game Date, Home?, OppID, Win?, Team Score, Opp Score, boxscore_id, wins, losses, game_datetime

Return Full Schedule

Returns:

  • ([object])


146
147
148
# File 'lib/espnscrape/NbaSchedule.rb', line 146

def getAllGames()
	return @game_list
end

#getFutureGames[object]

Note:

Future Games Layout: TeamID, GameID, Win?, boxscore_id, Game Date, Home?, OppID, Game Time, TV?, game_datetime

Return Schedule info of Future Games

Returns:

  • ([object])


186
187
188
# File 'lib/espnscrape/NbaSchedule.rb', line 186

def getFutureGames()
	return @game_list[@next_game, game_list.size]
end

#getLastGameobject

Note:

Past Games Layout: TeamID, GameID, Game Time, TV?, Game Date, Home?, OppID, Win?, Team Score, Opp Score, boxscore_id, wins, losses, game_datetime

Return Schedule Info of last game

Examples:

getLastGame() #=> ["UTA", 12, "00:00:00", false, "Nov 20", false, "DAL", false, "93", "102", "400828071", "6", "6"]

Returns:

  • (object)


164
165
166
# File 'lib/espnscrape/NbaSchedule.rb', line 164

def getLastGame()
	return @game_list[@next_game-1]
end

#getNextGameobject

Note:

Future Games Layout: TeamID, GameID, Win?, boxscore_id, Game Date, Home?, OppID, Game Time, TV?, game_datetime

Return Schedule Info of next game

Examples:

getNextGame() #=> ["UTA", 13, false, "NULL", "Nov 23", true, "OKC", "9:00PM", false]

Returns:

  • (object)


155
156
157
# File 'lib/espnscrape/NbaSchedule.rb', line 155

def getNextGame()
	return @game_list[@next_game]
end

#getNextGameIdInteger

Return GameID of Next Game

Returns:

  • (Integer)


170
171
172
# File 'lib/espnscrape/NbaSchedule.rb', line 170

def getNextGameId()
	return @next_game
end

#getNextTeamIdString

Return TeamID of next opponent

Examples:

getNextTeamid() #=> "OKC"

Returns:

  • (String)


178
179
180
181
# File 'lib/espnscrape/NbaSchedule.rb', line 178

def getNextTeamId()
	gameIndex = 6
	return getNextGame[gameIndex]
end

#getPastGames[object]

Note:

Past Games Layout: TeamID, GameID, Game Time, TV?, Game Date, Home?, OppID, Win?, Team Score, Opp Score, boxscore_id, wins, losses, game_datetime

Return Schedule info of Past Games

Returns:

  • ([object])


193
194
195
# File 'lib/espnscrape/NbaSchedule.rb', line 193

def getPastGames()
	return @game_list[0, @next_game]
end

#toStringString

Visualization Helper

Returns:

  • (String)


199
200
201
202
203
204
205
# File 'lib/espnscrape/NbaSchedule.rb', line 199

def toString
	str = ''
	@game_list.each do |game|
		str = str + game.join(',')
	end
	return str
end