-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-all-commentaries-complete1.py
More file actions
57 lines (40 loc) · 1.48 KB
/
get-all-commentaries-complete1.py
File metadata and controls
57 lines (40 loc) · 1.48 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
#!/usr/bin/env python
# coding: utf-8
# ## Create api
# In[1]:
import requests
import json
import os
import io
base_dir = os.getcwd()
db_folder_name = "commentaries1"
# if os.getcwd() != base_dir:
# os.chdir(base_dir)
with io.open("match_ids.json", "r") as file:
matches = json.load(file)
innings_total = [1,2]
for match_id in matches:
os.chdir(os.path.join(base_dir, db_folder_name))
match_dir_name = str(match_id)
if not os.path.exists(match_dir_name):
os.mkdir(match_dir_name)
os.chdir(match_dir_name)
for innings in innings_total:
innings_dir_name = "innings0"+str(innings)
if not os.path.exists(innings_dir_name):
os.mkdir(innings_dir_name)
os.chdir(innings_dir_name)
j=1 #start/restart with a non-zero number
while(j):
apistr = 'http://site.web.api.espn.com/apis/site/v2/sports/cricket/5/playbyplay?contentorigin=espn&event='+ str(match_id) +'&page=' + str(j) + '&period='+str(innings)+'§ion=cricinfo'
r = requests.get(apistr)
data = r.json()
items = data["commentary"]["items"]
if len(items) == 0:
j = 0
os.chdir("..")
continue
#my work goes here--------------------------
with io.open("comm_part"+str(j)+".json", "w") as file:
json.dump(data,file)
j += 1