-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.py
255 lines (184 loc) · 7.19 KB
/
default.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
'''
BBC Football Scores service for XBMC
by elParaguayo
Provides notifications for live matches from a large number of leagues.
Version: 0.1.0
'''
import os
import sys
if sys.version_info >= (2, 7):
import json as json
else:
import simplejson as json
import xbmc
import xbmcaddon
import xbmcgui
from resources.lib.footballscores import League
# Set the addon environment
_A_ = xbmcaddon.Addon()
_S_ = _A_.getSetting
pluginPath = _A_.getAddonInfo("path")
# Set some constants
# Define our images
IMG_GOAL = os.path.join(pluginPath, "resources", "images", "goal.png")
IMG_FT = os.path.join(pluginPath, "resources", "images", "whistle.png")
IMG_LATEST = os.path.join(pluginPath, "resources", "images" ,"football.png")
IMG_HT = os.path.join(pluginPath, "resources", "images", "ht.png")
IMG_FIXTURE = os.path.join(pluginPath, "resources", "images" , "fixture.png")
# STATUS_DICT object
# Format is {status: [status text, image path]}
STATUS_DICT = {"FT": ["Full Time", IMG_FT],
"HT": ["Half Time", IMG_HT],
"L": ["Latest", IMG_LATEST],
"Fixture": ["Fixture", IMG_FIXTURE]}
def localise(id):
'''Gets localised string.
Shamelessly copied from service.xbmc.versioncheck
'''
string = _A_.getLocalizedString(id).encode( 'utf-8', 'ignore' )
return string
def debug(msg):
'''Script for adding debug messages.
Takes one argument:
msg: debug message to send to XBMC log
'''
xbmc.log(u"bbclivefootballscores: {0}".format(msg),xbmc.LOGDEBUG)
def getSelectedLeagues():
'''Returns list of leagues selected by user in settings file.'''
# Try to get list of selected leagues from settings file
try:
# Get the settings value and convert to a list
watchedleagues = json.loads(str(_S_("watchedleagues")))
# if there's a problem
except:
# Create an empty list (stops service from crashing)
watchedleagues = []
# Return this list
return watchedleagues
def checkAlerts():
'''Setting is "True" when alerts are disabled.
Returns boolean:
True: Service should display alerts
False: Alerts are disabled
'''
return _S_("Alerts") != "true"
def serviceRunning():
'''User should be able to deactivate alerts (rather than deactivating
the service) via setting.
Returns a boolean as to whether we should provide alerts or not.
'''
return True
def updateWatchedLeagues(matchdict, selectedleagues):
'''Updates our active leagues to make sure that we're just looking at the
leagues that the user wants.
Takes 2 arguments:
matchdict: dictionary of leagues being watched
selectedleagues: list of league IDs chosen by user
Returns updated dictionary object.
'''
# Build a list of leagues selected by user that are not in
# our current dictionary
newleagues = [l for l in selectedleagues if l not in matchdict]
# Build a list of leagues in our dictionary that are no longer in
# list of leagues selected by users
removedleagues = [l for l in matchdict if l not in selectedleagues]
# Loop through new leagues
for l in newleagues:
# Add a League object to the dictioanary
try:
matchdict[l] = League(l)
except TypeError:
pass
# Loop through leaues to be removed
for l in removedleagues:
# Remove league from the dictionary
matchdict.pop(l)
# Return the dictionary
return matchdict
def Notify(subject, message, image=None):
'''Displays match notification.
Take 3 arguments:
subject: subject line
message: message line
image: path to icon
'''
xbmcgui.Dialog().notification(subject, message, image, 2000)
def checkMatch(match):
'''Look at the match and work out what notification we want to show.
Takes one argument:
match: footballscores.FootballMatch object
'''
# Has there been a goal?
if match.Goal:
# Gooooooooooooooooooooooooooooollllllllllllllll!
Notify("GOAL!", str(match), IMG_GOAL)
debug("GOAL: %s" % (match))
# Has the status changed? e.g. kick-off, half-time, full-time?
if match.StatusChanged:
# Get the relevant status info
info = STATUS_DICT.get(match.status, STATUS_DICT["Fixture"])
# Send the notification
Notify(info[0], str(match), info[1])
debug("STATUS: %s" % (match))
def doUpdates(matchdict):
'''Main function to updated leagues and check matches for updates.
Takes one argument:
matchdict: dictionary of leagues being watchedleagues
Returns updated dictionary
'''
ticker = u""
# Loop through each league that we're following
for league in matchdict:
# Get the league to update each match
matchdict[league].Update()
ticker += u"[B]{0}[/B]: ".format(matchdict[league].LeagueName)
ticker += u", ".join(unicode(m) for m in matchdict[league].LeagueMatches)
# Loop through the matches
for match in matchdict[league].LeagueMatches:
# and check it for updates
checkMatch(match)
debug(ticker)
xbmc.executebuiltin(u"skin.setstring(tickertext,{0})".format(ticker))
# Return the updated dicitonary object
return matchdict
# Script starts here.
# Let's get some initial data before we enter main service loop
# Build dictionary of leagues we want to follow
matchdict = updateWatchedLeagues({}, getSelectedLeagues())
debug("LeagueList - {0}".format(matchdict))
# Check if we need to show alerts or not.
alerts = checkAlerts()
# Variable for counting loop iterations
i = 0
# Main service loop - need to exit script cleanly if XBMC is shutting down
debug("Entering main loop...")
while not xbmc.abortRequested:
# 5 seconds before we do main update, let's check and see if there are any
# new leagues that we need to follow.
# Also, check whether the user has enabled/disabled alerts
if i == 11:
matchdict = updateWatchedLeagues(matchdict, getSelectedLeagues())
alerts = checkAlerts()
# If user wants alerts and we've reached ou desired loop number...
if alerts and not i:
# Update our match dictionary and check for updates.
debug("Checking scores...")
matchdict = doUpdates(matchdict)
# Sleep for 5 seconds (if this is longer, XBMC may not shut down cleanly.)
xbmc.sleep(5000)
# Increment our counter
# 12 x 5000 = 60,000 i.e. scores update every 1 minute
# Currently hard-coded - may look to change this.
i = (i + 1) % 12