-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcric_info.py
207 lines (193 loc) · 5.79 KB
/
cric_info.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
import requests
import json
def get_match_details(series_id, match_id):
match_link = "https://hs-consumer-api.espncricinfo.com/v1/pages/match/details?lang=en&seriesId={series_id}&matchId={match_id}&latest=true".format(
series_id=series_id, match_id=match_id
)
detail_res = requests.get(match_link).text
match_detail_data = json.loads(detail_res)["match"]
status_text = match_detail_data["statusText"]
team_1_score = match_detail_data["teams"][0]["score"]
team_2_score = match_detail_data["teams"][1]["score"]
team_1_name = match_detail_data["teams"][0]["team"]["name"]
team_2_name = match_detail_data["teams"][1]["team"]["name"]
# team_1_abv = match_detail_data["teams"][0]["team"]["abbreviation"]
# team_2_abv = match_detail_data["teams"][1]["team"]["abbreviation"]
try:
team_1_inning = int(match_detail_data["teams"][0]["inningNumbers"][0])
except:
team_1_inning = None
try:
team_2_inning = int(match_detail_data["teams"][1]["inningNumbers"][0])
except:
team_2_inning = None
format = match_detail_data["format"]
match_name = team_1_name + " vs " + team_2_name
# print(
# status_text,
# team_1_score,
# team_2_score,
# team_1_name,
# team_2_name,
# team_1_inning,
# team_2_inning,
# format,
# )
# team_1_scrcard = team_1_abv + " - " + team_1_score
# team_2_scrcard = team_2_abv + " - " + team_2_score
if format == "TEST":
if (
match_name
and team_1_score
and team_2_score
and status_text
and team_1_inning
and team_2_inning
):
string = (
match_name
+ "\n"
+ team_1_name
+ " "
+ str(team_1_inning)
+ "inning"
+ " - "
+ team_1_score
+ "\n"
+ team_2_name
+ " "
+ str(team_2_inning)
+ "inning"
+ " - "
+ team_2_score
+ "\n"
+ status_text
)
return string
elif (
match_name
and team_1_score
and status_text
and team_1_inning
and team_2_score == None
and team_2_inning == None
):
string = (
match_name
+ "\n"
+ team_1_name
+ " "
+ str(team_1_inning)
+ "inning"
+ " - "
+ team_1_score
+ "\n"
+ status_text
)
return string
elif (
match_name
and team_1_score == None
and team_2_score
and status_text
and team_1_inning == None
and team_2_inning
):
string = (
match_name
+ "\n"
+ team_2_name
+ " "
+ str(team_2_inning)
+ "inning"
+ " - "
+ team_2_score
+ "\n"
+ status_text
)
return string
elif (
match_name
and team_1_score == None
and team_2_score == None
and status_text
and team_1_inning == None
and team_2_inning == None
):
string = match_name + "\n" + status_text
return string
else:
if match_name and team_1_score and team_2_score and status_text:
string = (
match_name
+ "\n"
+ team_1_name
+ " - "
+ team_1_score
+ "\n"
+ team_2_name
+ " - "
+ team_2_score
+ "\n"
+ status_text
)
return string
elif team_2_score == None and status_text and match_name:
string = (
match_name
+ "\n"
+ team_1_name
+ " - "
+ team_1_score
+ "\n"
+ status_text
)
return string
elif team_1_score == None and status_text and match_name:
return (
match_name
+ "\n"
+ team_2_name
+ " - "
+ team_2_score
+ "\n"
+ status_text
)
else:
return "No Live Data"
# return status_text
def get_match_dict_list():
match_dict_list = []
try:
response = requests.get(
"https://hs-consumer-api.espncricinfo.com/v1/pages/matches/current?latest=true"
).text
match_data = json.loads(response)["matches"]
match_dict_list = [
{
"match_name": match["slug"],
"match_id": match["objectId"],
"series_id": match["series"]["objectId"],
"team-1": match["teams"][0]["team"]["longName"],
"team-2": match["teams"][1]["team"]["longName"],
}
for match in match_data
if match["status"] == "Live"
]
except Exception:
pass
match_list = ""
# print("Choose any one of the following: \n")
for i in range(0, len(match_dict_list)):
match_list += (
str(i)
+ ". "
+ match_dict_list[i]["team-1"]
+ " vs "
+ match_dict_list[i]["team-2"]
+ "\n"
)
if match_list:
return match_list, match_dict_list
else:
return "No Live Matches", match_dict_list