-
Notifications
You must be signed in to change notification settings - Fork 0
/
world_athletics_scraper.py
404 lines (319 loc) · 26 KB
/
world_athletics_scraper.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
import pandas as pd
import numpy as np
# import matplotlib.pyplot as plt
# import seaborn as sns
import requests
import re
from datetime import datetime, timedelta
from bs4 import BeautifulSoup
from os import makedirs
from tqdm.auto import tqdm
from glob import glob
# sns.set_theme(style='whitegrid')
class WorldAthleticsScraper:
'''A module to scrape the data tables on the World Athletics All Time and Season Bests lists.'''
def __init__(self, event: str | None = None) -> None:
'''Initialize a WorldAthleticsScraper Object.
Args:
- event (`str` | `None`): The running event of interest. Choose one from the following list or specify the event in a data download method:
* '60m'
* '100m'
* '200m'
* '400m'
* '800m'
* '1500m'
* 'mile'
* '3000m'
* '2mile'
* '5000m'
* '10000m'
* 'halfMarathon'
* 'marathon'
'''
self.event = event
self.data_dir = self.make_data_dir()
self.season_bests_html_dicts = {
'60m': {
'men': 'https://worldathletics.org/records/toplists/sprints/60-metres/all/men/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229683&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/sprints/60-metres/all/women/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229684&ageCategory=senior'
},
'100m': {
'men': 'https://worldathletics.org/records/toplists/sprints/100-metres/all/men/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229630&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/sprints/100-metres/all/women/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229509&ageCategory=senior'
},
'200m': {
'men': 'https://worldathletics.org/records/toplists/sprints/200-metres/all/men/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229605&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/sprints/200-metres/all/women/senior/2001?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229510&ageCategory=senior'
},
'400m': {
'men': 'https://worldathletics.org/records/toplists/sprints/400-metres/all/men/senior/2001?regionType=world&timing=electronic&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229631&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/sprints/400-metres/all/women/senior/2001?regionType=world&timing=electronic&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229511&ageCategory=senior'
},
'800m': {
'men': 'https://worldathletics.org/records/toplists/middlelong/800-metres/all/men/senior/2001?regionType=world&timing=electronic&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229501&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/800-metres/all/women/senior/2001?regionType=world&timing=electronic&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229512&ageCategory=senior'
},
'1500m': {
'men': 'https://worldathletics.org/records/toplists/middlelong/1500-metres/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229502&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/1500-metres/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229513&ageCategory=senior'
},
'mile': {
'men': 'https://worldathletics.org/records/toplists/middlelong/one-mile/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229503&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/one-mile/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229517&ageCategory=senior'
},
'3000m': {
'men': 'https://worldathletics.org/records/toplists/middlelong/3000-metres/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229607&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/3000-metres/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229519&ageCategory=senior'
},
'2mile': {
'men': 'https://worldathletics.org/records/toplists/middlelong/two-miles/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229608&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/two-miles/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229520&ageCategory=senior'
},
'5000m': {
'men': 'https://worldathletics.org/records/toplists/middlelong/5000-metres/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229609&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/5000-metres/all/women/senior/2001?regionType=world&page=9&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229514&ageCategory=senior'
},
'10000m': {
'men': 'https://worldathletics.org/records/toplists/middlelong/10000-metres/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229610&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/middlelong/10000-metres/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229521&ageCategory=senior'
},
'halfMarathon': {
'men': 'https://worldathletics.org/records/toplists/road-running/half-marathon/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229633&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/road-running/half-marathon/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229541&ageCategory=senior'
},
'marathon': {
'men': 'https://worldathletics.org/records/toplists/road-running/marathon/all/men/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229634&ageCategory=senior',
'women': 'https://worldathletics.org/records/toplists/road-running/marathon/all/women/senior/2001?regionType=world&page=1&bestResultsOnly=false&maxResultsByCountry=all&eventId=10229534&ageCategory=senior'
}
}
self.all_time_html_dicts = {
'60m': {
'men': f'https://worldathletics.org/records/all-time-toplists/sprints/60-metres/all/men/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229683&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/sprints/60-metres/all/women/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229684&ageCategory=senior'
},
'100m': {
'men': f'https://worldathletics.org/records/all-time-toplists/sprints/100-metres/all/men/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229630&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/sprints/100-metres/all/women/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229509&ageCategory=senior'
},
'200m': {
'men': f'https://worldathletics.org/records/all-time-toplists/sprints/200-metres/all/men/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229605&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/sprints/200-metres/all/women/senior?regionType=world&timing=electronic&windReading=regular&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229510&ageCategory=senior'
},
'400m': {
'men': f'https://worldathletics.org/records/all-time-toplists/sprints/400-metres/all/men/senior?regionType=world&timing=electronic&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229631&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/sprints/400-metres/all/women/senior?regionType=world&timing=electronic&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229511&ageCategory=senior'
},
'800m': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/800-metres/all/men/senior?regionType=world&timing=electronic&page=1&bestResultsOnly=false&firstDay=1899-12-31&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229501&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/800-metres/all/women/senior?regionType=world&timing=electronic&page=1&bestResultsOnly=false&firstDay=1899-12-31&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229512&ageCategory=senior'
},
'1500m': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/1500-metres/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229502&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/1500-metres/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229513&ageCategory=senior'
},
'mile': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/one-mile/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229503&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/one-mile/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229517&ageCategory=senior'
},
'3000m': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/3000-metres/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229607&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/3000-metres/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229519&ageCategory=senior'
},
'2mile': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/two-miles/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229608&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/two-miles/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229520&ageCategory=senior'
},
'5000m': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/5000-metres/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229609&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/5000-metres/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229514&ageCategory=senior'
},
'10000m': {
'men': f'https://worldathletics.org/records/all-time-toplists/middlelong/10000-metres/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229610&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/middlelong/10000-metres/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229521&ageCategory=senior'
},
'halfMarathon': {
'men': f'https://worldathletics.org/records/all-time-toplists/road-running/half-marathon/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229633&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/road-running/half-marathon/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229541&ageCategory=senior'
},
'marathon': {
'men': f'https://worldathletics.org/records/all-time-toplists/road-running/marathon/all/men/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229634&ageCategory=senior',
'women': f'https://worldathletics.org/records/all-time-toplists/road-running/marathon/all/women/senior?regionType=world&page=1&bestResultsOnly=false&firstDay=1899-12-30&lastDay={datetime.today() - timedelta(days=1):%Y-%m-%d}&maxResultsByCountry=all&eventId=10229534&ageCategory=senior'
}
}
self.valid_events = ['60m', '100m', '200m', '400m',
'800m', '1500m', 'mile', '3000m', '2mile',
'5000m', '10000m', 'halfMarathon', 'marathon']
if self.event not in self.valid_events and self.event is not None:
raise ValueError('Please choose a valid running event that is offered in this module.')
def make_data_dir(self) -> None:
'''Set up a data directory in the overall directory with the format: 'data/[yyyy_mm]', where the year and numeric month match the date of the download event.'''
makedirs(f'data/{datetime.today():%Y_%m}', exist_ok=True)
return f'data/{datetime.today():%Y_%m}'
def download_season_bests_data(self, sex: str, event: str | None = None, export: bool = False) -> pd.DataFrame:
'''Download and Concatenate all Seasons Bests for a season, then concatenates all the seasons' season best performances across all years into a single `pd.DataFrame`. The available years on the World Athletics season bests database are 2001 to the current season.
Args:
- sex (`str`): 'men' or 'women'
- event (`str`): choose one from the following list. The default is the event specified in the initializer.
* '60m'
* '100m'
* '200m'
* '400m'
* '800m'
* '1500m'
* 'mile'
* '3000m'
* '2mile'
* '5000m'
* '10000m'
* 'halfMarathon'
* 'marathon'
- export (`bool`): whether or not to export the download as a .csv file
Returns:
- dfs (`pd.DataFrame`): a DataFrame containing event data for all of the Season Bests for all years in the World Athletics Database
'''
if event is None:
event = self.event
html_path = self.season_bests_html_dicts[event][sex]
dfs = None
for i in np.arange(2001, datetime.today().year + 1):
new_path_1 = html_path.replace('2001', f'{i}')
num_pages = self.find_last_page_num(new_path_1)
if num_pages == 0: continue
for j in np.arange(1, num_pages + 1):
new_path_2 = new_path_1.replace('page=1', f'page={j}')
df = pd.read_html(new_path_2)[0]
if dfs is None:
dfs = df
continue
dfs = pd.concat([dfs, df])
match event:
case '60m' | '100m' | '200m' | '400m':
dfs['Mark_Seconds'] = dfs['Mark']
case 'halfMarathon' | 'marathon':
dfs['Mark_Seconds'] = dfs['Mark'].apply(self.convert_marathons)
case _:
dfs['Mark_Seconds'] = dfs['Mark'].apply(lambda row: float(row.split(':')[0]) * 60 + float(row.split(':')[1].replace('h', '')))
if export:
dfs.to_csv(f'{self.data_dir}/season_bests_{sex}_{event}.csv', index=False)
return dfs.reset_index(drop=True)
def download_all_time_data(self, sex: str, event: str | None = None, export: bool = False) -> pd.DataFrame:
'''Downloads all the pages of the world athletics all-time, all-performances (more than one entry per athlete is possible) list for an event and exports them as a single DataFrame.
Args:
- sex (`str`): 'men' or 'women'
- event (`str`): choose one from the following list. The default is the event specified in the initializer.
* '60m'
* '100m'
* '200m'
* '400m'
* '800m'
* '1500m'
* 'mile'
* '3000m'
* '2mile'
* '5000m'
* '10000m'
* 'halfMarathon'
* 'marathon'
- export (`bool`): If `True`, export the data to a .csv file in the data directory. Default is `False`.
Returns:
- dfs (`pd.DataFrame`): a single table of all the concatenated pages of the World Athletics Database'''
if event is None:
event = self.event
dfs = None
html_path = self.all_time_html_dicts[event][sex]
num_pages = self.find_last_page_num(html_path=html_path)
for i in np.arange(1, num_pages + 1):
df = pd.read_html(html_path.replace('page=1', f'page={i}'))[0]
if dfs is None:
dfs = df
continue
dfs = pd.concat([dfs, df])
match event:
case '60m' | '100m' | '200m' | '400m':
dfs['Mark_Seconds'] = dfs['Mark']
case 'halfMarathon' | 'marathon':
dfs['Mark_Seconds'] = dfs['Mark'].apply(self.convert_marathons)
case _:
dfs['Mark_Seconds'] = dfs['Mark'].apply(lambda row: float(row.split(':')[0]) * 60 + float(row.split(':')[1].replace('h', '')))
if export:
dfs.to_csv(f'{self.data_dir}/all_time_{sex}_{event}.csv', index=False)
return dfs.reset_index(drop=True)
def download_season_bests_data_all_events(self) -> None:
'''Download all season bests, all-performances datasets for all the events covered by this module.'''
for event in tqdm(self.valid_events):
for sex in ('men', 'women'):
self.download_season_bests_data(sex=sex, event=event, export='True')
return None
def download_all_time_data_all_events(self) -> None:
'''Download all all-time, all-performances datasets for all the events covered by this module.'''
for event in tqdm(self.valid_events):
for sex in ('men', 'women'):
self.download_all_time_data(sex=sex, event=event, export='True')
return None
def compile_season_bests_tables(self) -> None:
'''Compile all season bests datasets in the data directory into two files: one for all men's records, and one for all women's records'''
# Men's Table
file_list_men = glob('season_bests_men*.csv', root_dir=self.data_dir)
if 'season_bests_men_all_events.csv' in file_list_men: raise Exception('A compiled season bests file has been found in the current directory.')
# TODO: make input flow control to automatically delete a previously compiled all events file?
dfs = None
for file in file_list_men:
df = pd.read_csv(f'{self.data_dir}/{file}')
if dfs is None:
dfs = df.assign(event=file.split('_')[-1].split('.')[0])
continue
dfs = pd.concat([dfs, df.assign(event=file.split('_')[-1].split('.')[0])])
dfs.to_csv(f'{self.data_dir}/season_bests_men_all_events.csv', index=False)
# Women's Table
file_list_women = glob('season_bests_women*.csv', root_dir=self.data_dir)
if 'season_bests_women_all_events.csv' in file_list_women: raise Warning('A compiled season bests file has been found in the current directory.')
dfs = None
for file in file_list_women:
df = pd.read_csv(f'{self.data_dir}/{file}')
if dfs is None:
dfs = df.assign(event=file.split('_')[-1].split('.')[0])
continue
dfs = pd.concat([dfs, df.assign(event=file.split('_')[-1].split('.')[0])])
dfs.to_csv(f'{self.data_dir}/season_bests_women_all_events.csv', index=False)
return None
def compile_all_time_tables(self) -> None:
'''Compile all all-time datasets in the data directory into two files: one for all men's records, and one for all women's records'''
# Men's Table
file_list_men = glob('all_time_men*.csv', root_dir=self.data_dir)
if 'all_time_men_all_events.csv' in file_list_men: raise Exception('A compiled all events file has been found in the current directory.')
# TODO: make input flow control to automatically delete a previously compiled all events file?
dfs = None
for file in file_list_men:
df = pd.read_csv(f'{self.data_dir}/{file}')
if dfs is None:
dfs = df.assign(event=file.split('_')[-1].split('.')[0])
continue
dfs = pd.concat([dfs, df.assign(event=file.split('_')[-1].split('.')[0])])
dfs.to_csv(f'{self.data_dir}/all_time_men_all_events.csv', index=False)
# Women's Table
file_list_women = glob('all_time_women*.csv', root_dir=self.data_dir)
if 'all_time_women_all_events.csv' in file_list_women: raise Warning('A compiled all events file has been found in the current directory.')
dfs = None
for file in file_list_women:
df = pd.read_csv(f'{self.data_dir}/{file}')
if dfs is None:
dfs = df.assign(event=file.split('_')[-1].split('.')[0])
continue
dfs = pd.concat([dfs, df.assign(event=file.split('_')[-1].split('.')[0])])
dfs.to_csv(f'{self.data_dir}/all_time_women_all_events.csv', index=False)
return None
def convert_marathons(self, row: str) -> float:
'''Takes a cell of a half-marathon or marathon time from a `pd.DataFrame` and converts it to seconds.'''
if len(row.split(':')) == 3:
return float(row.split(':')[0]) * 3600 + float(row.split(':')[1]) * 60 + float(row.split(':')[2])
return float(row.split(':')[0]) * 60 + float(row.split(':')[1])
def find_last_page_num(self, html_path: str) -> int:
'''Uses `requests`, `re`, and `bs4`'s `BeautifulSoup` to find the maximum page number of a given dataset.
Args:
- html_path (`str`): the html path of the first page of the dataset
Returns:
- last_page_num (`int`): the page number of the last page of data'''
page = requests.get(html_path)
soup = BeautifulSoup(page.text, 'html.parser')
last_page_num = int(soup.find_all('a', attrs={'data-page': re.compile('[0-9]+')})[-1]['data-page'])
return last_page_num