forked from elParaguayo/service.bbclivefootballscores
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.py
249 lines (179 loc) · 7.33 KB
/
settings.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
'''
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/>.
'''
''' This script is part of the BBC Football Scores service by elParaguayo
It allows users to select which leagues they wish to receive updates
for.
It is called via the script configuration screen or by passing
parameters to trigger specific functions.
The script accepts the following parameters:
toggle: Turns score notifications on and off
reset: Resets watched league data
NB only one parameter should be passed at a time.
'''
import sys
if sys.version_info >= (2, 7):
import json as json
else:
import simplejson as json
import xbmc
import xbmcgui
import xbmcaddon
from resources.lib.footballscores import getAllLeagues
_A_ = xbmcaddon.Addon("service.bbclivefootballscores")
_S_ = _A_.getSetting
# Define modes
STANDARD = 0
RESET = 1
TOGGLE_NOTIFICATIONS = 2
modes= {"standard": STANDARD,
"reset": RESET,
"toggle": TOGGLE_NOTIFICATIONS}
def localise(id):
'''Gets localised string.
Shamelessly copied from service.xbmc.versioncheck
'''
string = _A_.getLocalizedString(id).encode( 'utf-8', 'ignore' )
return string
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 selectLeagues():
'''Get list of available leagues and allow user to select those
leagues from which they want to receive updates.
'''
# Get list of leagues
# Format is [{"name": "Name of League",
# "id": "competition-xxxxxxx"}]
leagues = getMasterLeagueList()
# Set a flag to keep displaying select dialog until flag changes
finishedSelection = False
# Load the list of leagues currently selected by user
watchedleagues = loadLeagues()
# Start loop - will be exited once user confirms selection or
# cancels
while not finishedSelection:
# Empty list for select dialog
userchoice = []
# Loop through leagues
for league in leagues:
try:
# Add league details to our list
# leagues.append([league["name"],int(league["id"][12:])])
# Check whether leagues is one we're following
if int(league["id"]) in watchedleagues:
# Mark the league if it's one the user has previously
# selected and add it to the select dialog
userchoice.append("*" + league["name"])
else:
# If not previously selected, we still need to add to
# select dialog
userchoice.append(league["name"])
# Hopefully we don't end up here...
except:
# Tell the user there's a problem
userchoice.append(localise(32020))
# We only need to tell the user once!
break
# Add an option to say we've finished selecting leagues
userchoice.append(localise(32022))
# Display the list
inputchoice = xbmcgui.Dialog().select(localise(32021),
userchoice)
# Check whether the user has clicked on a league...
if (inputchoice >=0 and not userchoice[inputchoice] == localise(32022)
and not userchoice[inputchoice] == localise(32021)):
# If it's one that's already in our watched league list...
if int(leagues[inputchoice]["id"]) in watchedleagues:
# ...then we need to remove it
watchedleagues.remove(int(leagues[inputchoice]["id"]))
# if not...
else:
# ... then we need to add it
watchedleagues.append(int(leagues[inputchoice]["id"]))
# If we're done
elif userchoice[inputchoice] == localise(32022):
# Save our new list
saveLeagues(watchedleagues)
# Set the flag to leave the select dialog loop
finishedSelection = True
# If there's an error or we hit cancel
elif (inputchoice == -1 or
userchoice[inputchoice] == localise(32020)):
# end the selection (but don't save new settings)
finishedSelection = True
def getMasterLeagueList():
'''Returns master list of leagues/competitions for which we
can obtain data.
Some competitions are only visible when matches are being
played so any new competitions are added to the master list
whenever this script is run.
Returns: masterLeagueList - list of competitions in dict
format {"name": xx, "id", xx}
'''
currentleagues = getAllLeagues()
try:
masterLeagueList = json.loads(_S_("masterlist"))
except:
masterLeagueList = []
masterLeagueList += [x for x in currentleagues
if x not in masterLeagueList]
_A_.setSetting(id="masterlist",value=json.dumps(masterLeagueList))
return masterLeagueList
def loadLeagues():
'''See if there are any previously selected leagues.
Returns list of league IDs.
'''
try:
watchedleagues = json.loads(str(_S_("watchedleagues")))
except:
watchedleagues = []
return watchedleagues
def saveLeagues(leagues):
'''Converts list to JSON compatible string and saves it to our
user's settings.
'''
rawdata = json.dumps(leagues)
_A_.setSetting(id="watchedleagues",value=rawdata)
def resetLeagues():
'''Clears all saved league IDs.
Useful if IDs change leading to duplicate menu entries.
'''
_A_.setSetting(id="watchedleagues",value="[]")
_A_.setSetting(id="masterlist",value="[]")
ok = xbmcgui.Dialog().ok(localise(32023), localise(32027))
def toggleNotification():
'''Toggles score notifications on or off.'''
state = not (_S_("Alerts") == "true")
Notify("BBC Football Scores", localise(32024) % (localise(32025) if state else localise(32026)))
_A_.setSetting(id="Alerts", value=str(state).lower())
# Let's check how the user has called the script
# If an argument has bee passed to the script, is it one that the script
# is expecting?
try:
mode = modes[sys.argv[1].lower()]
except (IndexError, KeyError):
mode = STANDARD
# User wants to reset league data
if mode == RESET:
resetLeagues()
# User wants to toggle notifications
elif mode == TOGGLE_NOTIFICATIONS:
toggleNotification()
# Let's run the script to select leagues to watch
else:
selectLeagues()