-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadvancedScript.py
More file actions
103 lines (84 loc) · 3.62 KB
/
advancedScript.py
File metadata and controls
103 lines (84 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import print_function
from sys import version_info
import requests
import json
import re
from bs4 import BeautifulSoup
if version_info.major is 2:
import pynotify
notifyModule = "pynotify"
if version_info.major is 3:
try:
from gi.Repository import Notify
notifyModule = "Notify"
except ImportError:
import notify2
notifyModule = "notify2"
from time import sleep
import logging
logging.basicConfig(level=logging.DEBUG)
def popUpMessage(title, message):
if notifyModule is "pynotify":
logging.debug("Initializing pynotify")
pynotify.init("Scorer")
logging.debug("Sending notification: title:{}, message:{}".format(title,message))
pynotify.Notification(title, message, "dialog-information").show()
elif notifyModule is "Notify":
logging.debug("Initializing Notify")
Notify.init("Scorer")
logging.debug("Sending notification: title:{}, message:{}".format(title,message))
Notify.Notification.new(title, message, "dialog-information").show()
else:
logging.debug("Initializing notify2")
notify2.init("Scorer")
logging.debug("Sending notification: title:{}, message:{}".format(title,message))
notify2.Notification(title, message, "dialog-information").show()
def getLastestScore(parsingJson):
firstTeamName = parsingJson['other_scores']['international'][0]['team1_name'].strip()
firstTeamScore = parsingJson['other_scores']['international'][0]['team1_desc'].replace(' ov',' overs').strip()
secondTeamName = parsingJson['other_scores']['international'][0]['team2_name'].strip()
secondTeamScore = parsingJson['other_scores']['international'][0]['team2_desc'].replace(' ov',' overs').strip()
matchSummary = parsingJson['match']['current_summary'].strip()
latestScore = str(firstTeamName) + ' - ' + str(firstTeamScore) + '\n' + str(secondTeamName) + ' - ' + str(secondTeamScore)
onCrease = re.sub(r'.*ov,','', str(matchSummary))
toDisplay = latestScore + '\n'+ 'On the crease -> '+ onCrease
logging.info("Score found is {}".format(toDisplay))
return toDisplay
liveXMLUrl = "http://static.cricinfo.com/rss/livescores.xml"
liveJSONUrl = "http://www.espncricinfo.com/netstorage/656493.json"
matchChoice = 0
score = ""
didInterrupt = False
print("Fetching matches..")
while True:
try:
logging.info("Sending requests")
dataFromXMLUrl = requests.get(liveXMLUrl)
dataFromJSONUrl = requests.get(liveJSONUrl)
while dataFromXMLUrl.status_code is not 200:
logging.debug("Request failed: trying again")
sleep(2)
dataFromXMLUrl = requests.get(liveXMLUrl)
data = BeautifulSoup(dataFromXMLUrl.text).find_all("description")
parsingJson = json.loads(dataFromJSONUrl.text)
if not matchChoice:
print("Matches available:")
for index, game in enumerate(data[1:], 1):
print(index, ".", str(game.text))
matchChoice = int(input("Enter your choice: "))
while True:
if matchChoice in range(1, index):
break
matchChoice = int(input("Invalid Choice. Enter your choice: "))
didInterrupt=False
updatedScore = getLastestScore(parsingJson)
popUpMessage("Score", updatedScore)
sleep(15)
except KeyboardInterrupt:
if didInterrupt:
logging.info("keyboard interrupted, once")
print("Bye bye")
break
else:
print("Press Ctrl+C again to quit")
matchChoice, didInterrupt = 0, True