-
Notifications
You must be signed in to change notification settings - Fork 2
/
collect_data.py
305 lines (277 loc) · 10.3 KB
/
collect_data.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
import time
import json
import requests
import argparse
import sqlite3
from datetime import datetime, timedelta
# Global vars
API_KEY = ""
db = None
conn = None
SHALLOW_SCRAPE = False
def isStringInt(s):
try:
int(s)
return True
except ValueError:
return False
def parseEvent(event_id):
def parsePool(pool):
#TODO: merge duplicate code with parseDE
try:
bouts = pool["bouts"]
except:
return
for bout in bouts:
if "fencers" not in bout:
continue
fs = bout["fencers"]
if len(fs) != 2:
continue
i,j = 0,1
if fs[0]["id"] > fs[1]["id"]:
i,j = 1,0
db.execute("""
INSERT OR IGNORE INTO bouts
(boutid, eventid, fencer1id, fencer2id, score1, score2, 'type')
VALUES
(%(boutid)s, %(eventid)s, %(fencer1id)s, %(fencer2id)s, %(score1)s, %(score2)s, '%(type)s')
""" % {
"boutid" : bout["id"],
"eventid" : event_id,
"fencer1id" : fs[i]["id"],
"fencer2id" : fs[j]["id"],
"score1" : fs[i]["score"],
"score2" : fs[j]["score"],
"type" : "pool",
})
def parseDE(de_table):
#TODO: merge duplicate code with parsePool
try:
bouts = de_table["bouts"]
except:
return
for bout in bouts:
if "fencers" not in bout:
continue
fs = bout["fencers"]
i,j = 0,1
if len(fs) != 2:
continue
if fs[0]["id"] > fs[1]["id"]:
i,j = 1,0
if len(fs) == 2: # In case of a bye
db.execute("""
INSERT OR IGNORE INTO bouts
(boutid, eventid, fencer1id, fencer2id, score1, score2, 'type')
VALUES
(%(boutid)s, %(eventid)s, %(fencer1id)s, %(fencer2id)s, %(score1)s, %(score2)s, '%(type)s')
""" % {
"boutid" : bout["id"],
"eventid" : event_id,
"fencer1id" : fs[i]["id"],
"fencer2id" : fs[j]["id"],
"score1" : fs[i]["score"],
"score2" : fs[j]["score"],
"type" : "de",
})
payload = {"_api_key" : API_KEY,
"_per_page" : "100", # won't be more than 100 events in a tournament right?
"event_id" : event_id,
}
r = requests.get("https://api.askfred.net/v1/roundresult", params=payload)
if r.status_code == 500:
print "Request failed with code 500. Payload: " + "https://api.askfred.net/v1/roundresult" + str(payload)
return
event_results = r.json()
if int(event_results["total_matched"]) > 100:
raise Exception("more than 100 roundresult with same id, wtf. event_id = " + event_id)
rnds = event_results["rounds"]
for rnd in rnds:
if rnd["round_type"] == "pool":
for pool in rnd["pools"]:
parsePool(pool)
elif rnd["round_type"] in ("de", "fo3", "apf8", "rep16"):
for de_table in rnd["de_tables"]:
parseDE(de_table)
else:
with open("to_verify.txt", "a") as f:
f.write(rnd["round_type"] + " in eventid=" + str(event_id))
for de_table in rnd["de_tables"]:
parseDE(de_table)
def scrapeResults(begin_date, end_date):
i = 1
batch_num = 100
while True:
payload = {"_api_key" : API_KEY,
"_sort" : "start_date_desc",
"_per_page" : batch_num,
"_page" : i,
"start_date_lte" : end_date.isoformat().split("T")[0],
"start_date_gte" : begin_date.isoformat().split("T")[0],
}
r = requests.get("https://api.askfred.net/v1/tournament", params=payload)
rjson = r.json()
for tournament in rjson["tournaments"]:
print "parsing " + str(tournament["id"])
if SHALLOW_SCRAPE and db.execute("SELECT * FROM tournaments WHERE tournamentid = " + str(tournament["id"])).fetchone():
continue
print tournament
print tournament["start_date"]
sql_insert = "INSERT OR IGNORE INTO tournaments (tournamentid, start_date) VALUES (%(tid)s, '%(sd)s');" % {"tid" : tournament["id"], "sd" : tournament["start_date"]}
db.execute(sql_insert)
if "events" not in tournament:
continue
for event in tournament["events"]:
db.execute("""INSERT OR IGNORE INTO events
(eventid, tournamentid, weapon)
VALUES
(%(eventid)s, %(tournamentid)s, '%(weapon)s')""" % {
"eventid" : event["id"],
"tournamentid" : event["tournament_id"],
"weapon" : event["weapon"],
})
try:
parseEvent(event["id"])
except:
time.sleep(10)
parseEvent(event["id"])
conn.commit()
time.sleep(10)
if i * batch_num >= int(rjson["total_matched"]):
break
i = i+1
def scrapePromotions(begin_date, end_date):
i = 1
batch_num = 100
while True:
payload = {"_api_key" : API_KEY,
"_sort" : "tournament_start_date_desc",
"_per_page" : batch_num,
"_page" : i,
"tournament_start_date_lte" : end_date.isoformat().split("T")[0],
"tournament_start_date_gte" : begin_date.isoformat().split("T")[0],
}
r = requests.get("https://api.askfred.net/v1/result", params=payload)
rjson = r.json()
for result in rjson["results"]:
db.execute("""INSERT OR IGNORE INTO tournament_results
(fencerid, eventid, weapon, tournamentid, entries, place, start_date)
VALUES
(%(f)s, %(e)s, '%(w)s', %(t)s, %(entries)s, %(place)s, '%(start_date)s')
""" % {
"f" : result["competitor_id"],
"e" : result["event_id"],
"w" : result["weapon"],
"t" : result["tournament_id"],
"entries" : result["entries"],
"place" : result["place"],
"start_date" : result["tournament_start_date"],
})
if "rating_Earned_letter" in result:
db.execute("""INSERT OR IGNORE INTO promotions
(fencerid, weapon, eventid, rating_earned_letter, rating_earned_year, rating_before_letter, rating_before_year)
VALUES
(%(f)s, '%(w)s', %(e)s, '%(letter)s', %(year)s, '%(bletter)s', %(byear)s)
""" % {
"f" : result["competitor_id"],
"e" : result["event_id"],
"w" : result["weapon"],
"letter" : result["rating_Earned_letter"],
"year" : result["rating_Earned_year"],
"bletter" : result["rating_before_letter"],
"byear" : result["rating_before_year"] if "rating_before_year" in result else "NULL",
})
print result["competitor_id"], result["weapon"], result["rating_Earned_letter"], result["tournament_start_date"]
conn.commit()
if i * batch_num >= int(rjson["total_matched"]):
break
i = i+1
time.sleep(10)
def scrapeAllFencers(begin_fencerid=0):
i = 1
batch_num = 100
current_fencerid = None
while True:
payload = {"_api_key" : API_KEY,
"_sort" : "fencer_id_desc",
"_per_page" : batch_num,
"_page" : i,
}
r = requests.get("https://api.askfred.net/v1/fencer", params=payload)
rjson = r.json()
for result in rjson["fencers"]:
sqlcmd = ("""INSERT OR IGNORE INTO fencers
(fencerid, first_name, last_name, birthyear, usfa_id, gender)
VALUES
(%(f)s, '%(fn)s', '%(ln)s', %(b)s, %(uid)s, '%(g)s')
""" % {
"f" : result["id"],
"fn" : result["first_name"].replace("'", "''"), #retarded sql escaping
"ln" : result["last_name"].replace("'", "''"), #retarded sql escaping
"b" : result["birthyear"],
"uid" : result["usfa_id"] if (result["usfa_id"] != "" and isStringInt(result["usfa_id"]) ) else "NULL",
"g" : result["gender"],
})
print sqlcmd
db.execute(sqlcmd)
current_fencerid = int(result["id"])
conn.commit()
if i * batch_num >= int(rjson["total_matched"]):
break
if current_fencerid < begin_fencerid:
break
i = i+1
time.sleep(10)
return
def scrapeAllFencersUpdate():
print db.execute("SELECT MAX(fencerid) FROM fencers").fetchone()
maxfencerid = int(db.execute("SELECT MAX(fencerid) FROM fencers").fetchone()[0])
scrapeAllFencers(maxfencerid)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Collects Askfred bout data')
parser.add_argument("-k", action="store", dest="API_KEY", default="",
help="Askfred REST API key")
parser.add_argument("-d", action="store", dest="db", default="data.db",
help="db target")
parser.add_argument("--scrape-results", action="store_true", dest="scrape_results", default=False,
help="Scrape tournament results. Must use with either --days or --begin-date")
parser.add_argument("--scrape-promotions", action="store_true", dest="scrape_promotions", default=False,
help="Scrape promotions. Must use with either --days or --begin-date")
parser.add_argument("--scrape-fencer", action="store_true", dest="scrape_fencer", default=False,
help="Scrape fencers.")
parser.add_argument("--scrape-fencer-update", action="store_true", dest="scrape_fencer_update", default=False,
help="Scrape fencers (only update).")
parser.add_argument("--days", action="store", dest="scrape_lookback", default=None,
help="Scrape lookback in calendar days")
parser.add_argument("--begin-date", action="store", dest="begin_date", default=None,
help="Scrape begin date in yyyy-mm-dd")
parser.add_argument("--end-date", action="store", dest="end_date", default=datetime.now().isoformat().split("T")[0],
help="Scrape end date in yyyy-mm-dd")
parser.add_argument("--shallow", action="store", dest="SHALLOW_SCRAPE", default=False,
help="Do not scrape tournament events if tournament_id already in db")
args = parser.parse_args()
API_KEY = args.API_KEY
SHALLOW_SCRAPE = args.SHALLOW_SCRAPE
conn = sqlite3.connect(args.db, timeout=20)
db = conn.cursor()
end_date = datetime.strptime(args.end_date, '%Y-%m-%d')
if args.scrape_fencer or args.scrape_fencer_update:
begin_date = datetime.now()
end_date = datetime.now()
if args.begin_date != None:
begin_date = datetime.strptime(args.begin_date, '%Y-%m-%d')
elif args.scrape_lookback != None:
begin_date = datetime.now() - timedelta(days=int(args.scrape_lookback))
print begin_date, end_date
if args.scrape_results:
scrapeResults(begin_date, end_date)
elif args.scrape_promotions:
scrapePromotions(begin_date, end_date)
elif args.scrape_fencer:
scrapeAllFencers()
elif args.scrape_fencer_update:
scrapeAllFencersUpdate()
conn.commit()
conn.close()
#parseEvent(118025)