-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCricWiz.py
254 lines (216 loc) · 8.57 KB
/
CricWiz.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
from pycricbuzz import Cricbuzz
import argparse
from tabulate import tabulate
import sys
from time import sleep
import subprocess as s
def main():
"""Handle all the processing and choices
"""
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--livescore", required=True, help="Show the liveScore")
args = vars(parser.parse_args())
if args['livescore']:
Rmatches()
sleep(1)
choice = input(colors("\nDo you want to get details of any of the above matches? (y/n) :", 36))
if choice.lower() == "y":
sleep(1)
ans = input("\nWhat do you want to get?\n\
1. Scorecard\n\
2. Commentary\n\
3. Squad\nEnter your choice: ")
sleep(1)
match_id = (input("\nEnter the Match ID of that match: "))
ch = check(match_id)
if ans == "1":
if ch == "preview":
print(colors("\nMatch has not yet started!\n", 31))
elif ch == "inprogress":
scoreCard(match_id)
notified = input("Hey there, This match is live do you want to get notified about this match?(y/n)")
if notified.lower() == "y":
print(colors("You will be notified!( Just press Ctrl + c to stop getting notifications)", 32))
try:
while True:
send(match_id)
except KeyboardInterrupt:
pass
else:
scoreCard(match_id)
elif ans == "2":
if ch != "preview":
commentary(match_id)
else:
print(colors("\nMatch has not yet started!\n", 31))
elif ans == "3":
if ch != "preview":
squad(match_id)
else:
print(colors("\nMatch has not yet started!\n", 31))
else:
print(colors("\nWrong choice!Please select a valid choice!\n", 31))
elif choice.lower() == "n":
print(colors("\nThank You for using CricWiz, have a nice day!\n", 35))
sys.exit()
else:
print(colors("\nWrong choice!Please select a valid choice!\n", 31))
def Rmatches():
"""To fetch the Recent matches held or upcoming or live matches using the cricbuzz object"""
c = Cricbuzz()
matches = c.matches()
headers = ["S.No", "Id", "Head", "Teams", "MatchType", "Status"]
my_data = []
counter = 1
for counter, match in enumerate(matches, start=1):
try:
idey = matches[counter - 1]['id']
head = matches[counter - 1]['srs']
teams = matches[counter - 1]['mchdesc']
matchType = matches[counter - 1]['type']
status = matches[counter - 1]['status']
my_data.append([counter, idey, head, teams, matchType, status])
except KeyError:
pass
print(colors(tabulate(my_data, headers=headers, tablefmt="fancy_grid"), 32))
def commentary(mid):
""" To Fetch commentary of the match"""
c = Cricbuzz()
matches = c.matches()
for match in matches:
if match['id'] == mid:
commentary = c.commentary(match['id'])['commentary']
print("\n--COMMENTARY--\n")
for index in commentary:
print(colors(index, 32))
print("\n")
def scoreCard(mid):
""" To Fetch scorecard of a match"""
c = Cricbuzz()
matches = c.matches()
my_data = []
for match in matches:
if match['id'] == mid:
game = c.scorecard(match['id'])
status = game['matchinfo']['status']
desc = game['matchinfo']['mchdesc']
state = game['matchinfo']['mchstate']
print(colors("\n{} | {} | {}\n".format(desc, status, state), 33))
try:
for i in range(2):
wickets = game['scorecard'][i]['wickets']
runs = game['scorecard'][i]['runs']
bowlteam = game['scorecard'][i]['bowlteam']
overs = game['scorecard'][i]['overs']
batteam = game['scorecard'][i]['batteam']
runrate = game['scorecard'][i]['runrate']
print(colors("\n{} - {} / {}({} Ovs)".format(batteam, runs, wickets, overs), 32))
sleep(1)
print("\n--BATTING CARD--\n")
battCard(game, i)
sleep(1)
print("\n--BOWLING CARD--\n")
bowlCard(game, i)
sleep(1)
except IndexError:
pass
def bowlCard(game, i):
""" A function to print the bowling card in a tabular form, which contains:
Name of the Bowler
Maidens bowled by the bowler
Overs bowled by the bowler
Runs conceded by the bowler
Wickets taken by the bowler
"""
header = ["Name", "Maidens", "Overs", "Runs", "Wickets"]
my_data = []
try:
for j in range(11):
maidens = game['scorecard'][i]['bowlcard'][j]['maidens']
overs = game['scorecard'][i]['bowlcard'][j]['overs']
runs = game['scorecard'][i]['bowlcard'][j]['runs']
name = game['scorecard'][i]['bowlcard'][j]['name']
wickets = game['scorecard'][i]['bowlcard'][j]['wickets']
my_data.append([name, maidens, overs, runs, wickets])
except IndexError:
pass
finally:
print(colors(tabulate(my_data, headers=header, tablefmt="fancy_grid"), 32))
def battCard(game, i):
""" A function to print the batting card in a tabular form, which contains:
Name of the Batsmen
Runs scored by the batsmen
Balls faced by the batsmen
Sixs hit by the batsmen
fours hit by the batsmen
Dismissal
"""
header = ["Name", "runs", "balls", "six", "fours", "dismissal"]
my_data = []
try:
for j in range(11):
name = game['scorecard'][i]['batcard'][j]['name']
runs = game['scorecard'][i]['batcard'][j]['runs']
balls = game['scorecard'][i]['batcard'][j]['balls']
six = game['scorecard'][i]['batcard'][j]['six']
fours = game['scorecard'][i]['batcard'][j]['fours']
dismissal = game['scorecard'][i]['batcard'][j]['dismissal']
my_data.append([name, runs, balls, six, fours, dismissal])
except IndexError:
pass
finally:
print(colors(tabulate(my_data, headers=header, tablefmt="fancy_grid"), 32))
def squad(mid):
""" To display the squad for the individual teams available for the match"""
header = ["S.no", "Player"]
c = Cricbuzz()
game = c.scorecard(mid)
try:
for i in range(2):
my_data = []
squad = game['squad'][i]['members']
sq_team = game['squad'][i]['team']
print(colors("\n--{} Squad--\n".format(sq_team), 32))
counter = 1
for counter, member in enumerate(game['squad'][i]['members'], start=1):
my_data.append([counter, member])
print(tabulate(my_data, headers=header, tablefmt="fancy_grid"))
print("\n")
sleep(1)
except KeyError:
game = c.commentary(mid)
try:
for i in range(1, 4):
print("\n" + game['commentary'][i])
except (IndexError, KeyError):
pass
def check(mid):
""" To check is the match has already started or not"""
c = Cricbuzz()
matches = c.matches()
for match in matches:
if match['id'] == mid:
return match['mchstate']
break
def send(mid):
""" To send the notification about the live score update"""
c = Cricbuzz()
game = c.scorecard(mid)
try:
for i in range(1):
wickets = game['scorecard'][i]['wickets']
runs = game['scorecard'][i]['runs']
bowlteam = game['scorecard'][i]['bowlteam']
overs = game['scorecard'][i]['overs']
batteam = game['scorecard'][i]['batteam']
runrate = game['scorecard'][i]['runrate']
message = "{} - {} / {}( {} Ovs )".format(batteam, runs, wickets, overs)
except IndexError:
pass
s.call(['notify-send', message])
sleep(60)
def colors(string, color):
"""A function to make the things look magical"""
return("\033[%sm%s\033[0m" % (color, string))
if __name__ == '__main__':
main()