-
Notifications
You must be signed in to change notification settings - Fork 12
/
cricbuzz.py
149 lines (137 loc) · 4.18 KB
/
cricbuzz.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
import requests
import json
import sys
from bs4 import BeautifulSoup
class Cricbuzz():
url = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml"
def __init__(self):
pass
def getxml(self,url):
try:
r = requests.get(url)
except requests.exceptions.RequestException as e:
print (e)
sys.exit(1)
soup = BeautifulSoup(r.text,"html.parser")
return soup
def matchinfo(self,match):
d = {}
d['id'] = match['id']
d['srs'] = match['srs']
d['mchdesc'] = match['mchdesc']
d['mnum'] = match['mnum']
d['type'] = match['type']
d['mchstate'] = match.state['mchstate']
d['status'] = match.state['status']
return d
def matches(self):
xml = self.getxml(self.url)
matches = xml.find_all('match')
info = []
for match in matches:
info.append(self.matchinfo(match))
return info
def livescore(self,mid):
xml = self.getxml(self.url)
match = xml.find(id = mid)
if match is None:
return "Invalid match id"
if match.state['mchstate'] == 'nextlive':
return "match not started yet"
curl = match['datapath'] + "commentary.xml"
comm = self.getxml(curl)
mscr = comm.find('mscr')
batting = mscr.find('bttm')
bowling = mscr.find('blgtm')
batsman = mscr.find_all('btsmn')
bowler= mscr.find_all('blrs')
data = {}
d = {}
data['matchinfo'] = self.matchinfo(match)
d['team'] = batting['sname']
d['score'] = []
d['batsman'] = []
for player in batsman:
d['batsman'].append({'name':player['sname'],'runs': player['r'],'balls':player['b'],'fours':player['frs'],'six':player['sxs']})
binngs = batting.find_all('inngs')
for inng in binngs:
d['score'].append({'desc':inng['desc'], 'runs': inng['r'],'wickets':inng['wkts'],'overs':inng['ovrs']})
data['batting'] = d
d = {}
d['team'] = bowling['sname']
d['score'] = []
d['bowler'] = []
for player in bowler:
d['bowler'].append({'name':player['sname'],'overs':player['ovrs'],'maidens':player['mdns'],'runs':player['r'],'wickets':player['wkts']})
bwinngs = bowling.find_all('inngs')
for inng in bwinngs:
d['score'].append({'desc':inng['desc'], 'runs': inng['r'],'wickets':inng['wkts'],'overs':inng['ovrs']})
data['bowling'] = d
return data
def commentary(self,mid):
xml = self.getxml(self.url)
match = xml.find(id = mid)
if match is None:
return "Invalid match id"
if match.state['mchstate'] == 'nextlive':
return "match not started yet"
curl = match['datapath'] + "commentary.xml"
comm = self.getxml(curl).find_all('c')
d = []
for c in comm:
d.append(c.text)
data = {}
data['matchinfo'] = self.matchinfo(match)
data['commentary'] = d
return data
def scorecard(self,mid):
xml = self.getxml(self.url)
match = xml.find(id = mid)
if match is None:
return "Invalid match id"
if match.state['mchstate'] == 'nextlive':
return "match not started yet"
surl = match['datapath'] + "scorecard.xml"
scard = self.getxml(surl)
scrs = scard.find('scrs')
innings = scrs.find_all('inngs')
data = {}
data['matchinfo'] = self.matchinfo(match)
squads = scard.find('squads')
teams = squads.find_all('team')
sq = []
sqd = {}
for team in teams:
sqd['team'] = team['name']
sqd['members'] = []
members = team['mem'].split(", ")
for mem in members:
sqd['members'].append(mem)
sq.append(sqd.copy())
data['squad'] = sq
d = []
card = {}
for inng in innings:
bat = inng.find('bttm')
card['batteam'] = bat['sname']
card['runs'] = inng['r']
card['wickets'] = inng['wkts']
card['overs'] = inng['noofovers']
card['runrate'] = bat['rr']
card['inngdesc'] = inng['desc']
batplayers = bat.find_all('plyr')
batsman = []
bowlers = []
for player in batplayers:
status = player.find('status').text
batsman.append({'name':player['sname'],'runs': player['r'],'balls':player['b'],'fours':player['frs'],'six':player['six'],'dismissal':status})
card['batcard'] = batsman
bowl = inng.find('bltm')
card['bowlteam'] = bowl['sname']
bowlplayers = bowl.find_all('plyr')
for player in bowlplayers:
bowlers.append({'name':player['sname'],'overs':player['ovrs'],'maidens':player['mdns'],'runs':player['roff'],'wickets':player['wkts']})
card['bowlcard'] = bowlers
d.append(card.copy())
data['scorecard'] = d
return data