├── requirements.txt ├── cricbuzz.pyc ├── server.py ├── README.md └── cricbuzz.py /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==0.12.1 2 | requests==2.3.0 3 | beautifulsoup4==4.4.0 -------------------------------------------------------------------------------- /cricbuzz.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dynamitechetan/CricketAPI/HEAD/cricbuzz.pyc -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, jsonify, request 2 | import requests 3 | import json 4 | import sys 5 | from bs4 import BeautifulSoup 6 | from cricbuzz import Cricbuzz 7 | from flask import Response 8 | app = Flask(__name__) 9 | 10 | @app.route('/getMatches') 11 | def getMatches(): 12 | c = Cricbuzz() 13 | list = c.matches() 14 | return Response(json.dumps(list), mimetype='application/json') 15 | 16 | @app.route('/livescore') 17 | def getMatchInfo(): 18 | matchId = request.args.get('matchId') 19 | c = Cricbuzz() 20 | list = c.livescore(matchId) 21 | return Response(json.dumps(list), mimetype='application/json') 22 | 23 | @app.route('/scorecard') 24 | def scorecard(): 25 | matchId = request.args.get('matchId') 26 | c = Cricbuzz() 27 | list = c.scorecard(matchId) 28 | return Response(json.dumps(list), mimetype='application/json') 29 | 30 | @app.route('/commentary') 31 | def commentary(): 32 | matchId = request.args.get('matchId') 33 | c = Cricbuzz() 34 | list = c.commentary(matchId) 35 | return Response(json.dumps(list), mimetype='application/json') 36 | 37 | if __name__ == '__main__': 38 | app.run( 39 | host="0.0.0.0", 40 | port=int("1234") 41 | ) 42 | 43 | 44 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CricketAPI 2 | ----------------------------------------------------------------------------------------------------------- 3 | Cricket API - Live Scores | Commentary | ScoreCard 4 | ----------------------------------------------------------------------------------------------------------- 5 | CricketAPI aims to provide free access to all cricket matches via API for all ODIs, Tests & T20s (International and IPL). 6 | ## It includes following features: 7 | - Scorecards of matches 8 | - Ball by Ball Commentary 9 | - Live Scores 10 | 11 | # Usage 12 | - getAllMatches
13 | ```http://localhost:1234/getMatches``` 14 |
15 | This endpoint will return list of all Live and Upcoming matches.
16 | 17 | - liveScore
18 | ```http://localhost:1234/livescore?matchId={matchID}``` 19 |
20 | This endpoint will return live scores of {matchID}.
21 | 22 | - scorecard
23 | ```http://localhost:1234/scorecard?matchId={matchID}``` 24 |
25 | This endpoint will return scorecard of {matchId}.
26 | 27 | 28 | - getAllMatches
29 | ```http://localhost:1234/commentary?matchId={matchID}``` 30 |
31 | This endpoint will return ball by ball commentary of {matchID}.
32 | 33 | # Running and Setup 34 | Tested on python 2.7
35 | ```pip install -r requirements.txt```
36 | ```python server.py``` 37 | # Heroku 38 | ```http://cricketapi.herokuapp.com/{endpoint}```
39 | eg. ```http://cricketapi.herokuapp.com/getMatches``` 40 | -------------------------------------------------------------------------------- /cricbuzz.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | import sys 4 | from bs4 import BeautifulSoup 5 | 6 | class Cricbuzz(): 7 | url = "http://synd.cricbuzz.com/j2me/1.0/livematches.xml" 8 | def __init__(self): 9 | pass 10 | 11 | def getxml(self,url): 12 | try: 13 | r = requests.get(url) 14 | except requests.exceptions.RequestException as e: 15 | print (e) 16 | sys.exit(1) 17 | soup = BeautifulSoup(r.text,"html.parser") 18 | return soup 19 | 20 | def matchinfo(self,match): 21 | d = {} 22 | d['id'] = match['id'] 23 | d['srs'] = match['srs'] 24 | d['mchdesc'] = match['mchdesc'] 25 | d['mnum'] = match['mnum'] 26 | d['type'] = match['type'] 27 | d['mchstate'] = match.state['mchstate'] 28 | d['status'] = match.state['status'] 29 | return d 30 | 31 | def matches(self): 32 | xml = self.getxml(self.url) 33 | matches = xml.find_all('match') 34 | info = [] 35 | 36 | for match in matches: 37 | info.append(self.matchinfo(match)) 38 | return info 39 | 40 | def livescore(self,mid): 41 | xml = self.getxml(self.url) 42 | match = xml.find(id = mid) 43 | if match is None: 44 | return "Invalid match id" 45 | if match.state['mchstate'] == 'nextlive': 46 | return "match not started yet" 47 | curl = match['datapath'] + "commentary.xml" 48 | comm = self.getxml(curl) 49 | mscr = comm.find('mscr') 50 | batting = mscr.find('bttm') 51 | bowling = mscr.find('blgtm') 52 | batsman = mscr.find_all('btsmn') 53 | bowler= mscr.find_all('blrs') 54 | data = {} 55 | d = {} 56 | data['matchinfo'] = self.matchinfo(match) 57 | d['team'] = batting['sname'] 58 | d['score'] = [] 59 | d['batsman'] = [] 60 | for player in batsman: 61 | d['batsman'].append({'name':player['sname'],'runs': player['r'],'balls':player['b'],'fours':player['frs'],'six':player['sxs']}) 62 | binngs = batting.find_all('inngs') 63 | for inng in binngs: 64 | d['score'].append({'desc':inng['desc'], 'runs': inng['r'],'wickets':inng['wkts'],'overs':inng['ovrs']}) 65 | data['batting'] = d 66 | d = {} 67 | d['team'] = bowling['sname'] 68 | d['score'] = [] 69 | d['bowler'] = [] 70 | for player in bowler: 71 | d['bowler'].append({'name':player['sname'],'overs':player['ovrs'],'maidens':player['mdns'],'runs':player['r'],'wickets':player['wkts']}) 72 | bwinngs = bowling.find_all('inngs') 73 | for inng in bwinngs: 74 | d['score'].append({'desc':inng['desc'], 'runs': inng['r'],'wickets':inng['wkts'],'overs':inng['ovrs']}) 75 | data['bowling'] = d 76 | return data 77 | 78 | def commentary(self,mid): 79 | xml = self.getxml(self.url) 80 | match = xml.find(id = mid) 81 | if match is None: 82 | return "Invalid match id" 83 | if match.state['mchstate'] == 'nextlive': 84 | return "match not started yet" 85 | curl = match['datapath'] + "commentary.xml" 86 | comm = self.getxml(curl).find_all('c') 87 | d = [] 88 | for c in comm: 89 | d.append(c.text) 90 | data = {} 91 | data['matchinfo'] = self.matchinfo(match) 92 | data['commentary'] = d 93 | return data 94 | 95 | def scorecard(self,mid): 96 | xml = self.getxml(self.url) 97 | match = xml.find(id = mid) 98 | if match is None: 99 | return "Invalid match id" 100 | if match.state['mchstate'] == 'nextlive': 101 | return "match not started yet" 102 | surl = match['datapath'] + "scorecard.xml" 103 | scard = self.getxml(surl) 104 | scrs = scard.find('scrs') 105 | innings = scrs.find_all('inngs') 106 | data = {} 107 | data['matchinfo'] = self.matchinfo(match) 108 | squads = scard.find('squads') 109 | teams = squads.find_all('team') 110 | sq = [] 111 | sqd = {} 112 | 113 | for team in teams: 114 | sqd['team'] = team['name'] 115 | sqd['members'] = [] 116 | members = team['mem'].split(", ") 117 | for mem in members: 118 | sqd['members'].append(mem) 119 | sq.append(sqd.copy()) 120 | data['squad'] = sq 121 | d = [] 122 | card = {} 123 | for inng in innings: 124 | bat = inng.find('bttm') 125 | card['batteam'] = bat['sname'] 126 | card['runs'] = inng['r'] 127 | card['wickets'] = inng['wkts'] 128 | card['overs'] = inng['noofovers'] 129 | card['runrate'] = bat['rr'] 130 | card['inngdesc'] = inng['desc'] 131 | batplayers = bat.find_all('plyr') 132 | batsman = [] 133 | bowlers = [] 134 | for player in batplayers: 135 | status = player.find('status').text 136 | batsman.append({'name':player['sname'],'runs': player['r'],'balls':player['b'],'fours':player['frs'],'six':player['six'],'dismissal':status}) 137 | card['batcard'] = batsman 138 | bowl = inng.find('bltm') 139 | card['bowlteam'] = bowl['sname'] 140 | bowlplayers = bowl.find_all('plyr') 141 | for player in bowlplayers: 142 | bowlers.append({'name':player['sname'],'overs':player['ovrs'],'maidens':player['mdns'],'runs':player['roff'],'wickets':player['wkts']}) 143 | card['bowlcard'] = bowlers 144 | d.append(card.copy()) 145 | data['scorecard'] = d 146 | return data 147 | 148 | 149 | 150 | --------------------------------------------------------------------------------