-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataextractors.py
More file actions
240 lines (219 loc) · 7.87 KB
/
Copy pathdataextractors.py
File metadata and controls
240 lines (219 loc) · 7.87 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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
def extract_runners(soup):
runners = []
for tableBody in soup.select("table.resultRaceGrid tbody"):
for race_items in tableBody.select("tr:nth-of-type(2)"):
runner = {}
hid = race_items["data-hid"].strip()
position = race_items.select("td:nth-of-type(2) h3")[0].text.strip()
dist = race_items.select("td:nth-of-type(3)")[0].text.strip()
age = race_items.select("td:nth-of-type(5)")[0].text.strip()
official_rating = race_items.select("td:nth-of-type(8)")[0].text.strip()
weight, headgear = extract_impediments(race_items.select("td:nth-of-type(6)"))
tid = extract_trainer(race_items.select("td:nth-of-type(7) a"))
jid, claiming_weight = extract_jockey(tableBody.select("tr:nth-of-type(3)"))
runner["hid"] = hid
runner["position"] = position
runner["dist"] = dist
runner["age"] = age
runner["official_rating"] = official_rating
runner["weight"] = weight
runner["weight"]["claiming_weight"] = claiming_weight
runner["weight"]["total"] = compute_weight(runner) - claiming_weight
runner["headgear"] = headgear
runner["tid"] = tid
runner["jid"] = jid
runners.append(runner)
return runners, None
def extract_trainer(soup):
if len(soup) > 0:
return re.search('\d+', soup[0].get("href").strip())
return None
def extract_jockey(soup):
if len(soup) > 0:
jid = re.search('\d+', soup[0].select('a')[0].get("href").strip())
tag = soup[0].select('sup')
if len(tag) > 0:
claiming_weight = tag[0].text.strip()
claiming_weight = int(claiming_weight)
return jid, claiming_weight
return None, 0
def extract_headgear(result, value):
if "b" in value:
result["blinkers"] = True
if "v" in value:
result["visor"] = True
if "h" in value:
result["hood"] = True
if "e/s" in value:
result["eye_shield"] = True
if "t" in value:
result["tongue_strap"] = True
if "p" in value:
result["sheepskin_cheekpieces"] = True
def compute_weight(runner):
weightDict = runner["weight"]
lb = weightDict["lb"] or 0
stone = weightDict["st"] or 0
return int(stone) * 14 + int(lb)
def extract_impediments(soup):
weight = {}
headgear = {}
count = 0
for value in content.stripped_strings:
count += 1
if count == 1:
m = re.search('(\d+)-(\d+)', value)
if m != None:
weight["st"] = m.group(1)
weight["lb"] = m.group(2)
if count == 2:
if content.img != None:
weight["overweight"] = value
else:
extract_headgear(headgear, value)
if count == 3:
if content.img != None:
extract_headgear(headgear, value)
else:
if value == "1":
headgear["first_time"] = True
if count == 4 and content.img != None:
if value == "1":
headgear["first_time"] = True
return (weight, headgear)
def extract_race_type(soup):
try:
h3 = soup.select("div.popUpHead h3")[0]
m = re.findall(' Chase | Hurdle', h3.text)
if len(m) == 0 or all(x == "" for x in m[0]):
return "flat", None
else:
return "jumps", None
except IndexError:
return None, {"message": "Race title was not found"}
def extract_runner_distance(distance_string):
distance = 0
if distance_string != None:
fractions = {u'\xbd': 0.5, u'\xbc': 0.25, u'\xbe': 0.75,
'nk': 0.35, 'snk': 0.25, 'hd': 0.22, 'shd': 0.18,
'dht': 0, 'nse': 0.14, 'dist': 50, 'min': 0.8}
(whole, fractional) = re.match('(\d+)?(\D+)?', distance_string).groups()
if whole == None and fractional == None:
return 0
if whole != None:
distance += int(whole)
if fractional != None:
distance += fractions[fractional.replace("fs", "")]
return distance * 8 * 0.33333
def extract_winning_time(soup):
race_info = soup.select("div.raceInfo")
if len(race_info) == 0:
return None, {"message": "no_race_info"}
else:
if "TIME" not in race_info[0].text:
return None, {"message": "no_time", "race_info": race_info[0].text}
else:
m = re.findall('TIME ([0-9]*\.?[0-9]+)s|TIME (\d+)m ([0-9]*\.?[0-9]+)s', race_info[0].text)
if len(m) == 0 or all(x == "" for x in m[0]):
return None, {"message": "time_format", "race_info": race_info[0].text}
else:
matches = m[0]
try:
if matches[0] != "":
secs = float(matches[0])
if secs >= 20:
return secs, None
elif secs < 20 and secs > 0:
splits = matches[0].split(".")
return float(splits[0]) * 60 + float(splits[1]), None
else:
return float(matches[1]) * 60 + float(matches[2]), None
except ValueError:
return None, {"message": "time_format", "race_info": race_info[0].text}
return None, {"message": "mega failure"}
def get_aus_going(going_text):
if re.search("fast|good 2", going_text, re.IGNORECASE) != None:
return {"fast": 1, "medium": 0, "slow": 0}
elif re.search("good 3|dead 4|dead 5", going_text, re.IGNORECASE) != None:
return {"fast": 0, "medium": 1, "slow": 0}
else:
return {"fast": 0, "medium": 0, "slow": 1}
def get_usa_going(going_text):
if re.search("firm|fast", going_text, re.IGNORECASE) != None:
return {"fast": 1, "medium": 0, "slow": 0}
elif re.search("good|cuppy|muddy", going_text, re.IGNORECASE) != None:
return {"fast": 0, "medium": 1, "slow": 0}
else:
return {"fast": 0, "medium": 0, "slow": 1}
def get_going(going_text):
if re.search("hard|firm|fast", going_text, re.IGNORECASE) != None:
return {"fast": 1, "medium": 0, "slow": 0}
elif re.search("good to firm|good|good to soft|yielding|standard to fast|standard|standard to slow", going_text, re.IGNORECASE) != None:
return {"fast": 0, "medium": 1, "slow": 0}
else:
return {"fast": 0, "medium": 0, "slow": 1}
def get_flat_distance_category(distance):
if distance <= 1540:
return {"short": 1, "medium": 0, "long": 0}
elif distance <= 2640:
return {"short": 0, "medium": 1, "long": 0}
else:
return {"short": 0, "medium": 0, "long": 1}
def get_jumps_distance_category(distance):
if distance <= 4400:
return {"short": 1, "medium": 0, "long": 0}
elif distance <= 6160:
return {"short": 0, "medium": 1, "long": 0}
else:
return {"short": 0, "medium": 0, "long": 1}
def value_from_values(values, indices):
for i in indices:
if values[i] != "":
return int(values[i])
return 0
def compute_distance(matches):
if len(matches) == 0 or all(x == "" for x in matches[0]):
return None
""" miles are in positions 0, 3, 5, 9 """
miles = value_from_values(matches[0], [0, 3, 5, 9])
""" furlongs are in positions 1, 6, 7, 10 """
furlongs = value_from_values(matches[0], [1, 6, 7, 10])
""" yards are in positions 2, 4, 8, 11 """
yards = value_from_values(matches[0], [2, 4, 8, 11])
return miles * 8 * 220 + furlongs * 220 + yards
def extract_race_going(soup):
try:
h1 = soup.select("h1")[0]
countryMatches = re.findall(' (\(USA\)) | (\(AUS\)) ', h1.text)
going_text = soup.select("div.popUpHead ul li")[0].text
if len(countryMatches) == 0:
return get_going(going_text), None
else:
if countryMatches[0] != "":
return get_usa_going(going_text), None
else:
return get_aus_going(going_text), None
except IndexError:
return None, {"message": "Race title was not found"}
def extract_race_distance(soup):
info = soup.select("div.popUpHead div.leftColBig ul li")
if len(info) == 0:
return None, {"message": "No header"}
else:
parentheful_matches = re.findall('\((\d+)m(\d+)f(\d+)y\)|\((\d+)m(\d+)y\)|\((\d+)m(\d+)f\)|\((\d+)f(\d+)y\)|\((\d+)m\)|\((\d+)f\)|\((\d+)y\)', info[0].text)
parenthesisless_matches = re.findall('(\d+)m(\d+)f(\d+)y |(\d+)m(\d+)y |(\d+)m(\d+)f |(\d+)f(\d+)y |(\d+)m |(\d+)f |(\d+)y ', info[0].text)
distance = compute_distance(parentheful_matches)
if distance == None:
distance = compute_distance(parenthesisless_matches)
if distance == None or distance == 0:
return None, {"message": "Distance format is bad"}
race_type, errors = extract_race_type(soup)
if errors == None:
if race_type == "flat":
return {"value": distance, "category": get_flat_distance_category(distance)}, None
else:
return {"value": distance, "category": get_jumps_distance_category(distance)}, None
return None, errors