IPL Match Notifications using Python
So finally after a gruesome wait, IPL is finally gonna start in few hours. In this post, we’ll see how we can create a Python script which notifies us about the time remaining in a match to start or a match is already started.
In this script, we’ll scrape espncricinfo.com to fetch match data.
Note: This code works for Mac. In order to make it work on Windows, you’ll need to modify the notify function and use win32con python package to write code for Windows notification.
Script:
def match_status():
from bs4 import BeautifulSoup
import requests
import datetime
import sys
baseurl='https://www.espncricinfo.com/'
result = requests.get(baseurl)
soup=BeautifulSoup(result.text,'lxml')
match_today=False
for link in soup.findAll('a', href=True):
if 'indian-premier-league' in str(link['href']):
matchurl=link['href']
match_today=True
break
if match_today==False:
return "No match today"
sys.exit(0)
result = requests.get(baseurl+matchurl)
soup=BeautifulSoup(result.text,'lxml')
team1=soup.findAll("div", { "class" : "team-col" })[0].text.strip()
team2=soup.findAll("div", { "class" : "team-col" })[2].text.strip()
try:
match_start_time=soup.find("div", { "class" : "status-label" }).text.strip()
match_start_time=datetime.datetime.strptime(match_start_time.replace(',',''), '%d-%b-%Y %I:%M %p')
time_remaining=match_start_time-datetime.datetime.utcnow()
return "Match between "+team1+" and "+team2+" will start in "+str(time_remaining)+" hours"
except:
return "Match between "+team1+" and "+team2+" is in progress"
def notify(title, text):
import os
os.system("""osascript -e 'display notification "{}" with title "{}"'""".format(text, title))
def main():
status=match_status()
notify("IPL match notification", status)
if __name__ == '__main__':
main()
Save it as ipl_match_notification.py. Test it by executing python3 ipl_match_notification.py and you’ll see the notification about MI vs CSK match.
Now you can set up a cron for this and never miss a match because of work :)