-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtennisExplorer.py
504 lines (441 loc) · 20.1 KB
/
tennisExplorer.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# -*- coding: utf-8 -*-
import time
import requests
from bs4 import BeautifulSoup
from utils import *
from datetime import date, datetime, timedelta
BASE_URL = "https://www.tennisexplorer.com/"
COMPETITIONS_TO_SKIP = (
"A Day at the Drive (Adelaide)", # 29/01/2021
"Abu Dhabi - exh.", # 16/12/2021
"Adria Tour", # 03/07/2020
"Adria Tour 2", # 13/06/2020
"Adria Tour 3", # 20/06/2020
"Adria Tour 4", # 27/06/2020
"Adriatic Tennis Series", # 08/06/2020
"African Games",
"Altec Styslinger Tennis Exhibition", # 29/06/2020
"AllAmerican Team Cup", # 03/07/2020
"Asian Games", # 24/09/2023
"Atlanta Open", # 24/08/2020
"Austrian Bundesliga", # 22/05/2022
"Austrian Championship", # 27/06/2021
"Austrian Pro Series", # 27/05/2020
"Austrian Young Guns", # 07/07/2020
"Baltic Tennis League", # 02/06/2020
"Basel Open", # 18/07/2020
"Battle of Bradenton", # 11/06/2020
"Battle of the Brits", # 23/06/2020
"Battle of the Brits 2", # 27/07/2020
"Battle of the Brits 3", # 20/12/2020
"Bett", # 17/07/2020
"bett1 Aces", # 13/07/2020
"Boodles Tennis Challenge", # 25/06/2019
"Bratislava Open", # 17/06/2020
"British Tour", # 03/07/2020
"British Tour 2", # 06/07/2020
"British Tour 3", # 09/07/2020
"British Tour 4", # 18/07/2020
"British Tour 5", # 23/07/2020
"British Tour 6", # 21/08/2020
"British Tour 7", # 28/08/2020
"British Tour 8", # 04/09/2020
"British Tour 9", # 02/10/2020
"British Tour 10", # 02/10/2020
"British Tour 11", # 18/12/2020
"Budapest Kupa Exhibition Series", # 22/05/2020
"Bundesliga - men", # 03/07/2022
"Bundesliga - women", # 08/05/2022
"Campionati Italiani Assoluti", # 22/06/2020
"Challenge Elite FFT", # 06/07/2020
"Challenge Elite FFT 2", # 13/07/2020
"Challenge Elite FFT 3", # 20/07/2020
"Copa del Rey", # 21/08/2020
"Cup of Friendship", # 04/07/2020
"CZE TA President's Cup", # 26/05/2020
"Czech league", # 12/12/2021
"Delray Beach - seniors", # 06/01/2021
"Diriyah Tennis Cup", # 12/12/2019
"DTB German Pro Series", # 09/06/2020
"East Coast Pro Series", # 01/06/2020
"Eastern European Championship", # 15/06/2020
"European Pro Tennis Series", # 08/06/2020
"Finnish Tennis Tour", # 21/08/2020
"FPT Portugal Series", # 24/06/2020
"FPT Portugal Series 2", # 01/07/2020
"FPT Portugal Series 3", # 08/07/2020
"FPT Portugal Series 4", # 15/07/2020
"France - Championship", # 20/11/2022
"Friendship Cup", # 18/05/2020
"German Championships", # 06/12/2022
"GP de la Gruyere", # 21/07/2020
"Grand Slam Tennis Tours MatchPlay", # 26/05/2020
"Grand Slam Tennis Tours MatchPlay 2020",
"Hawaii Open",
"HSC Cup Piestany", # 23/07/2020
"Hurlingham - exhibition", # 21/06/2022
"International Premier League", # 27/07/2020
"International Tennis Series", # 18/04/2020
"Israel National Tennis Tour", # 23/06/2020
"Italian Championship", # 20/06/2020
"Italian National Tour", # 29/06/2020
"Kooyong - exh.", # 14/01/2020
"Laver Cup", # 23/09/2022
"Liga MAPFRE de Tenis", # 10/07/2020
"Liga MAPFRE de Tenis 2", # 15/07/2020
"Liga MAPFRE de Tenis 3", # 17/07/2020
"Liga MAPFRE de Tenis 4", # 22/07/2020
"Liga MAPFRE de Tenis 5", # 29/07/2020
"Liverpool - exhibition", # 16/06/2022
"London - seniors", # 25/11/2021
"Macau Tennis Masters", # 24/12/2024
"Macha Lake Open", # 24/06/2020
"Marbello Exhibition Series", # 06/05/2020
"Merko Cup", # 29/07/2020
"Moravia Open", # 15/07/2020
"National Championship", # 11/07/2021
"National Tennis Tour Switzerland", # 01/07/2020
"Netherlands - Championship", # 13/12/2022
"New Zealand Premier League", # 03/06/2020
"ÖTV Challenge Series", # 06/07/2020
"ÖTV Challenge Series 2", # 13/07/2020
"ÖTV Challenge Series 3", # 20/07/2020
"ÖTV Challenge Series 4", # 27/07/2020
"Pacific Games", # 22/11/2023
"Pan American Games", # 24/10/2023
"Peugeot Tennis Tour", # 25/08/2020
"Polish national championship", # 10/07/2022
"Polish National Tour", # 17/06/2020
"Polish National Tour 2", # 06/07/2020
"Polish National Tour 3", # 24/07/2020
"Polish National Tour 4", # 29/07/2020
"Polish National Tour 5", # 15/08/2020
"Polish National Tour 6", # 20/08/2020
"Privatbanka Open", # 09/07/2020
"Riyadh - exhibition", # 10/2024
"Securitas Pro Cup", # 24/07/2020
"SK Soccer Cup", # 23/05/2020
"Svijany Open", # 29/07/2020
"Swedish Summer Tour", # 18/06/2020
"Swiss Masters", # 06/07/2020
"Swiss Nationalliga A", # 07/05/2022
"Tennis Point Exhibition Series", # 01/05/2020
"Tennis Point Exhibition Series 2", # 07/05/2020
"Tennis Point Exhibition Series 3", # 10/05/2020
"Tennis Point Exhibition Series 4", # 14/05/2020
"Tennis Point Exhibition Series 5", # 20/05/2020
"Tennis Point Exhibition Series 6", # 27/05/2020
"Tennis Point Exhibition Series 7", # 03/06/2020
"Tennis Point Exhibition Series 8", # 10/06/2020
"Tennis Point Exhibition Series 9", # 07/07/2020
"Tennis Point Exhibition Series 10", # 14/07/2020
"Tennis Point Exhibition Series 11", # 21/07/2020
"Tennis Point Series (USA)", # 14/05/2020
"Tennis Point Series 2 (USA)", # 22/05/2020
"Tennisportalen Open", # 26/06/2020
"Thiem's Seven", # 07/07/2020
"Tie-Break Cup Zermatt", # 09/07/2020
"Tipsport Elite Trophy", # 08/08/2020
"Top League CTS", # 11/06/2020
"TPG Exhibition Series", # 08/06/2020
"UK Pro Series", # 16/01/2022
"UK Pro Series 2", # 20/07/2020
"UK Pro Series 3", # 27/07/2020
"UK Pro Series 4", # 03/08/2020
"UK Pro Series 5", # 10/08/2020
"Ultimate Tennis Showdown", # 14/06/2020
"Ultimate Tennis Showdown 2", # 25/07/2020
"Ultimate Tennis Showdown 3", # 16/10/2020
"Ultimate Tennis Showdown 2 2020",
"UTF Invitational", # 09/06/2020
"UTR Leschly Challenge", # 31/05/2020
"UTR Pro Match Series", # 01/01/2022
"UTR Pro Tennis Series", # 09/05/2022
"UTR Pro Tennis Series 2", # 17/01/2022
"UTR Pro Tennis Series 3", # 13/12/2020
"UTR Pro Tennis Series 4", # 25/01/2021
"UTR Pro Tennis Series 5",
"UTR Pro Tennis Series 6", # 03/01/2022
"UTR Pro Tennis Series 7", # 28/03/2022
"UTR Pro Tennis Series 8", # 04/07/2022
"UTR Pro Tennis Series 9",
"UTS Championship", # 24/05/2021
"Valencia challenge", # 05/06/2020
"Verbier Open", # 26/09/2020
"Waikiki Cup", # 18/12/2022
"West Coast Pro Series", # 01/06/2020
"World TeamTennis", # 14/11/2021
"World Tennis League", # 19/12/2022
"Zilina" # 30/07/2020
)
SEX_KEYWORDS = {'M': 'atp-men', 'W': 'wta-women'}
SURFACE_ABBREVIATIONS = {
"Clay": "T",
"Grass": "H",
"Hard": "D",
"Indoors": "I"
}
def getLastGamesByPlayer(playerID, numGames = 8):
year = date.today().year
print("# Player => {}".format(player['tennisExplorerKeyword']))
lastGames = []
while len(lastGames) < numGames and year > 2018:
url = BASE_URL + "player/{}?annual={}".format(player['tennisExplorerKeyword'], year)
print(url)
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")
table = soup.select("div[id=matches-" + str(year) + "-1-data]")
if len(table) > 0:
schedule = table[0].select("tr")
noMatches = schedule[0].select("td.first.tl")
if len(noMatches) == 0:
for game in schedule:
if "head" in game['class']:
# Competition
competitionLinks = game.select("a")
if len(competitionLinks) > 0:
competition = game.select("a")[0].text.strip()
else:
competition = game.select("span")[0].text.strip()
validCompetition = competition not in invalidCompetitions
else:
# Game
if validCompetition:
if competition not in shownCompetitions:
try:
print("\t-> {}".format(competition))
except Exception as e:
print(competition)
shownCompetitions.append(competition)
previousGame = {}
time = game.select("td")[0].text.split(".")
previousGame['time'] = "{}-{}-{}".format(year, time[1], time[0])
player1 = game.select("td")[2].select("a")[0]['href'].split("/")[-2]
player2 = game.select("td")[2].select("a")[1]['href'].split("/")[-2]
numPlayer = player1 == player['tennisExplorerKeyword'] and 1 or 2
previousGame['opponent'] = numPlayer == 1 and player2 or player1
score = game.select("td")[4].select("a")[0].text.split(", ")
if score[0] != "":
set1 = score[0].split("-")
playerGames = numPlayer == 1 and int(set1[0]) or int(set1[1])
opponentGames = numPlayer == 1 and int(set1[1]) or int(set1[0])
if playerGames > 10:
numGames = playerGames // 10
playerGames = numGames
elif opponentGames > 10:
numGames = opponentGames // 10
opponentGames = numGames
if playerGames - opponentGames >= 2:
previousGame['breakDone'] = 1
previousGame['breakReceived'] = -1
elif opponentGames - playerGames >= 2:
previousGame['breakDone'] = -1
previousGame['breakReceived'] = 1
elif playerGames == 0:
previousGame['breakDone'] = 0
elif opponentGames == 0:
previousGame['breakReceived'] = 0
else:
previousGame['breakDone'] = -1
previousGame['breakReceived'] = -1
lastGames.append(previousGame)
if len(lastGames) == numGames:
break
year -= 1
return lastGames
def getDailyGames(day = date.today().strftime("%Y-%m-%d"), sex = "men"):
gamesDay = datetime.strptime(day, "%Y-%m-%d")
if sex == "men":
url = BASE_URL + "next/?type=atp-single&year={}&month={}&day={}".format(gamesDay.year, '{:02d}'.format(gamesDay.month), '{:02d}'.format(gamesDay.day))
else:
url = BASE_URL + "next/?type=wta-single&year={}&month={}&day={}".format(gamesDay.year, '{:02d}'.format(gamesDay.month), '{:02d}'.format(gamesDay.day))
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")
tables = soup.select("table[class=result]")
dottedDate = "{}. {}. {}".format('{:02d}'.format(gamesDay.day), '{:02d}'.format(gamesDay.month), gamesDay.year)
played = False
tournament = None
tournaments = []
games = []
for table in tables:
liSet = table.parent.select("li[class=set]")
if len(liSet) > 0 and liSet[0].text == dottedDate:
rows = table.select("tr")
break
for row in rows:
rowClasses = row['class']
if "head" in rowClasses:
# Competition
tournament = None
links = row.select("td[class=t-name] a")
if len(links) > 0:
tournamentName = row.select("td[class=t-name] a")[0].text.strip()
if tournamentName not in COMPETITIONS_TO_SKIP:
if tournamentName not in tournaments:
tournaments.append(tournamentName)
href = row.select("td[class=t-name] a")[0]['href'].split("/")
tournament = href[1]
else:
tournament = "itf"
else:
id = row['id']
if id[-1] != "b":
# Home player
thirdColumn = row.select("td")[2]
if "result" in thirdColumn['class']:
# Game played
played = True
else:
# Game not played
played = False
if tournament is not None:
game1 = {
'tournament': tournament,
'time': row.select("td")[0].text[0:5]
}
game2 = game1.copy()
game1['player1'] = row.select("td")[1].select("a")[0]['href'].split("/")[2]
game2['player2'] = row.select("td")[1].select("a")[0]['href'].split("/")[2]
else:
# Away player
if not played and tournament is not None:
game1['player2'] = row.select("td")[0].select("a")[0]['href'].split("/")[2]
game2['player1'] = row.select("td")[0].select("a")[0]['href'].split("/")[2]
#if game1['tournament'] != "itf" and "itf" not in game1['tournament']:
games.append(game1)
games.append(game2)
return games
def getTournaments(sex, year):
tournaments = []
url = BASE_URL + "calendar/{}/{}/".format(SEX_KEYWORDS[sex], year)
soup = BeautifulSoup(requests.get(url).text, "lxml")
rows = soup.select("table[id=tournamentList] tbody tr")
for row in rows:
if not "month" in row['class']:
tournament = {}
rowHead = row.select("th")
if len(rowHead) > 0:
tournamentElement = rowHead[0].select("a")[0]
if row.select("td[class^=tr]")[0].text != "-":
tournamentPrize = int(row.select("td[class=tr]")[0].text.split(" ")[0].replace(",", ""))
else:
tournamentPrize = 0
tournament['_id'] = tournamentElement['href'].split("/")[1]
tournament['sex'] = sex
tournament['year'] = year
tournament['name'] = tournamentElement.text.strip()
if tournament['name'] not in COMPETITIONS_TO_SKIP:
print(tournamentElement.text.strip())
tagColor = row.select("td[class=s-color]")[0]
tagColorHasChild = len(tagColor.find_all()) != 0
print(tagColorHasChild)
if tagColorHasChild:
tournament['surface'] = SURFACE_ABBREVIATIONS[row.select("td[class=s-color] span")[0]['title']]
else:
tournament['surface'] = None
if " chall" in tournament['name']:
tournament['category'] = "CH"
elif " ITF" in tournament['name']:
tournament['category'] = "ITF"
elif sex == "M":
tournament['category'] = "ATP"
if tournamentPrize > 12000000:
tournament['subcategory'] = "GS"
elif tournamentPrize > 5000000:
tournament['subcategory'] = "1000"
elif tournamentPrize > 1300000:
tournament['subcategory'] = "500"
else:
tournament['subcategory'] = "250"
else:
tournament['category'] = "WTA"
if tournamentPrize > 12000000:
tournament['subcategory'] = "GS"
elif tournamentPrize > 5000000:
tournament['subcategory'] = "1000"
elif tournamentPrize > 1300000:
tournament['subcategory'] = "500"
else:
tournament['subcategory'] = "250"
urlTournament = BASE_URL + "{}/{}/{}/".format(tournament['_id'], year, SEX_KEYWORDS[sex])
for attempt in range(3):
try:
soup = BeautifulSoup(requests.get(urlTournament).text, "lxml")
break
except:
time.sleep(1)
tournament['country'] = getKeywordFromString(soup.select("h1")[0].text.split(" (")[1].split(")")[0])
tournaments.append(tournament)
return tournaments
def getPlayedDailyGames(day = date.today().strftime("%Y-%m-%d"), sex = "men"):
gamesDay = datetime.strptime(day, "%Y-%m-%d")
if sex == "men":
url = BASE_URL + "next/?type=atp-single&year={}&month={}&day={}".format(gamesDay.year, '{:02d}'.format(gamesDay.month), '{:02d}'.format(gamesDay.day))
else:
url = BASE_URL + "next/?type=wta-single&year={}&month={}&day={}".format(gamesDay.year, '{:02d}'.format(gamesDay.month), '{:02d}'.format(gamesDay.day))
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, "lxml")
tables = soup.select("table[class=result]")
dottedDate = "{}. {}. {}".format('{:02d}'.format(gamesDay.day), '{:02d}'.format(gamesDay.month), gamesDay.year)
tournament = None
tournaments = []
games = []
for table in tables:
liSet = table.parent.select("li[class=set]")
if len(liSet) > 0 and liSet[0].text == dottedDate:
rows = table.select("tr")
break
for row in rows:
rowClasses = row['class']
if "head" in rowClasses:
# Competition
tournament = None
links = row.select("td[class=t-name] a")
if len(links) > 0:
tournamentName = row.select("td[class=t-name] a")[0].text.strip()
if tournamentName not in COMPETITIONS_TO_SKIP:
if tournamentName not in tournaments:
tournaments.append(tournamentName)
href = row.select("td[class=t-name] a")[0]['href'].split("/")
tournament = href[1]
else:
tournament = "itf"
else:
id = row['id']
if id[-1] != "b":
# Home player
thirdColumn = row.select("td")[2]
if tournament is not None:
game1 = {
'tournament': tournament,
'time': row.select("td")[0].text[0:5]
}
game2 = game1.copy()
game1['player1'] = row.select("td")[1].select("a")[0]['href'].split("/")[2]
game2['player2'] = row.select("td")[1].select("a")[0]['href'].split("/")[2]
else:
# Away player
if tournament is not None:
game1['player2'] = row.select("td")[0].select("a")[0]['href'].split("/")[2]
game2['player1'] = row.select("td")[0].select("a")[0]['href'].split("/")[2]
#if game1['tournament'] != "itf" and "itf" not in game1['tournament']:
games.append(game1)
games.append(game2)
return games
def getLastDaysPlayers(sex = 'men', numDays = 2):
players = []
numDay = 1
while numDay <= (numDays + 1):
day = (datetime.now() - timedelta(days=(numDay - 1))).strftime('%Y-%m-%d')
print('Getting players from {}...'.format(day))
dailyGames = getPlayedDailyGames(day, sex)
for game in dailyGames:
if game['player1'] not in players:
players.append(game['player1'])
if game['player2'] not in players:
players.append(game['player2'])
numDay += 1
return players