-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchProcessor.py
342 lines (313 loc) · 13.8 KB
/
matchProcessor.py
1
2
3
4
5
6
7
8
9
10
11
12
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
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#----------------------------------------------------------------------------------------------------------------
# matchProcessor.py
# - Processes match pages and gives info
# - Currently gives...
# - Match status
# - Live
# - Upcoming time
# - Final
# - Event
# - Match in the Event
# - Team names
# - Team image links
# - Picks and Bans (When Available)
# - Date
# - Time
# - Patch (When Available)
# - Streams and vods (When Available)
# - Head-To-Head stats (When Available)
# - Scores (When Available)
# - Stats of all players from all played maps and the combined stats of all maps (When Available)
# - Economy stats of all rounds of all maps (When Available)
#
# 2/2/2024
# - Created
# - Took related code from vlr.py and moved it here
# - Added support for all known types of matches
# - Upcoming
# - Completed
# - Live
# - Live matches where all maps are determined
# - Live matches where next maps are TBD: Seen in Challengers League 2024 North America: Qualifiers
# - Live matches where the score is updated live: Needs more testing
# - Added Head-To-Head stats
# - Added Team Image Links
# - Added Vods and Streams
# - Need to find better way to write lines 202-253
# 2/5/2024
# - Added Economy stats
# - Still need to find better way to write lines 232-283
#----------------------------------------------------------------------------------------------------------------
import requests
import json
import re
from bs4 import BeautifulSoup, Comment
def getMatchPage(matchID): #returns a dictionary of the stats of match given by id
matchStats = {}
url = "https://www.vlr.gg/" + str(matchID)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
mainHeader = soup.find('div', class_ = "match-header-vs")
matchStatus = mainHeader.find('div', class_ = 'match-header-vs-note').getText().strip()
images = mainHeader.findAll('img', src = True)
teamAImageLink = images[0]['src']
teamBImageLink = images[1]['src']
if('vlr.png' in teamAImageLink):
teamAImageLink = "https://www.vlr.gg/img/vlr/tmp/vlr.png"
else:
teamAImageLink = "https:" + teamAImageLink
if('vlr.png' in teamBImageLink):
teamBImageLink = "https://www.vlr.gg/img/vlr/tmp/vlr.png"
else:
teamBImageLink = "https:" + teamBImageLink
teamAName = mainHeader.find('div', class_ = "match-header-link-name mod-1").findAll('div')[0].getText().strip()
teamBName = mainHeader.find('div', class_ = "match-header-link-name mod-2").findAll('div')[0].getText().strip()
if(('final' in matchStatus) or ('live' in matchStatus)):
teamAScore = int(mainHeader.find('div', class_ = "js-spoiler").findAll('span')[0].getText().strip())
teamBScore = int(mainHeader.find('div', class_ = "js-spoiler").findAll('span')[2].getText().strip())
if(matchStatus != 'live'):
winner = ""
if(teamAScore > teamBScore):
winner = teamAName
loser = teamBName
else:
winner = teamBName
loser = teamAName
else:
winner = 'LiveMatch'
loser = 'LiveMatch'
superHeader = soup.find('div', class_ = "match-header-super")
date = superHeader.findAll('div', class_ = 'moment-tz-convert')[0].getText().strip()
time = superHeader.findAll('div', class_ = 'moment-tz-convert')[1].getText().strip()
mainEvent = superHeader.find('a').find('div').findAll('div')[0].getText().strip()
eventSubtext = superHeader.find('a').find('div').findAll('div')[1].getText().strip()
eventSubtext = eventSubtext.replace("\t", "")
eventSubtext = eventSubtext.replace("\n", "")
toDelete = soup.findAll('div', class_ = 'moment-tz-convert')
toDelete[0].decompose()
toDelete[1].decompose()
patch = soup.find('div', class_ = 'match-header-date').getText().strip()
mapSection = soup.find('div', class_ = "match-header-note")
if(mapSection != None):
mapText = mapSection.getText().strip()
picksAndBans = mapText.split(";")
for i in range(0, len(picksAndBans)):
picksAndBans[i] = picksAndBans[i].strip()
else:
picksAndBans = ["None"]
videos = {}
#Completed Match
if(('final' in matchStatus)):
if('Not yet available') not in soup.find('div', class_ = 'match-vods').getText().strip():
vods = {}
vodList = soup.find('div', class_ = 'match-vods').find('div', class_ = 'match-streams-container').findAll('a', href = True)
for vod in vodList:
mapNum = vod.getText().strip()
vodLink = vod['href']
vods[mapNum] = vodLink
videos['Vods'] = vods
else:
vods = 'Not Available Yet'
videos['Vods'] = vods
#Live or Upcoming match
else:
if('No stream available') not in soup.find('div', class_ = 'match-streams').getText().strip():
streams = {}
streamList = soup.find('div', class_ = 'match-streams').find('div', class_ = 'match-streams-container').findChildren(recursive = False)
for stream in streamList:
streamName = stream.getText().strip()
if('More Streams' not in streamName):
try:
streamLink = stream.find('a', href = True)['href']
except TypeError:
streamLink = stream['href']
streams[streamName] = streamLink
videos['Streams'] = streams
else:
streams = 'No Stream Available'
videos['Streams'] = streams
if((soup.find('div', class_ = 'match-h2h-matches') != None)):
headToHeadMatches = soup.find('div', class_ = 'match-h2h-matches').findAll('a', href = True)
i = 0
headToHead = {}
for match in headToHeadMatches:
link = match['href']
event = match.find('div', class_ = 'match-h2h-matches-event-name text-of').getText().strip()
matchInEvent = match.find('div', class_ = 'match-h2h-matches-event-series text-of').getText().strip()
encounterDate = match.find('div', class_ = 'match-h2h-matches-date').getText().strip()
scoreSection = match.find('div', class_ = 'match-h2h-matches-score')
scores = scoreSection.getText().strip().split("\n")
team1Score = scores[0]
team2Score = scores[1]
headToHead[i] = {
"link" : link,
"event" : event,
"matchInEvent" : matchInEvent,
"encounterDate" : encounterDate,
"team1Score" : team1Score,
"team2Score" : team2Score
}
i += 1
else:
headToHead = {
"None" : "None"
}
matchStats["Status"] = matchStatus
matchStats["Event"] = mainEvent
matchStats["eventDetail"] = eventSubtext
matchStats["Teams"] = teamAName + " vs " + teamBName
matchStats["TeamA"] = teamAName
matchStats["TeamB"] = teamBName
matchStats['TeamAImage'] = teamAImageLink
matchStats['TeamBImage'] = teamBImageLink
matchStats["PicksAndBans"] = picksAndBans
matchStats["Date"] = date
matchStats["Time"] = time
matchStats["Patch"] = patch
matchStats["Videos"] = videos
matchStats["HeadToHead"] = headToHead
if(('final' in matchStatus) or ('live' in matchStatus)):
statsContainer = soup.find('div', class_ = "vm-stats-container").findAll('div', class_ = "vm-stats-game")
mapDetails = getPlayerDataMatch(statsContainer, teamAName, teamBName)
matchStats["scoreA"] = teamAScore
matchStats["scoreB"] = teamBScore
matchStats["Winner"] = winner
matchStats["Loser"] = loser
matchStats["Maps"] = mapDetails
if('final' in matchStatus):
matchStats["Economy"] = getEconomyStats(matchID)
return matchStats
def getPlayerDataMatch(statsContainer, teamAName, teamBName): #to help with getMatchStats
matchStats = {}
tbdCounter = 0
for game in statsContainer:
mapDetails = {}
if(game['data-game-id'] != 'all'):
mapName = game.find('div', class_ = "map").getText().strip().split()[0]
if(mapName != 'TBD'):
mapDetails["rndsA"] = game.findAll('div', class_ = "score")[0].getText().strip().split()[0]
mapDetails["rndsB"] = game.findAll('div', class_ = "score")[1].getText().strip().split()[0]
if((mapDetails["rndsA"] == "0") and (mapDetails["rndsB"] == "0")):
matchStats[mapName] = 'None'
continue
mapDetails["TrndsA"] = game.findAll('span', class_ = "mod-t")[0].getText().strip()
mapDetails["TrndsB"] = game.findAll('span', class_ = "mod-t")[1].getText().strip()
mapDetails["CTrndsA"] = game.findAll('span', class_ = "mod-ct")[0].getText().strip()
mapDetails["CTrndsB"] = game.findAll('span', class_ = "mod-ct")[1].getText().strip()
else:
matchStats["TBD" + str(tbdCounter)] = 'None'
tbdCounter += 1
continue
else:
mapName = 'all'
playerTables = game.findAll('tbody')
mapDetails[teamAName] = loadPlayerData(playerTables[0].findAll('tr'))
mapDetails[teamBName] = loadPlayerData(playerTables[1].findAll('tr'))
matchStats[mapName] = mapDetails
return matchStats
def loadPlayerData(players): #to help with getPlayerDataMatch
playerStats = {}
for player in players:
playerDic = {}
info = player.findAll('td')
name = player.find('div', class_ = "text-of").getText().strip().split()[0]
try:
playerDic["agent"] = player.find('img')["alt"]
except TypeError:
playerDic['agent'] = '0'
try:
playerDic["rating"] = info[2].getText().strip().split()[0]
except IndexError:
playerDic["rating"] = '0'
try:
playerDic["acs"] = info[3].getText().strip().split()[0]
except IndexError:
playerDic["acs"] = '0'
try:
playerDic["kills"] = info[4].getText().strip().split()[0]
except IndexError:
playerDic["kills"] = '0'
try:
playerDic["deaths"] = info[5].find('span', class_ = "side mod-both").getText().strip().split()[0]
except IndexError:
playerDic["deaths"] = '0'
try:
playerDic["assists"] = info[6].getText().strip().split()[0]
except IndexError:
playerDic["assists"] = '0'
try:
playerDic["kdDiff"] = info[7].getText().strip().split()[0]
except IndexError:
playerDic["kdDiff"] = '0'
try:
playerDic["kast"] = info[8].getText().strip().split()[0]
except IndexError:
playerDic["kast"] = '0'
try:
playerDic["adr"] = info[9].getText().strip().split()[0]
except IndexError:
playerDic["adr"] = '0'
try:
playerDic["hsPercent"] = info[10].getText().strip().split()[0]
except IndexError:
playerDic["hsPercent"] = '0'
try:
playerDic["firstKills"] = info[11].getText().strip().split()[0]
except IndexError:
playerDic["firstKills"] = '0'
try:
playerDic["firstDeaths"] = info[12].getText().strip().split()[0]
except IndexError:
playerDic["firstDeaths"] = '0'
try:
playerDic["fkDiff"] = info[13].getText().strip().split()[0]
except IndexError:
playerDic["fkDiff"] = '0'
playerStats[name] = playerDic
return playerStats
def getEconomyStats(matchID):
matchEconStats = {}
url = "https://www.vlr.gg/" + str(matchID) + "/?game=all&tab=economy"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
soup.find('div', class_ = 'vm-stats-game mod-active').decompose()
allStats = soup.find('div', class_ = 'vm-stats-container').findAll('div', class_ = 'vm-stats-game')
allStats = [mapStat for mapStat in allStats if 'not available yet' not in mapStat.getText()]
i = 1
for mapStat in allStats:
mapEconStats = {}
econStats = mapStat.findAll('table', class_ = 'wf-table-inset mod-econ')[1].findAll('td')
j = 1
for stat in econStats:
if('BANK' not in stat.getText()):
comments = stat.find_all(string = isComment)
team1Bank = stat.findAll('div', class_ = 'bank')[0].getText().strip()
team2Bank = stat.findAll('div', class_ = 'bank')[1].getText().strip()
team1Loadout = getValue(comments[0])
team2Loadout = getValue(comments[1])
winBox = stat.findAll('div', class_ = 'rnd-sq')
if('mod-win' in str(winBox[0])):
team1Win = True
else:
team1Win = False
mapEconStats[j] = {
"Team1Bank" : team1Bank,
"Team2Bank" : team2Bank,
"Team1Loadout" : team1Loadout,
"Team2Loadout" : team2Loadout,
"Team1Win" : team1Win
}
j += 1
else:
continue
matchEconStats["Map" + str(i)] = mapEconStats
i += 1
return matchEconStats
def isComment(input):
return isinstance(input, Comment)
def getValue(input):
match = re.search(r'(\d+\.\d+)', input)
if match:
return str(match.group(1))
else:
return None