-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.py
306 lines (236 loc) · 10.4 KB
/
collect.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
# %%#
import os
import warnings
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from selenium import webdriver
from selenium.webdriver.support.select import Select
# %#%#
url_page = "https://ifsc.results.info"
years = [str(year) for year in range(2019, 2023)] # 1990 2024
year = years[0]
events = "Past events"
leagues = "World Cups and World Championships"
disciplines = ["Lead", "Boulder", "Speed", "Combined"]
discipline = disciplines[1]
cups = "All cups"
genders = ['M', 'W']
gender = genders[0]
levels = ["Q", "S", "F"]
level = levels[1]
tabs = {"Q": ['Group A', 'Group B'],
"S": ['Result'],
"F": ['Result']}
tab = tabs["S"][0]
base = ["Unique", "Year", "Discipline", "Competition", "Gender", "Level", "Group", "Name", "Number", "Country"]
scb = ["Top1", "Zone1", "Top2", "Zone2", "Top3", "Zone3", "Top4", "Zone4", "Top5", "Zone5"]
scl = ["Route1", "Route2"]
scs = ["Run1", "Run2", "Run3", "Run4", "Run5"]
columns = base + scb + scl + scs
back = "window.history.go(-1)"
wait = 2
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=UserWarning)
def get_scores(strng):
splt = strng.split("\n")
lens = len(splt)
score = [np.inf, np.inf]
if lens == 3:
score[0] = int(splt[0])
score[1] = int(splt[1])
if lens == 2:
score[1] = int(splt[0])
return score
def flatten_list(lst):
new = []
for lsi in lst:
new += lsi
return new
def go_back():
# driver.execute_script(back)
button = driver.find_elements_by_class_name('back-button')[0]
button.click()
return None
# %%#
exe = r'C:/Users/GMBHP/Repos/ekanban_ste1/Drivers/geckodriver.exe'
service = webdriver.firefox.service.Service(exe)
try:
driver
except Exception:
driver = webdriver.Firefox(service=service)
driver.get(url_page)
# %#%#
# discipline, year, competition, level, gender, group, climber
for discipline in disciplines:
file = "ifsc_%s.xlsx" % discipline
if os.path.isfile(file):
res = pd.read_excel(file)
else:
print("Check directory, no ifsc.xlsx file found!")
inp = input("Hit 'return' to stop and change the path, type 'go' to continue.")
if inp == '':
res = rex
res = pd.DataFrame([], columns=columns)
# %#%#
for year in years:
# year
driver.implicitly_wait(wait)
sels = driver.find_elements_by_class_name('custom-select')
sels[0].click()
select = Select(sels[0])
select.select_by_visible_text(year)
# event
driver.implicitly_wait(1)
sels[1].click()
select = Select(sels[1])
select.select_by_visible_text(events)
# league
driver.implicitly_wait(1)
sels[2].click()
select = Select(sels[2])
select.select_by_visible_text(leagues)
# discipline
driver.implicitly_wait(1)
sels[3].click()
select = Select(sels[3])
select.select_by_visible_text(discipline)
# cups
driver.implicitly_wait(1)
sels[4].click()
select = Select(sels[4])
select.select_by_visible_text(cups)
driver.implicitly_wait(1)
cards = driver.find_element_by_class_name('card-container').find_elements_by_tag_name("a")
if not cards:
print("nothing", year)
continue
for idk, card in enumerate(cards):
cards = driver.find_element_by_class_name('card-container').find_elements_by_tag_name("a")
card = cards[idk]
comp = ' '.join(card.text.split('\n'))
card.find_element_by_tag_name("div").click()
if "CANCELLED" in comp:
print("cancelled", comp)
go_back()
continue
driver.implicitly_wait(1)
disc = driver.find_element_by_xpath("//*[contains(text(), '%s')]" % discipline)
disc.click()
for idl, level in enumerate(levels):
driver.implicitly_wait(1)
elems = driver.find_elements_by_xpath("//*[contains(text(), '%s')]" % level)
elems = [elem for elem in elems if elem.text == level]
if not elems:
print("nothing", year, comp, level)
continue
for idg, gender in enumerate(genders):
elems = driver.find_elements_by_xpath("//*[contains(text(), '%s')]" % level)
elems = [elem for elem in elems if elem.text == level]
try: # IFSC Climbing Worldcup (B) - Sheffield (GBR) 2010 no Women Semi
elem = elems[idg]
except Exception:
print("missing", year, comp, level)
continue
elem.click()
tabz = tabs[level]
for tab in tabz:
driver.implicitly_wait(wait)
groups = driver.find_elements_by_xpath("//*[contains(text(), '%s')]" % tab)
groups = [group for group in groups if group.text == tab]
if not groups or discipline == "Lead":
tab = "Result"
groups = driver.find_elements_by_xpath("//*[contains(text(), '%s')]" % tab)
groups = [group for group in groups if group.text == tab]
try:
group = groups[0]
except Exception:
print("nogroup", uni)
continue
group.click()
unis = [year, discipline, comp, gender, level, tab]
uni = ';'.join(unis)
uniz = [uni] + unis
if uni in res['Unique'].unique():
print("skip", uni)
continue
rei = pd.DataFrame([], columns=columns)
driver.implicitly_wait(1)
# lead!
if discipline == "Lead":
rows = driver.find_elements_by_class_name('r-row')
# row = rows[0]
for row in rows:
rank = row.find_element_by_class_name('rank').text
name = row.find_element_by_class_name('r-name').text
nuco = row.find_element_by_class_name('r-name-sub').text
try:
number, country = nuco.split(" • ")
except Exception:
number, country = "-", nuco
cli = [name, number, country]
print(*unis, *cli)
scores = []
vals = row.find_elements_by_class_name('px-2')
if vals:
for val in vals:
vat = val.text
try:
scr, rte = vat.split("\n")
except Exception:
scr, rte = val, "-"
try:
hld, plc = scr.replace(")", "").split(" (")
except Exception:
hld, plc = scr, "-"
scores.append(hld)
else:
hld = row.find_element_by_class_name('r-score').text
plc, rte = '-', '-'
scores.append(hld)
lens = len(scores)
dct = dict()
for key, val in zip(base[:-3], uniz):
dct[key] = val
for key, val in zip(base[-3:], cli):
dct[key] = val
for key, val in zip(scl[:lens], scores):
dct[key] = val
rei = rei.append(dct, ignore_index=True)
# boulder!
if discipline == "Boulder":
climbers = driver.find_elements_by_class_name('boulder-asc-detail')
climberz = driver.find_elements_by_class_name('r-name')
climberc = driver.find_elements_by_class_name('r-name-sub')
for idc, climber in enumerate(climbers):
name = climberz[idc].text
nuco = climberc[idc].text
try:
number, country = nuco.split(" • ")
except Exception:
number, country = "-", nuco
cli = [name, number, country]
print(*unis, *cli)
driver.implicitly_wait(1)
rnds = climber.find_elements_by_class_name('asc-cell-container')
scores = np.ravel(np.array([get_scores(rnd.text) for rnd in rnds]))
lens = len(scores)
print(scores)
dct = dict()
for key, val in zip(base[:-3], uniz):
dct[key] = val
for key, val in zip(base[-3:], cli):
dct[key] = val
for key, val in zip(scb[:lens], scores):
dct[key] = val
rei = rei.append(dct, ignore_index=True)
res = res.append(rei)
res.to_excel(file, index=False)
# gender (after completing tabs/groups)
go_back()
# card (after completing card/comp)
go_back()
# stop selenium
driver.quit()
# %%#