forked from enen92/script.screensaver.cocktail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.py
444 lines (409 loc) · 17.7 KB
/
interface.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# -*- coding: utf-8 -*-
'''
script.screensaver.cocktail - A random cocktail recipe screensaver for kodi
Copyright (C) 2015 enen92,Zag
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/>.
'''
import xbmcaddon
import xbmcgui
import xbmc
import sys
import os
import urllib
import cocktail as cocktailscreensaver
from resources.lib import thecocktaildb
from resources.lib import ingredient_details
from resources.lib import youtube
from resources.lib import favourites
from resources.lib.common_cocktail import *
contextmenu_labels_drink_original = [translate(32001),translate(32002)]
contextmenu_options_drink_original = ['recipe','youtube']
INGREDIENT_DRINK_PANEL_CONTROL = 32501
REGULAR_PANEL_CONTROL = 32500
BACK_BACKGROUND_CONTROL = 32502
BACK_ICON_CONTROL = 32503
class Main(xbmcgui.WindowXML):
def __init__( self, *args, **kwargs ):
self.status = None
self.ingredient = None
self.category = None
self.glass = None
self.alcohol = None
def onInit(self):
if self.status == None:
self.last_focused_mainmenu_item = 0
self.last_focused_glass_item = 0
self.last_focused_category_item = 0
self.last_focused_alchool_item = 0
self.main_menu()
#Enable back button for touch devices
if addon.getSetting('enable-back') == "false":
self.getControl(BACK_BACKGROUND_CONTROL).setVisible(False)
self.getControl(BACK_ICON_CONTROL).setVisible(False)
def main_menu(self):
self.status = 'main_menu'
self.last_focused_drink = 0
self.last_focused_ingredient = 0
items = []
menu_items = [(translate(32003),'categories',os.path.join(addon_path,"resources","skins","default","media","menuicons","categories.png")),(translate(32004),'glass',os.path.join(addon_path,"resources","skins","default","media","menuicons","glass.png")),(translate(32005),'alcohol',os.path.join(addon_path,"resources","skins","default","media","menuicons","alcohol.png")),(translate(32006),'ingredient',os.path.join(addon_path,"resources","skins","default","media","menuicons","ingredient.png")),(translate(32007),'search',os.path.join(addon_path,"resources","skins","default","media","menuicons","search.png")),(translate(32025),'favourites',os.path.join(addon_path,"resources","skins","default","media","menuicons","favourites.png")),(translate(32008),'screensaver',os.path.join(addon_path,"resources","skins","default","media","menuicons","screensaver.png"))]
for label,identifier,icon in menu_items:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': icon })
item.setProperty('category',identifier)
items.append(item)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(items)
self.setFocusId(REGULAR_PANEL_CONTROL)
self.getControl(REGULAR_PANEL_CONTROL).selectItem(self.last_focused_mainmenu_item)
return
def alcoholic_type(self):
self.status = 'alcoholic_selection'
self.last_focused_drink = 0
self.last_focused_ingredient = 0
items = []
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
categories = cocktailsdb_api.List().alcoholic()
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
for label in categories:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': os.path.join(addon_path,"resources","skins","default","media","menuicons",urllib.quote(label).lower()+".png") })
item.setProperty('category','alcoholic_selection')
items.append(item)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(items)
self.setFocusId(REGULAR_PANEL_CONTROL)
self.getControl(REGULAR_PANEL_CONTROL).selectItem(self.last_focused_alchool_item)
return
def glass_type(self):
self.status = 'glass_selection'
self.last_focused_drink = 0
self.last_focused_ingredient = 0
items = []
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
categories = cocktailsdb_api.List().glass()
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
for label in categories:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': os.path.join(addon_path,"resources","skins","default","media","glasses",urllib.quote(label).lower().replace('/','-')+".png") })
item.setProperty('category','glass_selection')
items.append(item)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(items)
self.setFocusId(REGULAR_PANEL_CONTROL)
self.getControl(REGULAR_PANEL_CONTROL).selectItem(self.last_focused_glass_item)
return
def categories(self):
self.status = 'category_selection'
self.last_focused_drink = 0
self.last_focused_ingredient = 0
items = []
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
categories = cocktailsdb_api.List().category()
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
for label in categories:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': os.path.join(addon_path,"resources","skins","default","media","category",urllib.quote(label).lower().replace('/','-')+".png") })
item.setProperty('category','category_selection')
items.append(item)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(items)
self.setFocusId(REGULAR_PANEL_CONTROL)
self.getControl(REGULAR_PANEL_CONTROL).selectItem(self.last_focused_category_item)
return
def ingredient_picker(self):
self.status = 'ingredient_selection'
self.last_focused_drink = 0
items = []
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
categories = cocktailsdb_api.List().ingredient()
for label in categories:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': thecocktaildb.API_INGREDIENT_URL + urllib.quote(removeNonAscii(label)) +'.png' })
item.setProperty('category','ingredient_picker')
item.setProperty('ingredient_thumb',thecocktaildb.API_INGREDIENT_URL + urllib.quote(removeNonAscii(label)) +'.png' )
item.setProperty('id','None')
items.append(item)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).addItems(items)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
self.setFocusId(INGREDIENT_DRINK_PANEL_CONTROL)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).selectItem(self.last_focused_ingredient)
return
def search(self):
keyb = xbmc.Keyboard('', translate(32009))
keyb.doModal()
if (keyb.isConfirmed()):
search_parameter = urllib.quote_plus(keyb.getText())
if not search_parameter: xbmcgui.Dialog().ok(translate(32000),translate(32010))
else:
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
cocktails_list = cocktailsdb_api.Search().cocktail(search_parameter)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
if not cocktails_list: xbmcgui.Dialog().ok(translate(32000),translate(32011))
else:
self.list_cocktails(cocktails_list)
def reset_variables(self):
self.ingredient = None
self.category = None
self.glass = None
self.alcohol = None
return
def list_favourites(self):
has_favourites = favourites.has_favourites()
if has_favourites:
favourite_cocktails = favourites.get_favourites()
self.list_cocktails(favourite_cocktails)
return
def list_cocktails(self,cocktails_list):
self.status = 'cocktail_listing'
if not cocktails_list:
xbmcgui.Dialog().ok(translate(32000),translate(32012))
else:
items = []
for cocktail in cocktails_list:
item = xbmcgui.ListItem(cocktail.name)
item.setArt({ 'thumb': cocktail.thumb })
item.setProperty('drink_thumb',cocktail.thumb)
item.setProperty('id',str(cocktail.id))
item.setProperty('category','cocktail_listing')
items.append(item)
self.cocktail_items = items
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).addItems(items)
self.setFocusId(INGREDIENT_DRINK_PANEL_CONTROL)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).selectItem(0)
return
def set_youtube_videos(self,video_list):
items = []
self.status = 'video_listing'
for label,thumb,video_id in video_list:
item = xbmcgui.ListItem(label)
item.setArt({ 'thumb': thumb })
item.setProperty('video_id',video_id)
item.setProperty('category','video_listing')
items.append(item)
self.youtube_videos = items
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(items)
return
def cocktail_player(self,cocktail_id):
screensaver = cocktailscreensaver.Screensaver(
'script-cocktail-Cocktailplayer.xml',
addon_path,
'default',
cocktail_id,
)
screensaver.doModal()
del screensaver
def set_ingredient_description(self,ingredient_name,ingredient_thumb,ingredient_description):
ingredient_details.start(ingredient_name,ingredient_thumb,ingredient_description)
return
def onAction(self,action):
if action.getId() == ACTION_RETURN or action.getId() == ACTION_ESCAPE:
if self.status == 'main_menu': self.close()
elif 'selection' in self.status: self.main_menu()
elif self.status == 'video_listing':
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).addItems(self.cocktail_items)
self.setFocusId(INGREDIENT_DRINK_PANEL_CONTROL)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).selectItem(self.last_focused_drink)
self.status = 'cocktail_listing'
else:
if self.category:
self.reset_variables()
self.categories()
elif self.glass:
self.reset_variables()
self.glass_type()
elif self.alcohol:
self.reset_variables()
self.alcoholic_type()
elif self.ingredient:
self.reset_variables()
self.ingredient_picker()
else:
self.main_menu()
elif action.getId() == ACTION_CONTEXT_MENU:
#restart contextmenu
self.contextmenu_labels_drink = []
for item in contextmenu_labels_drink_original:
self.contextmenu_labels_drink.append(item)
self.contextmenu_options_drink = []
for item in contextmenu_options_drink_original:
self.contextmenu_options_drink.append(item)
if xbmc.getCondVisibility("Control.HasFocus("+str(INGREDIENT_DRINK_PANEL_CONTROL)+")"):
control = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getSelectedItem()
self.last_focused_drink = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getSelectedPosition()
control_label = control.getLabel()
control_thumb = control.getProperty('drink_thumb')
control_drink_id = control.getProperty('id')
if control.getProperty('id') != 'None':
if favourites.is_favourite(control_drink_id):
self.contextmenu_labels_drink.append(translate(32028))
self.contextmenu_options_drink.append('removefavourite')
else:
self.contextmenu_labels_drink.append(translate(32027))
self.contextmenu_options_drink.append('addfavourite')
choose = xbmcgui.Dialog().select(translate(32000),self.contextmenu_labels_drink)
if choose > - 1:
if self.contextmenu_options_drink[choose] == 'youtube':
video_list = youtube.return_youtubevideos(control_label + ' drink')
if not video_list: xbmcgui.Dialog().ok(translate(32000),translate(32013))
else:
self.set_youtube_videos(video_list)
elif self.contextmenu_options_drink[choose] == 'recipe':
cocktail_id = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getSelectedItem().getProperty('id')
self.cocktail_player(cocktail_id)
return
elif self.contextmenu_options_drink[choose] == 'addfavourite':
favourites.add_to_favourite_drinks(control_label,control_drink_id,control_thumb)
return
elif self.contextmenu_options_drink[choose] == 'removefavourite':
favourites.remove_from_favourites(control_drink_id)
items = []
size = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).size()
if size > 0:
for i in xrange(0,size):
items.append(self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getListItem(i))
if items:
refresh = True
for drink in items:
if not favourites.is_favourite(drink.getProperty('id')):
if drink.getProperty('id') != control_drink_id:
refresh = False
break
if refresh:
self.list_favourites()
else:
#If here...we are in ingredient picker
ingredient_name = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getSelectedItem().getLabel()
ingredient_thumb = self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).getSelectedItem().getProperty('ingredient_thumb')
#TODO get ingredient description when available
ingredient_description = translate(32029)
self.set_ingredient_description(ingredient_name,ingredient_thumb,ingredient_description)
return
def onClick(self,controlId):
if controlId == REGULAR_PANEL_CONTROL:
identifier = self.getControl(controlId).getSelectedItem().getProperty('category')
self.focused_item = self.getControl(controlId).getSelectedPosition()
if identifier == 'screensaver':
self.reset_variables()
xbmc.executescript(os.path.join(addon_path,'cocktail.py'))
elif identifier == 'search':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.search()
elif identifier == 'alcohol':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.alcoholic_type()
elif identifier == 'glass':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.glass_type()
elif identifier == 'categories':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.categories()
elif identifier == 'ingredient':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.ingredient_picker()
elif identifier == 'favourites':
self.last_focused_mainmenu_item = self.focused_item
self.reset_variables()
self.list_favourites()
elif identifier == 'category_selection':
category = self.getControl(controlId).getSelectedItem().getLabel()
self.ingredient = None
self.last_focused_category_item = self.focused_item
self.category = category
self.glass = None
self.alcohol = None
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
cocktails = cocktailsdb_api.Filter().category(category)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
self.list_cocktails(cocktails)
elif identifier == 'alcoholic_selection':
tipo = self.getControl(controlId).getSelectedItem().getLabel()
self.ingredient = None
self.last_focused_alchool_item = self.focused_item
self.category = None
self.glass = None
self.alcohol = tipo
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
cocktails = cocktailsdb_api.Filter().alcohol(tipo)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
self.list_cocktails(cocktails)
elif identifier == 'glass_selection':
glass = self.getControl(controlId).getSelectedItem().getLabel()
self.last_focused_glass_item = self.focused_item
self.ingredient = None
self.category = None
self.glass = glass
self.alcohol = None
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
cocktails = cocktailsdb_api.Filter().glass(glass)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
self.list_cocktails(cocktails)
elif identifier == 'video_listing':
youtube_id = self.getControl(controlId).getSelectedItem().getProperty('video_id')
player = xbmc.Player()
player.play('plugin://plugin.video.youtube/play/?video_id='+youtube_id)
while player.isPlaying():
xbmc.sleep(200)
xbmc.sleep(500)
self.getControl(INGREDIENT_DRINK_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).reset()
self.getControl(REGULAR_PANEL_CONTROL).addItems(self.youtube_videos)
self.setFocusId(REGULAR_PANEL_CONTROL)
self.getControl(REGULAR_PANEL_CONTROL).selectItem(self.focused_item)
if controlId == INGREDIENT_DRINK_PANEL_CONTROL:
identifier = self.getControl(controlId).getSelectedItem().getProperty('category')
if identifier == 'ingredient_picker':
ingredient = self.getControl(controlId).getSelectedItem().getLabel()
self.ingredient = ingredient
self.last_focused_drink = 0
self.last_focused_ingredient = self.getControl(controlId).getSelectedPosition()
self.category = None
self.glass = None
self.alcohol = None
xbmc.executebuiltin( "ActivateWindow(busydialog)" )
cocktails = cocktailsdb_api.Filter().ingredient(ingredient)
xbmc.executebuiltin( "Dialog.Close(busydialog)" )
self.list_cocktails(cocktails)
elif identifier == 'cocktail_listing':
cocktail_id = self.getControl(controlId).getSelectedItem().getProperty('id')
self.cocktail_player(cocktail_id)
if __name__ == '__main__':
if len(sys.argv) <= 1:
#Start interface
main = Main(
'script-cocktail-Main.xml',
addon_path,
'default',
'',
)
main.doModal()
del main
sys.modules.clear()
else:
#Start screensaver
xbmc.executescript(os.path.join(addon_path,'cocktail.py'))