-
Notifications
You must be signed in to change notification settings - Fork 2
/
finnmarkslopet_live.py
558 lines (447 loc) · 18 KB
/
finnmarkslopet_live.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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Exemple d'une course bien remplie : https://www.wikidata.org/wiki/Q19455277
import requests
from bs4 import BeautifulSoup, NavigableString
import datetime
import json
import os # Files and folder manipulations
import re # Regular expressions
import csv # CSV file manipulations
import sys
from collections import Counter
from termcolor import colored
verbose = 1
try:
# For Python 3.0 and later
from urllib.request import urlopen
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen
class WikidataItem(object):
def __init__(self):
self.label = ''
self.qid = ''
def set_qid(self):
if self.label in self.qids_list:
self.qid = self.qids_list[self.label]
elif verbose:
print('Unknown {}: {}'.format(self.type, self.label))
if self.type == 'race':
unknown_races_qids.append(self.label)
else:
unknown_mushers_qids.append(self.label)
class Musher(WikidataItem):
def __init__(self, m_id):
super().__init__()
self.type = "musher"
self.id = m_id
self.number = ''
self.country = ''
self.country_qid = ''
self.residence = ''
self.final_rank = 0
# 0: didn't finish (no explanation) | -1: disqualified
self.last_checkpoint = ''
self.last_checkpoint_qid = ''
self.dogs_number_start = 0
self.dogs_number_end = 0
self.qids_list = musher_qids
self.finish_time = 0
self.total_time = 0
class Race(WikidataItem):
"""An edition of the Finnmarkslopet."""
# Faire un CSV par course
# colonnes :
# numéro de dossard,
# nom du musher,
# classement final,
# lieu d'abandon le cas échéant,
# source
def __init__(self, r_id):
super().__init__()
self.type = "race"
self.id = r_id
self.raw_checkpoints = []
self.checkpoints = []
self.raw_start = ''
self.mushers=[]
self.qids_list = race_qids
self.winner = ""
self.distance = 0
def statusUrl(self):
"""Returns the URL of the status page for this race"""
return root_url + '/race/results/status.jsp?lang=en&rid=' + str(self.id)
def resultsUrl(self):
"""Returns the URL of the results page for this race"""
return root_url + '/race/results/results.jsp?lang=en&rid=' + str(self.id)
def musherResultsUrl(self, m_id):
"""Returns the URL of the results page for a musher."""
return root_url + '/race/results/musher.jsp?lang=en&rid=' + str(self.id) + '&entr.id=' + str(m_id)
def getStatus(self):
"""Get all data that can be scrapped from the status page"""
if verbose:
print('Parsing race #{}'.format(self.id))
status_url = self.statusUrl()
response = requests.get(status_url)
soup = BeautifulSoup(response.text)
# Header is in the form "<race> <year>", we need "<year> <race>"
header = soup.select('#rshead')[0].find_all('span')
raw_label = header[0].string.split()
for i in raw_label:
if i.isdigit():
self.year = i
raw_label.remove(i)
self.label = "{} {}".format(self.year, ' '.join(raw_label))
self.raw_start = header[2].string
self.set_qid()
if verbose:
print(colored('Race: {} ({}) - startdate: {}'.format(self.label, self.qid, self.raw_start), 'green'))
###### STATUS GRID ######
# The page has an awful structure with nested tables
# so best to get down the tree manually.
status = soup.select('#status-grid')
status = status[0].contents[1].contents[1::2]
del status[0] #Remove the title lign
###### CHECKPOINTS ######
# Get the start point
checkpoints_data = status.pop(0).find_all('td')
#Remove the "→" cells
checkpoints_data = checkpoints_data[::2]
if verbose:
print("Checkpoints:")
for c in checkpoints_data:
checkpoint = c('img')[0].get('title').split(':')[0].strip()
self.raw_checkpoints.append(checkpoint)
if checkpoint not in all_checkpoints:
all_checkpoints.append(checkpoint)
if checkpoint in checkpoints_qids:
self.checkpoints.append(checkpoints_qids[checkpoint])
if verbose:
print(checkpoint, checkpoints_qids[checkpoint])
else:
if verbose:
print(colored("Unknown checkpoint: '{}'".format(checkpoint), 'yellow'))
unknown_checkpoints_qids.append("{} (race: {})".format(checkpoint, self.qid))
self.distance = int(checkpoints_data[-1]('img')[0].get('title').split(':')[1].split()[0])
if verbose:
print("\n")
print("Total distance: {}".format(self.distance))
print("\n")
###### MUSHER IDS ######
# List of registered mushers that didn't even start the race
did_not_start = [133]
for s in status:
if len(s.contents) > 1:
url = s.find_all('a')
if len(url):
m_id = int(re.findall(r'\d+$', url[0].get('href'))[0])
if m_id not in did_not_start:
self.mushers.append(m_id)
if verbose >=1:
print("{} mushers in the race".format(len(self.mushers)))
print(self.mushers)
def getMusherResults(self, m_id):
global quick_statements
musher = Musher(m_id)
musher_results_url = self.musherResultsUrl(m_id)
response = requests.get(musher_results_url)
soup = BeautifulSoup(response.text)
# Another tables mess to sort manually
tables = soup.select('table')
header = tables[1]('td')
# The first <td> contains the number and the name with a '.' in between
# The name of the musher can also have a '.', though...
raw_header = header[0].string.split('.')
raw_header = [item.strip() for item in raw_header]
musher.number = raw_header.pop(0)
musher.label = re.sub(' +',' ', '. '.join(raw_header)).strip()
musher.country = header[2]('img')[0].get('title')
musher.country_qid = country_qids[musher.country]
musher.residence = header[4].string
musher.set_qid()
all_mushers.append(musher.label)
if musher.country not in all_countries:
all_countries.append(musher.country)
if musher.residence not in all_cities:
all_cities.append(musher.residence)
if len(header) == 7:
if header[6].string == '(Disqualified)':
musher.final_rank = -1
else:
musher.final_rank = int(re.findall(r'\d+$', header[6].string)[0])
if musher.final_rank == 1:
self.winner = musher.qid
raw_checkpoints = tables[3]('tr')[1::]
cleaned_checkpoints = []
for row in raw_checkpoints:
columns = row('td')
current_row = {}
current_row['checkpoint'] = strip_tags(columns[0])
current_row['time_in'] = strip_tags(columns[1])
current_row['time_out'] = strip_tags(columns[2])
if len(columns) > 3:
dogs = strip_tags(columns[3])
if dogs:
if not dogs.isdigit():
dogs = re.sub('<s></s>', '', dogs) #empty <s> tag on some lines...
dogs = int(dogs)
current_row['dogs'] = dogs
cleaned_checkpoints.append(current_row)
#check if the musher did start the race at all
if not cleaned_checkpoints[0]['time_out']:
self.mushers.remove(m_id)
else:
musher.dogs_number_start = cleaned_checkpoints[0]['dogs']
for i in cleaned_checkpoints:
if i['dogs']:
musher.dogs_number_end = i['dogs']
if i['time_in']:
musher.last_checkpoint = i['checkpoint']
start_time = convert_time(self.year, cleaned_checkpoints[0]['time_out'])
musher.finish_time = convert_time(self.year,i['time_in'])
musher.total_time = int((musher.finish_time - start_time).total_seconds()/60)
# if the musher didn't even manage to the first checkpoint
if not musher.last_checkpoint:
musher.last_checkpoint = cleaned_checkpoints[0]['checkpoint']
musher.finish_time = convert_time(self.year, cleaned_checkpoints[0]['time_out'])
#manually force the number of dogs where the table is wrong
dog_number_error = [312, 314]
if musher.id in dog_number_error:
musher.dogs_number_end = 0
musher.dogs_number_start = 0
if musher.last_checkpoint in checkpoints_qids:
musher.last_checkpoint_qid = checkpoints_qids[musher.last_checkpoint]
else:
if verbose:
print(colored("Unknown checkpoint: {}".format(musher.last_checkpoint), 'yellow'))
unknown_checkpoints_qids.append("{} ({} in the {})".format(musher.last_checkpoint, musher.qid, self.qid))
if musher.dogs_number_start <=0:
if verbose:
print(colored("Musher with no dogs at start: {} ({}) in the {} ({})".format(musher.label, musher.id, self.label, self.qid), 'yellow'))
no_dogs_at_start.append("{} ({}) in the {} ({})".format(musher.label, musher.id, self.label, self.qid))
if musher.dogs_number_end <=0:
if verbose:
print(colored("Musher with no dogs at the end: {} ({}) in the {} ({}) -- Final rank: {}".format(musher.label, musher.id, self.label, self.qid, musher.final_rank), 'yellow'))
no_dogs_at_end.append("{} ({}) in the {} ({}) -- Final rank: {}".format(musher.label, musher.id, self.label, self.qid, musher.final_rank))
if verbose:
print(musher.label, musher.id, musher.qid, musher.number, musher.country, musher.country_qid, str(musher.total_time), str(musher.final_rank), str(musher.dogs_number_start), str(musher.dogs_number_end), musher.last_checkpoint, musher.last_checkpoint_qid)
#quick_statements += self.participant_quick_statements(musher) + "\n"
def race_quick_statements(self):
reference = "\tS854\t\"{}\"".format(self.statusUrl())
#winner
statement = "{}\tP1346\t{}".format(self.qid, self.winner) + reference + "\n"
# entrants
statement += "{}\tP1132\t{}".format(self.qid, len(self.mushers)) + reference + "\n"
# Starting point
statement += "{}\tP1427\t{}".format(self.qid, self.checkpoints[0]) + reference + "\n"
# Destination
statement += "{}\tP1444\t{}".format(self.qid, self.checkpoints[-1]) + reference + "\n"
# Checkpoints
for k, v in enumerate(self.checkpoints):
rank = k + 1
statement += "{}\tP276\t{}\tP1545\t\"{}\"".format(self.qid, v, rank)
if rank not in [1, len(self.checkpoints)]:
statement += "\tP31\tQ19162210"
statement += reference + "\n"
# Total distance
statement += "{}\tP2043\t{}".format(self.qid, self.distance) + reference + "\n"
return statement
def participant_quick_statements(self, musher):
# qualifiers in reverse order because QS starts by the end
# Doc de QS : https://tools.wmflabs.org/wikidata-todo/quick_statements.php
statement = "{}\tP710\t{}".format(self.qid, musher.qid)
if musher.final_rank > 0 and musher.dogs_number_end > 0:
statement += "\tP2105\t{}".format(musher.dogs_number_end)
if musher.dogs_number_start > 0:
statement += "\tP2103\t{}".format(musher.dogs_number_start)
if musher.number:
statement += "\tP1618\t\"{}\"".format(musher.number)
if musher.finish_time:
statement += "\tP585\t+0000000{0:%Y}-{0:%m}-{0:%d}T00:00:00Z/11".format(musher.finish_time)
if musher.final_rank > 0:
statement += "\tP2047\t{}".format(musher.total_time)
statement += "\tP1352\t{}".format(musher.final_rank)
else:
if musher.last_checkpoint_qid:
statement += "\tP276\t{}".format(musher.last_checkpoint_qid)
if musher.final_rank == -1:
# disqualification
statement += "\tP793\tQ1229261"
else:
# abandon
statement += "\tP793\tQ18595374"
reference = "\tS854\t\"{}\"".format(self.musherResultsUrl(musher.id))
return statement + reference
def strip_tags(string):
if isinstance(string, str):
soup = BeautifulSoup(string)
else:
soup = string
for tag in soup.findAll(True):
s = ""
for c in tag.contents:
if not isinstance(c, NavigableString):
c = strip_tags(c)
s += str(c).strip()
tag.replaceWith(s)
return soup.text.strip()
def convert_time(year, date_string):
# base format is "D/M H:M"
date_chunks = date_string.split()
day_month = date_chunks[0].split('/')
day = int(day_month[0])
month = int(day_month[1])
hour_min = date_chunks[1].split(':')
hour = int(hour_min[0])
minute = int(hour_min[1])
return datetime.datetime(int(year), month, day, hour, minute)
def import_ids():
"""
Import the qids of the mushers, checkpoints and races on Wikidata
"""
with open(races_dir + 'finnmarkslopet/' + 'finnmarkslopet-qid.csv', 'r') as csv_race_ids:
reader = csv.DictReader(csv_race_ids)
for row in reader:
race_qids.update({row['race']: row['qid'] })
csv_race_ids.closed
with open(races_dir + 'finnmarkslopet/' + 'checkpoint-id.csv', 'r') as csv_checkpoints_ids:
reader = csv.DictReader(csv_checkpoints_ids)
for row in reader:
checkpoints_qids.update({row['Checkpoint']: row['qid'] })
csv_checkpoints_ids.closed
with open(races_dir + 'mushers-qid.csv', 'r') as csv_musher_ids:
reader = csv.DictReader(csv_musher_ids)
for row in reader:
if verbose and row['label'] in musher_qids.keys():
print('Duplicate Musher: {}'.format(row['label']))
musher_qids.update({row['label']: row['qid'] })
if row['alias']:
aliases = row['alias'].split('|')
for alias in aliases:
alias = alias.strip()
if verbose and alias in musher_qids.keys():
print('Duplicate Musher: {}'.format(alias))
musher_qids.update({alias: row['qid'] })
csv_musher_ids.closed
def parse_single_race(r_id, m_id = 0):
global quick_statements
r = Race(r_id)
r.getStatus()
if m_id:
r.getMusherResults(m_id)
else:
for m in r.mushers:
r.getMusherResults(m)
quick_statements += r.race_quick_statements() + "\n"
def parse_all_races():
for r_id in races_ids:
parse_single_race(r_id)
def save_quick_statements(text):
if len(text):
file = 'qs.txt'
with open(file,"w") as f:
f.write(text)
f.close()
"""
Presets
"""
country_qids = {
'Aragon': 'Q29', #Spain
'Austria': 'Q40',
'Basque country': 'Q47588',
'Belgia': 'Q31',
'Catalonia': 'Q29', #Spain
'Czech Republic': 'Q213',
'Denmark': 'Q35',
'England': 'Q145', #UK
'Faroe Islands': 'Q4628',
'Finland': 'Q33',
'France': 'Q142',
'Germany': 'Q183',
'Great Britain': 'Q145', #UK
'Hungary': 'Q28',
'Iceland': 'Q189',
'Italy': 'Q38',
'Nederland': 'Q55',
'Norway': 'Q20',
'Poland': 'Q36',
'Russia': 'Q159',
'Scotland': 'Q145', #UK
'Slovakia': 'Q214',
'Spain': 'Q29',
'Sweden': 'Q34',
'Swiss': 'Q39',
'USA': 'Q30',
'Uruguay': 'Q77',
'Wales': 'Q145' #UK
}
dropbox_dir = os.environ['HOME'] + "/Dropbox/"
races_dir = dropbox_dir + 'finnmarkslopet/'
race_qids = {}
checkpoints_qids = {}
musher_qids = {}
unknown_races_qids = []
unknown_checkpoints_qids = []
unknown_mushers_qids = []
no_dogs_at_start = []
no_dogs_at_end = []
import_ids()
quick_statements = ""
"""
The main part of the script
"""
# Get the races list
races_ids = []
all_countries = []
all_cities = []
all_checkpoints = []
all_mushers = []
root_url = 'http://www.finnmarkslopet.no'
index_url = root_url + '/rhist/results.jsp?lang=en'
response = requests.get(index_url)
soup = BeautifulSoup(response.text)
races = soup.select('table.winners a')
#"""
for r in races:
r_id = int(re.findall(r'\b\d+\b', r.get('href'))[0])
races_ids.append(r_id)
# Cycle trough the races
if len(sys.argv) > 1:
if all(arg.isdigit() for arg in sys.argv[1:]):
if len(sys.argv) == 3:
parse_single_race(sys.argv[1], sys.argv[2]) #parse a single musher in a single race
else:
parse_single_race(sys.argv[1]) #parse all mushers in a single race
else:
raise ValueError("Invalid arguments")
else:
parse_all_races()
save_quick_statements(quick_statements)
print("\n\n=========")
print("Checkpoints:\n")
print(sorted(all_checkpoints))
print("\n\n=========")
print("Countries:\n")
print(sorted(all_countries))
print("\n\n=========")
#print("Cities:\n")
#print(all_cities)
#print("\n\n=========")
print("Mushers:\n")
print(sorted(all_mushers))
print("\n\n=========")
print("Unknown qIDs:\n")
print("races:")
print(sorted(unknown_races_qids))
print("checkpoints")
print(sorted(unknown_checkpoints_qids))
print("mushers")
print(sorted(unknown_mushers_qids))
print("\n\n=========")
print("No dogs at start:")
print(sorted(no_dogs_at_start))
print("No dogs in the end:")
for n in no_dogs_at_end:
print(n)
#"""