
Cricket Score Notifier
- 1 minBring a regular follower of cricket, I was lazy to open espncricinfo to check the scores everytime. Then I got an Idea to write a script to notify me with present cricket match scores every 5 minutes. Without any delay I wrote the below script.
I used pynotify to create popup for score and json from espncricinfo to extract the score
Code :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import pynotify | |
import re | |
from time import sleep | |
import json | |
#Fuction to show notification using pynotify in Ubuntu | |
def popup(title, message): | |
pynotify.init("Test") | |
pop = pynotify.Notification(title, message) | |
pop.show() | |
return | |
def getscore(): | |
#change the below url according to the match | |
url = "http://www.espncricinfo.com/india-v-sri-lanka-2015-16/engine/match/963701.json" | |
r = requests.get(url) | |
while r.status_code is not 200: | |
r = requests.get(url) | |
data = json.loads(r.text) | |
desc = data['description'].strip() | |
player_status = data['match']['current_summary'].strip() | |
team1_name = data['other_scores']['international'][2]['team1_name'].strip() | |
team1_score = data['innings'][0]['runs'].strip() | |
team1_wick = data['innings'][0]['wickets'].strip() | |
team1_ove = data['innings'][0]['overs'].strip() | |
team2_name = data['other_scores']['international'][2]['team2_name'].strip() | |
team2_score = data['innings'][1]['runs'].strip() | |
team2_wick = data['innings'][1]['wickets'].strip() | |
team2_ove = data['innings'][1]['overs'].strip() | |
recent_runs = '' | |
for recent_over in data['live']['recent_overs']: | |
for run in recent_over: | |
if run['ball'] == '•': | |
run['ball'] = 0 | |
recent_runs = recent_runs + str(run['ball']) + ' ' | |
recent_runs = recent_runs + ' | ' | |
score = str(desc) + '\n\n' + str(team1_name) + ' : ' + str(team1_score) + '/' + str(team1_wick) + ' ' + str(team1_ove) + '' + '\n\n' + str(team2_name) + ' : ' + str(team2_score) + '/' + str(team2_wick) + ' ' + str(team2_ove) | |
player_status = re.sub(r'.*ov,','', str(player_status)) | |
score = score + '\n\nPlayer status: ' + player_status | |
score = score + '\n\nRecent Overs: ' + recent_runs | |
popup("Score Board:-", score) | |
print score | |
#Fetch the score every 30 secs | |
sleep(30) | |
if __name__ == "__main__": | |
while True: | |
getscore() |
Please comment your views/suggestions below.