Cricket score in command line
using python and open cricscore api
Intro
Earlier I developed a simple cricket score api known as cricscore-api which gets data from cricinfo website.
It was developed to support a simple Android App back then.
Later it was open sourced for everyone.
API
Cricscore API has 2 simple APIs.
-
List all matches
As shown below, it returns list of matches, along with matchid
.$ curl "http://cricscore-api.appspot.com/csa" # line breaks added to the output for readability. [ {"id":895819,"t2":"India","t1":"Australia"}, {"id":959389,"t2":"Pakistan A","t1":"England Lions"} ]
-
Get detail for specific match.
Take the id of the match of interest and use it as query parameter to same end point.$ curl "http://cricscore-api.appspot.com/csa?id=895819" # line breaks added to the output for readability. [ { "de":"2nd T20I: Australia v India at Melbourne, Jan 29, 2016", "id":895819, "si":"Australia v India" } ]
Goals
- Show the score of a particular match in the console.
- Output new score detail, whenever it changes.
Code
Self explanatory for python people.
In line comments (starting with #
) for everyone else.
import urllib2, json, time
# Score set to empty initially.
score = ''
# Run forever
while True:
# Fetch the url for the specific match.
response = urllib2.urlopen('http://cricscore-api.appspot.com/csa?id=656493')
# Get the score details `de` from the response json.
data = json.load(response)
new_score = data[0]['de']
# Whenever score changes, print to console.
if(new_score != score):
print new_score
score = new_score
# Give a break, hit API after 2 seconds.
time.sleep(2)
Run it
- Store the above code in file
score.py
with required match id. - Run
python score.py
Ctrl
+C
to exit the script.
Further
This code can be further developed, to select the match in the command line itself.
You are free to do so, I may not find time.
Later
Later, I will add output for a live match in Run it section.