-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtournament_traversal.py
More file actions
183 lines (137 loc) · 4.64 KB
/
Copy pathtournament_traversal.py
File metadata and controls
183 lines (137 loc) · 4.64 KB
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
from __future__ import annotations
from typing import TypedDict
from atptour import *
class Tournaments(TypedDict):
tournaments: list[Tournament]
class Tournament(TypedDict):
name: str
location: str
date: str
year: int
matches: list[Match]
class Match(TypedDict):
arena: str
duration: str
link: str
notes: str
parsed: list[str]
from typing import Literal
CHEVRON_TYPE = Literal["chevron ", "icon-chevron-"]
DIRECTION = Literal["down", "up"]
def toggle(chevron: CHEVRON_TYPE = "chevron ", direction: DIRECTION = "down"):
xpath = rf"//span[@class='{chevron}{direction}']/."
toggled = State.driver.find_elements(By.XPATH, xpath)
while toggled:
scroll_to(toggled[0], align_to_bottom=False)
toggled[0].click()
toggled = State.driver.find_elements(By.XPATH, xpath)
toggle()
def save_tournaments():
url = "https://www.atptour.com/en/tournaments"
State.driver.get(url)
tournaments = []
for el in State.driver.find_elements(By.CLASS_NAME, "non-live-cta"):
link = el.find_element(By.XPATH, "./*").get_attribute("href")
if link:
tournaments.append(link)
with open("temp/tournaments.txt", "w") as file:
file.write("\n".join(tournaments))
def is_apt_tournament():
logo_name = (
State.driver.find_element(By.XPATH, r"//div[@class='badge']/*")
.get_attribute("src")
.split("/")[-1]
.split(".")[0]
)
if logo_name.isnumeric():
return True
return False
def parse_tournament(link: str) -> None | Tournament:
safe_get(link)
accept_cookies()
tournament_name, info = State.driver.find_element(
By.CLASS_NAME, "schedule"
).text.split("\n")
location, date = map(str.strip, info.split("|"))
date, year = date.split(",")
year = int(year)
tournament: Tournament = {
"name": tournament_name,
"date": date,
"location": location,
"year": year,
"matches": [],
}
if not is_apt_tournament():
print(tournament_name, "is not apt tournament")
return None
# expand all
chevrons = State.driver.find_elements(By.XPATH, r"//div[@class='atp_accordion-item-toggler']"
r"/span[@class='icon-chevron-down']")
for chevron in chevrons[1:]:
try:
scroll_to(chevron, align_to_bottom=False)
chevron.click()
except (ElementClickInterceptedException, ElementNotInteractableException):
pass
for match in State.driver.find_elements(By.CLASS_NAME, "match"):
try:
link = match.find_element(
By.XPATH,
r"./div[@class='match-footer']/div[@class='match-cta']/a[text()='Stats']",
).get_attribute("href")
except NoSuchElementException:
# if no link
continue
try:
values = match.find_element(
By.XPATH, r"./div[@class='match-header']"
).text
arena, duration = values.split("\n")
except NoSuchElementException:
arena = duration = ''
except ValueError:
arena = values[0]
duration = 0
try:
date = match.find_element(
By.XPATH,
r"./ancestor::div[contains(@class, 'atp_accordion-item')]/div[@class='atp_accordion-header']",
).text
except NoSuchElementException:
date = ''
try:
notes = match.find_element(By.XPATH, r"./div[@class='match-notes']").text
except NoSuchElementException:
notes = ''
tournament["matches"].append(
{
"arena": arena,
"duration": duration,
"link": link,
"notes": notes,
"parsed" : []
}
)
return tournament
@save_as_json
def traverse():
result: Tournaments = {"tournaments": []}
with open("temp/tournaments.txt", "r") as file:
tournaments = file.readlines()
for link in tournaments:
print(link)
try:
tournament = parse_tournament(link)
if tournament:
result["tournaments"].append(tournament)
print("OK")
except Exception as e:
print(f"Error while parsing tournament\n{link=}")
print(e)
with open("temp/traverse_temp.json", "w") as f:
json.dump(result, f, indent=4)
return result
if __name__ == '__main__':
# save_tournaments()
traverse()