Skip to content

Commit 3d26d02

Browse files
committed
Adding xkcd scrapper and cricket notifications
1 parent 284e62e commit 3d26d02

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

python2.7/CricketNotifications.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import requests
2+
from bs4 import BeautifulSoup
3+
import time
4+
import notify2
5+
notify2.init("CricBuzz")
6+
n=notify2.Notification(None,icon="/home/rohith-gilla/icon.png")
7+
n.set_urgency(notify2.URGENCY_NORMAL)
8+
n.set_timeout(1000)
9+
url="http://synd.cricbuzz.com/j2me/1.0/livematches.xml"
10+
r=requests.get(url)
11+
soup=BeautifulSoup(r.content,'html.parser')
12+
temp=soup.find_all('match')
13+
series_names=[]
14+
datapath=[]
15+
for i in temp:
16+
i=str(i)
17+
i=i.split('datapath')[1]
18+
i=i.split('"')
19+
datapath.append(i[1])
20+
series_names.append(i[13])
21+
for i in range(len(datapath)):
22+
print str(i+1)+")"+series_names[i]
23+
ip=input("Enter the match number: ")
24+
datapath=datapath[ip-1]
25+
series_names=series_names[ip-1]
26+
com_url=datapath+"commentary.xml"
27+
old_overs="0"
28+
counter=0;
29+
loop_v=0
30+
while(loop_v==0):
31+
r=requests.get(com_url)
32+
soup=BeautifulSoup(r.content,'html.parser')
33+
try:
34+
temp=soup.find('c')
35+
comm=str(temp)
36+
comm=comm.replace('<c><![CDATA[',"")
37+
comm=comm.replace("]]></c>","")
38+
test=comm
39+
except:
40+
counter+=1
41+
pass
42+
temp=str(soup.find_all('mscr'))
43+
bat_tem=temp.split('sname="')[1].split('"')[0]
44+
runs=temp.split('r="')[3]
45+
runs=runs.split('"')[0]
46+
wickets=temp.split('wkts="')[1]
47+
wickets=wickets.split('"')[0]
48+
overs=temp.split('ovrs')
49+
overs=overs[1]
50+
overs=overs.split('"')[1]
51+
batsman=temp.split('btsmn')
52+
try:
53+
bat1=batsman[1].split('sname="')[1].split('"')[0]
54+
r1=batsman[1].split('r="')[1].split('"')[0]
55+
except:
56+
counter+=1
57+
pass
58+
try:
59+
bat2=batsman[3].split('sname="')[1].split('"')[0]
60+
r2=batsman[3].split('r="')[1].split('"')[0]
61+
except:
62+
counter+=1
63+
pass
64+
# print "____"*20
65+
n.update(bat_tem+":"+runs + "/"+wickets + " Overs : "+str(overs))
66+
if(old_overs ==overs):
67+
pass
68+
else:
69+
print "____"*20
70+
print comm
71+
print "Score :"+runs + "/"+wickets
72+
print "Overs :"+str(overs)
73+
try:
74+
print bat1+" :"+r1," "+bat2+" :"+r2
75+
n.update(bat_tem+" :"+runs + "/"+wickets + "Overs : "+str(overs),bat1+" :"+r1+" "+bat2+" :"+r2)
76+
except:
77+
pass
78+
old_overs=overs
79+
n.show()
80+
if(int(wickets)==10):
81+
loop_v=1
82+
time.sleep(15)
83+
# print datapath,series_names
84+
85+
86+
87+
88+
89+
90+
91+
92+
# print datapath
93+
# print series_names

python2.7/xkcd.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import re
2+
import requests
3+
from bs4 import BeautifulSoup
4+
import random
5+
import os
6+
l=[] #For storing random numbers
7+
def download_img(data,filename): #Function to download images
8+
if (os.path.isdir('XKCD')): #Asserts for existence
9+
pass
10+
else:
11+
os.mkdir('XKCD') #If false create a folder
12+
op_file=open('XKCD/'+filename,'wb')
13+
op_file.write(data) #Download off
14+
print "Downloaded",filename
15+
choice=input("How many random images you want to download?? xD \n")
16+
for i in range(choice):
17+
l.append(str(random.randint(1,1933))) #Last comic till date is 1933
18+
for i in l:
19+
url="https://xkcd.com/"+str(i)+"/"
20+
r=requests.get(url)
21+
soup=BeautifulSoup(r.content,'html.parser')
22+
filename=str(soup.select('#ctitle')).split('">')
23+
filename=filename[1].split('<')
24+
filename=filename[0] #Getting filename using string manip
25+
img_url=soup.select('#comic')
26+
img_url=str(img_url).split('src=')[1]
27+
img_url='https:'+img_url.split('"')[1]
28+
download_img(requests.get(img_url).content,filename+'.png') #Caling the func i times

0 commit comments

Comments
 (0)