Send cricket scores to mobile using python

Python is a very powerfull language which can be used for many high level tasks. In this article, I will show you basics of web scrapping using python and sending sms using twilio API. 

Note that we will be requiring requests, Beautifulsoup and twilio modules to run the script. To install them, open cmd, change the current directory  of cmd to Scripts folder inside python installation directory, and type: 1) pip install requests  2) pip install bs4  3)  pip install twilio one by one. 

You need to create an account on twilio website to use their API. After successful account creation, open your twilio dashboard. It looks like this:

screenshot-14

It shows you your ACCOUNT SID and AUTH TOKEN. Take note of them as they will be used later.

Now we create 3 python files: cricbuzzcrawler.py, details.py and main.py. Save them in same folder. Now open cricbuzzcrawler.py. This file will contain the code to scrap cricbuzz website. Type in it the following code:

import requests, bs4

res = requests.get("http://www.cricbuzz.com")
soup = bs4.BeautifulSoup(res.text, "html.parser")
element = soup.find_all('div', {'class': 'cb-col cb-col-25 cb-mtch-blk'})
country = str(input("enter a country whose score you want to know: "))
for i in range (0, 5):
if country in str(element[i].getText()):
msg = element[i].getText()
break
else:
msg="scores for the country you requested not found."

Now open details.py and type the following code:

# Edit the following details to match your twilio credentials.

sid = "SID" # twilio account SID
auth = "Auth. no" # twilio account Auth No.
frm = "number" # twilio number
sendto = "receiving number" # your receiving mobile number (if using the number for first time, add the number in verified numbers of twilio first)

In the above code, change the sid and auth values to match with your twilio account.

Now open main.py and type the following code:

import time, sys
from twilio.rest import TwilioRestClient
from details import sid, auth, sendto, frm
from cricbuzzcrawler import msg

if msg=="scores for the country you requested not found.":
print(msg)
sys.exit(2)
else:
print ("enter time period in minutes to send SMSes: ")
t= int(input())
client = TwilioRestClient(sid, auth)
while True:
client.messages.create(to=sendto, from_=frm, body=msg)
time.sleep(int(t*60))

Now you are done! Run main.py. It will prompt user to enter name of team in short form eg: WI, IND etc. Enter the team and then enter the time after which you want to periodically get scores (in minutes). Then this script will send you live cricket scores of that team periodically after the pecified time. TO stop getting scores, terminate the python script.

Leave a Reply

Your email address will not be published. Required fields are marked *