-
Notifications
You must be signed in to change notification settings - Fork 0
/
setupAndDownload.py
155 lines (121 loc) · 4.7 KB
/
setupAndDownload.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
import urllib3
import base64
import json
import re
from textprocess import getName, getNumber
urllib3.disable_warnings(
urllib3.exceptions.InsecureRequestWarning
) #we do not check the certs of the resultssite, because it is not important enouogh for such a project.
http = urllib3.PoolManager()
def upload(imageName):
try:
f = open(imageName,
"rb") # open our image file as read only in binary mode
except Exception as ex:
print(ex)
print('It seems as if your image to be uploaded is not ready.')
quit()
image_data = f.read()
b64_image = base64.standard_b64encode(image_data)
url = 'https://api.imgur.com/3/image'
payload = {'image': b64_image}
headers = {'Authorization': 'Client-ID bb79416fdaad09a'}
try:
response = http.request('POST',
url,
headers=headers,
fields=payload,
retries=False)
except Exception as ex:
print(ex)
print('Imgur could not be reached. Check your Internet.')
quit()
responseJSON = json.loads(response.data)
return responseJSON["data"]["link"]
def download(URL):
try:
r = http.request('GET', URL) #get the actual site
except Exception as ex:
print(ex)
print('Internet not working.')
quit()
page = r.data.decode('UTF-8')
return page
def getProfile(URL):
profileURL = URL + 'gc/stages/winners'
page = download(profileURL)
if 'Could not find race' in page:
raise Exception(URL +
' does not exist, please check your input arguments.')
quit()
stageProfile = [[] for i in range(6)]
stageNames = []
sections = re.split(r'icon profile p', page)[1:]
for i in range(len(sections)):
steepness = int(sections[i][0])
stageProfile[steepness].append(i)
#test if Timetrial
testPart = re.split(r' - ', sections[i])[0]
if testPart[-1] == ')':
stageProfile[0].append(i)
testPart = re.split(r'<a href="', testPart)[1]
stageName = re.split(r'"', testPart)[0]
stageNames.append(stageName)
return (stageProfile, stageNames)
def getStageReadiness(URL):
stagesURL = URL + 'gc/stages/leaders-overview'
page = download(stagesURL)
stages = re.split(r'<tr class=" "><td class=" " >', page)[1:]
numberOfStages = len(stages)
# GC Points KOM Youth Teams
FLAGS = [0, 0, 0, 0, 0]
for stageNumber in range(len(stages)):
testStage = stages[stageNumber]
for i in range(len(FLAGS)):
if i < 4:
testStage = re.split(r'href="rider/', testStage, 1)[1]
else:
testStage = re.split(r'href="team/', testStage, 1)[1]
if not testStage[0] == '"' and (FLAGS[i] == stageNumber
or FLAGS[i] == 0):
#do not start at 0
FLAGS[i] = stageNumber + 1
#pedaleur = re.split(r'data-name="gc" >\+', pedaleur, 1)[1]
return (FLAGS, numberOfStages)
def getTeamnames(URL):
teamDict = dict()
rankingURL = URL + 'gc/startlist/teams-ranked'
page = download(rankingURL)
teamEntries = re.split(r'a href="', page)[1:]
for teamEntry in teamEntries:
teamURL = 'https://www.procyclingstats.com/' + re.split(
r'"', teamEntry, 1)[0]
teamPage = download(teamURL)
(namePart, abbrevationPart) = re.split(r'Abbreviation: </b>', teamPage,
1)
teamAbbrevation = abbrevationPart[:3]
namePart = re.split(r'></span><h1>', namePart, 1)[1]
teamName = re.split(r' ', namePart, 1)[0]
if '<' in teamName:
teamName = re.split(r' <', teamName, 1)[0]
teamDict[teamName] = teamAbbrevation
return teamDict
def getPedaleurs(URL):
pedaleurURL = URL + 'stage-1/startlist'
page = download(pedaleurURL)
allTeams = re.split(r' <a href="team/', page)[1:]
pedaleurInfo = dict()
for team in allTeams:
team = re.split(r'>', team, 1)[1]
EQUIPE = getName(team)
pedaleurs = re.split(r'width: 27px; ">', team)[1:]
for pedaleur in pedaleurs:
NUMBER = getNumber(pedaleur)
pedaleur = re.split(r'<span class="flag ', pedaleur, 1)[1]
NATION = pedaleur[:2]
pedaleur = re.split(r'><span class=""><span>', pedaleur, 1)[1]
NOM = getName(pedaleur)
pedaleur = re.split(r'</span> ', pedaleur, 1)[1]
PRENOM = getName(pedaleur)
pedaleurInfo[NUMBER] = (NOM, PRENOM, NATION, EQUIPE, NUMBER)
return pedaleurInfo