fix(ui): Masks password input, renders empty list if no search or seasons results...
[plugin.video.netflix.git] / resources / lib / KodiHelper.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # Module: KodiHelper
4 # Created on: 13.01.2017
5
6 import os
7 import urllib
8 import xbmcplugin
9 import xbmcgui
10 import xbmcaddon
11 import xbmc
12 import json
13 try:
14    import cPickle as pickle
15 except:
16    import pickle
17
18 class KodiHelper:
19     """Consumes all the configuration data from Kodi as well as turns data into lists of folders and videos"""
20
21     def __init__ (self, plugin_handle, base_url):
22         """Fetches all needed info from Kodi & configures the baseline of the plugin
23
24         Parameters
25         ----------
26         plugin_handle : :obj:`int`
27             Plugin handle
28
29         base_url : :obj:`str`
30             Plugin base url
31         """
32         self.plugin_handle = plugin_handle
33         self.base_url = base_url
34         self.addon = xbmcaddon.Addon()
35         self.plugin = self.addon.getAddonInfo('name')
36         self.base_data_path = xbmc.translatePath(self.addon.getAddonInfo('profile'))
37         self.home_path = xbmc.translatePath('special://home')
38         self.plugin_path = self.addon.getAddonInfo('path')
39         self.cookie_path = self.base_data_path + 'COOKIE'
40         self.data_path = self.base_data_path + 'DATA'
41         self.config_path = os.path.join(self.base_data_path, 'config')
42         self.msl_data_path = xbmc.translatePath('special://profile/addon_data/service.msl').decode('utf-8') + '/'
43         self.verb_log = self.addon.getSetting('logging') == 'true'
44         self.default_fanart = self.addon.getAddonInfo('fanart')
45         self.win = xbmcgui.Window(xbmcgui.getCurrentWindowId())
46         self.library = None
47         self.setup_memcache()
48
49     def refresh (self):
50         """Refrsh the current list"""
51         return xbmc.executebuiltin('Container.Refresh')
52
53     def show_rating_dialog (self):
54         """Asks the user for a movie rating
55
56         Returns
57         -------
58         :obj:`int`
59             Movie rating between 0 & 10
60         """
61         dlg = xbmcgui.Dialog()
62         return dlg.numeric(heading=self.get_local_string(string_id=30019) + ' ' + self.get_local_string(string_id=30022), type=0)
63
64     def show_search_term_dialog (self):
65         """Asks the user for a term to query the netflix search for
66
67         Returns
68         -------
69         :obj:`str`
70             Term to search for
71         """
72         dlg = xbmcgui.Dialog()
73         term = dlg.input(self.get_local_string(string_id=30003), type=xbmcgui.INPUT_ALPHANUM)
74         if len(term) == 0:
75             term = ' '
76         return term
77
78     def show_add_to_library_title_dialog (self, original_title):
79         """Asks the user for an alternative title for the show/movie that gets exported to the local library
80
81         Parameters
82         ----------
83         original_title : :obj:`str`
84             Original title of the show (as suggested by the addon)
85
86         Returns
87         -------
88         :obj:`str`
89             Title to persist
90         """
91         dlg = xbmcgui.Dialog()
92         return dlg.input(heading=self.get_local_string(string_id=30031), defaultt=original_title, type=xbmcgui.INPUT_ALPHANUM)
93
94     def show_password_dialog (self):
95         """Asks the user for its Netflix password
96
97         Returns
98         -------
99         :obj:`str`
100             Netflix password
101         """
102         dlg = xbmcgui.Dialog()
103         return dlg.input(self.get_local_string(string_id=30004), type=xbmcgui.INPUT_ALPHANUM, option=xbmcgui.ALPHANUM_HIDE_INPUT)
104
105     def show_email_dialog (self):
106         """Asks the user for its Netflix account email
107
108         Returns
109         -------
110         term : :obj:`str`
111             Netflix account email
112         """
113         dlg = xbmcgui.Dialog()
114         return dlg.input(self.get_local_string(string_id=30005), type=xbmcgui.INPUT_ALPHANUM)
115
116     def show_login_failed_notification (self):
117         """Shows notification that the login failed
118
119         Returns
120         -------
121         bool
122             Dialog shown
123         """
124         dialog = xbmcgui.Dialog()
125         dialog.notification(self.get_local_string(string_id=30008), self.get_local_string(string_id=30009), xbmcgui.NOTIFICATION_ERROR, 5000)
126         return True
127
128     def show_missing_inputstream_addon_notification (self):
129         """Shows notification that the inputstream addon couldn't be found
130
131         Returns
132         -------
133         bool
134             Dialog shown
135         """
136         dialog = xbmcgui.Dialog()
137         dialog.notification(self.get_local_string(string_id=30028), self.get_local_string(string_id=30029), xbmcgui.NOTIFICATION_ERROR, 5000)
138         return True
139
140     def show_no_search_results_notification (self):
141         """Shows notification that no search results could be found
142
143         Returns
144         -------
145         bool
146             Dialog shown
147         """
148         dialog = xbmcgui.Dialog()
149         dialog.notification(self.get_local_string(string_id=30011), self.get_local_string(string_id=30013))
150         return True
151
152     def show_no_seasons_notification (self):
153         """Shows notification that no seasons be found
154
155         Returns
156         -------
157         bool
158             Dialog shown
159         """
160         dialog = xbmcgui.Dialog()
161         dialog.notification(self.get_local_string(string_id=30010), self.get_local_string(string_id=30012))
162         return True
163
164     def set_setting (self, key, value):
165         """Public interface for the addons setSetting method
166
167         Returns
168         -------
169         bool
170             Setting could be set or not
171         """
172         return self.addon.setSetting(key, value)
173
174     def get_credentials (self):
175         """Returns the users stored credentials
176
177         Returns
178         -------
179         :obj:`dict` of :obj:`str`
180             The users stored account data
181         """
182         return {
183             'email': self.addon.getSetting('email'),
184             'password': self.addon.getSetting('password')
185         }
186
187     def get_custom_library_settings (self):
188         """Returns the settings in regards to the custom library folder(s)
189
190         Returns
191         -------
192         :obj:`dict` of :obj:`str`
193             The users library settings
194         """
195         return {
196             'enablelibraryfolder': self.addon.getSetting('enablelibraryfolder'),
197             'customlibraryfolder': self.addon.getSetting('customlibraryfolder')
198         }
199
200     def get_ssl_verification_setting (self):
201         """Returns the setting that describes if we should verify the ssl transport when loading data
202
203         Returns
204         -------
205         bool
206             Verify or not
207         """
208         return self.addon.getSetting('ssl_verification') == 'true'
209
210     def set_main_menu_selection (self, type):
211         """Persist the chosen main menu entry in memory
212
213         Parameters
214         ----------
215         type : :obj:`str`
216             Selected menu item
217         """
218         self.win.setProperty('main_menu_selection', type)
219
220     def get_main_menu_selection (self):
221         """Gets the persisted chosen main menu entry from memory
222
223         Returns
224         -------
225         :obj:`str`
226             The last chosen main menu entry
227         """
228         return self.win.getProperty('main_menu_selection')
229
230     def setup_memcache (self):
231         """Sets up the memory cache if not existant"""
232         cached_items = self.win.getProperty('memcache')
233         # no cache setup yet, create one
234         if len(cached_items) < 1:
235             self.win.setProperty('memcache', pickle.dumps({}))
236
237     def invalidate_memcache (self):
238         """Invalidates the memory cache"""
239         self.win.setProperty('memcache', pickle.dumps({}))
240
241     def has_cached_item (self, cache_id):
242         """Checks if the requested item is in memory cache
243
244         Parameters
245         ----------
246         cache_id : :obj:`str`
247             ID of the cache entry
248
249         Returns
250         -------
251         bool
252             Item is cached
253         """
254         cached_items = pickle.loads(self.win.getProperty('memcache'))
255         return cache_id in cached_items.keys()
256
257     def get_cached_item (self, cache_id):
258         """Returns an item from the in memory cache
259
260         Parameters
261         ----------
262         cache_id : :obj:`str`
263             ID of the cache entry
264
265         Returns
266         -------
267         mixed
268             Contents of the requested cache item or none
269         """
270         cached_items = pickle.loads(self.win.getProperty('memcache'))
271         if self.has_cached_item(cache_id) != True:
272             return None
273         return cached_items[cache_id]
274
275     def add_cached_item (self, cache_id, contents):
276         """Adds an item to the in memory cache
277
278         Parameters
279         ----------
280         cache_id : :obj:`str`
281             ID of the cache entry
282
283         contents : mixed
284             Cache entry contents
285         """
286         cached_items = pickle.loads(self.win.getProperty('memcache'))
287         cached_items.update({cache_id: contents})
288         self.win.setProperty('memcache', pickle.dumps(cached_items))
289
290     def build_profiles_listing (self, profiles, action, build_url):
291         """Builds the profiles list Kodi screen
292
293         Parameters
294         ----------
295         profiles : :obj:`dict` of :obj:`str`
296             List of user profiles
297
298         action : :obj:`str`
299             Action paramter to build the subsequent routes
300
301         build_url : :obj:`fn`
302             Function to build the subsequent routes
303
304         Returns
305         -------
306         bool
307             List could be build
308         """
309         for profile_id in profiles:
310             profile = profiles[profile_id]
311             url = build_url({'action': action, 'profile_id': profile_id})
312             li = xbmcgui.ListItem(label=profile['profileName'], iconImage=profile['avatar'])
313             li.setProperty('fanart_image', self.default_fanart)
314             xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=True)
315             xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
316         xbmcplugin.endOfDirectory(self.plugin_handle)
317         return True
318
319     def build_main_menu_listing (self, video_list_ids, user_list_order, actions, build_url):
320         """Builds the video lists (my list, continue watching, etc.) Kodi screen
321
322         Parameters
323         ----------
324         video_list_ids : :obj:`dict` of :obj:`str`
325             List of video lists
326
327         user_list_order : :obj:`list` of :obj:`str`
328             Ordered user lists, to determine what should be displayed in the main menue
329
330         actions : :obj:`dict` of :obj:`str`
331             Dictionary of actions to build subsequent routes
332
333         build_url : :obj:`fn`
334             Function to build the subsequent routes
335
336         Returns
337         -------
338         bool
339             List could be build
340         """
341         preselect_items = []
342         for category in user_list_order:
343             for video_list_id in video_list_ids['user']:
344                 if video_list_ids['user'][video_list_id]['name'] == category:
345                     label = video_list_ids['user'][video_list_id]['displayName']
346                     if category == 'netflixOriginals':
347                         label = label.capitalize()
348                     li = xbmcgui.ListItem(label=label)
349                     li.setProperty('fanart_image', self.default_fanart)
350                     # determine action route
351                     action = actions['default']
352                     if category in actions.keys():
353                         action = actions[category]
354                     # determine if the item should be selected
355                     preselect_items.append((False, True)[category == self.get_main_menu_selection()])
356                     url = build_url({'action': action, 'video_list_id': video_list_id, 'type': category})
357                     xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=True)
358
359         # add recommendations/genres as subfolders (save us some space on the home page)
360         i18n_ids = {
361             'recommendations': self.get_local_string(30001),
362             'genres': self.get_local_string(30010)
363         }
364         for type in i18n_ids.keys():
365             # determine if the lists have contents
366             if len(video_list_ids[type]) > 0:
367                 # determine action route
368                 action = actions['default']
369                 if type in actions.keys():
370                     action = actions[type]
371                 # determine if the item should be selected
372                 preselect_items.append((False, True)[type == self.get_main_menu_selection()])
373                 li_rec = xbmcgui.ListItem(label=i18n_ids[type])
374                 li_rec.setProperty('fanart_image', self.default_fanart)
375                 url_rec = build_url({'action': action, 'type': type})
376                 xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url_rec, listitem=li_rec, isFolder=True)
377
378         # add search as subfolder
379         action = actions['default']
380         if 'search' in actions.keys():
381             action = actions[type]
382         li_rec = xbmcgui.ListItem(label=self.get_local_string(30011))
383         li_rec.setProperty('fanart_image', self.default_fanart)
384         url_rec = build_url({'action': action, 'type': 'search'})
385         xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url_rec, listitem=li_rec, isFolder=True)
386
387         # no srting & close
388         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_UNSORTED)
389         xbmcplugin.endOfDirectory(self.plugin_handle)
390
391         # (re)select the previously selected main menu entry
392         idx = 1
393         for item in preselect_items:
394             idx += 1
395             preselected_list_item = idx if item else None
396         preselected_list_item = idx + 1 if self.get_main_menu_selection() == 'search' else preselected_list_item
397         if preselected_list_item != None:
398             xbmc.executebuiltin('ActivateWindowAndFocus(%s, %s)' % (str(self.win.getFocusId()), str(preselected_list_item)))
399         return True
400
401     def build_video_listing (self, video_list, actions, type, build_url):
402         """Builds the video lists (my list, continue watching, etc.) contents Kodi screen
403
404         Parameters
405         ----------
406         video_list_ids : :obj:`dict` of :obj:`str`
407             List of video lists
408
409         actions : :obj:`dict` of :obj:`str`
410             Dictionary of actions to build subsequent routes
411
412         type : :obj:`str`
413             None or 'queue' f.e. when it´s a special video lists
414
415         build_url : :obj:`fn`
416             Function to build the subsequent routes
417
418         Returns
419         -------
420         bool
421             List could be build
422         """
423         for video_list_id in video_list:
424             video = video_list[video_list_id]
425             if type != 'queue' or (type == 'queue' and video['in_my_list'] == True):
426                 li = xbmcgui.ListItem(label=video['title'])
427                 # add some art to the item
428                 li = self._generate_art_info(entry=video, li=li)
429                 # it´s a show, so we need a subfolder & route (for seasons)
430                 isFolder = True
431                 url = build_url({'action': actions[video['type']], 'show_id': video_list_id})
432                 # lists can be mixed with shows & movies, therefor we need to check if its a movie, so play it right away
433                 if video_list[video_list_id]['type'] == 'movie':
434                     # it´s a movie, so we need no subfolder & a route to play it
435                     isFolder = False
436                     url = build_url({'action': 'play_video', 'video_id': video_list_id})
437                 # add list item info
438                 li = self._generate_entry_info(entry=video, li=li)
439                 li = self._generate_context_menu_items(entry=video, li=li)
440                 xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=isFolder)
441
442         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
443         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_TITLE)
444         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_VIDEO_YEAR)
445         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_GENRE)
446         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LASTPLAYED)
447         xbmcplugin.endOfDirectory(self.plugin_handle)
448         return True
449
450     def build_search_result_listing (self, video_list, actions, build_url):
451         """Builds the search results list Kodi screen
452
453         Parameters
454         ----------
455         video_list : :obj:`dict` of :obj:`str`
456             List of videos or shows
457
458         actions : :obj:`dict` of :obj:`str`
459             Dictionary of actions to build subsequent routes
460
461         build_url : :obj:`fn`
462             Function to build the subsequent routes
463
464         Returns
465         -------
466         bool
467             List could be build
468         """
469         return self.build_video_listing(video_list=video_list, actions=actions, type='search', build_url=build_url)
470
471     def build_no_seasons_available (self):
472         """Builds the season list screen if no seasons could be found
473
474         Returns
475         -------
476         bool
477             List could be build
478         """
479         self.show_no_seasons_notification()
480         xbmcplugin.endOfDirectory(self.plugin_handle)
481         return True
482
483     def build_no_search_results_available (self, build_url, action):
484         """Builds the search results screen if no matches could be found
485
486         Parameters
487         ----------
488         action : :obj:`str`
489             Action paramter to build the subsequent routes
490
491         build_url : :obj:`fn`
492             Function to build the subsequent routes
493
494         Returns
495         -------
496         bool
497             List could be build
498         """
499         self.show_no_search_results_notification()
500         return xbmcplugin.endOfDirectory(self.plugin_handle)
501
502     def build_user_sub_listing (self, video_list_ids, type, action, build_url):
503         """Builds the video lists screen for user subfolders (genres & recommendations)
504
505         Parameters
506         ----------
507         video_list_ids : :obj:`dict` of :obj:`str`
508             List of video lists
509
510         type : :obj:`str`
511             List type (genre or recommendation)
512
513         action : :obj:`str`
514             Action paramter to build the subsequent routes
515
516         build_url : :obj:`fn`
517             Function to build the subsequent routes
518
519         Returns
520         -------
521         bool
522             List could be build
523         """
524         for video_list_id in video_list_ids:
525             li = xbmcgui.ListItem(video_list_ids[video_list_id]['displayName'])
526             li.setProperty('fanart_image', self.default_fanart)
527             url = build_url({'action': action, 'video_list_id': video_list_id})
528             xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=True)
529
530         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
531         xbmcplugin.endOfDirectory(self.plugin_handle)
532         return True
533
534     def build_season_listing (self, seasons_sorted, season_list, build_url):
535         """Builds the season list screen for a show
536
537         Parameters
538         ----------
539         seasons_sorted : :obj:`list` of :obj:`str`
540             Sorted season indexes
541
542         season_list : :obj:`dict` of :obj:`str`
543             List of season entries
544
545         build_url : :obj:`fn`
546             Function to build the subsequent routes
547
548         Returns
549         -------
550         bool
551             List could be build
552         """
553         for index in seasons_sorted:
554             for season_id in season_list:
555                 season = season_list[season_id]
556                 if int(season['idx']) == index:
557                     li = xbmcgui.ListItem(label=season['text'])
558                     # add some art to the item
559                     li = self._generate_art_info(entry=season, li=li)
560                     # add list item info
561                     li = self._generate_entry_info(entry=season, li=li, base_info={'mediatype': 'season'})
562                     li = self._generate_context_menu_items(entry=season, li=li)
563                     url = build_url({'action': 'episode_list', 'season_id': season_id})
564                     xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=True)
565
566         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_NONE)
567         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_VIDEO_YEAR)
568         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
569         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LASTPLAYED)
570         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_TITLE)
571         xbmcplugin.endOfDirectory(self.plugin_handle)
572         return True
573
574     def build_episode_listing (self, episodes_sorted, episode_list, build_url):
575         """Builds the episode list screen for a season of a show
576
577         Parameters
578         ----------
579         episodes_sorted : :obj:`list` of :obj:`str`
580             Sorted episode indexes
581
582         episode_list : :obj:`dict` of :obj:`str`
583             List of episode entries
584
585         build_url : :obj:`fn`
586             Function to build the subsequent routes
587
588         Returns
589         -------
590         bool
591             List could be build
592         """
593         for index in episodes_sorted:
594             for episode_id in episode_list:
595                 episode = episode_list[episode_id]
596                 if int(episode['episode']) == index:
597                     li = xbmcgui.ListItem(label=episode['title'])
598                     # add some art to the item
599                     li = self._generate_art_info(entry=episode, li=li)
600                     # add list item info
601                     li = self._generate_entry_info(entry=episode, li=li, base_info={'mediatype': 'episode'})
602                     li = self._generate_context_menu_items(entry=episode, li=li)
603                     url = build_url({'action': 'play_video', 'video_id': episode_id, 'start_offset': episode['bookmark']})
604                     xbmcplugin.addDirectoryItem(handle=self.plugin_handle, url=url, listitem=li, isFolder=False)
605
606         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_EPISODE)
607         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_NONE)
608         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_VIDEO_YEAR)
609         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL)
610         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_LASTPLAYED)
611         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_TITLE)
612         xbmcplugin.addSortMethod(handle=self.plugin_handle, sortMethod=xbmcplugin.SORT_METHOD_DURATION)
613         xbmcplugin.endOfDirectory(self.plugin_handle)
614         return True
615
616     def play_item (self, esn, video_id, start_offset=-1):
617         """Plays a video
618
619         Parameters
620         ----------
621         esn : :obj:`str`
622             ESN needed for Widevine/Inputstream
623
624         video_id : :obj:`str`
625             ID of the video that should be played
626
627         start_offset : :obj:`str`
628             Offset to resume playback from (in seconds)
629
630         Returns
631         -------
632         bool
633             List could be build
634         """
635         inputstream_addon = self.get_inputstream_addon()
636         if inputstream_addon == None:
637             self.show_missing_inputstream_addon_notification()
638             self.log(msg='Inputstream addon not found')
639             return False
640
641         # inputstream addon properties
642         msl_service_url = 'http://localhost:' + str(self.addon.getSetting('msl_service_port'))
643         play_item = xbmcgui.ListItem(path=msl_service_url + '/manifest?id=' + video_id)
644         play_item.setProperty(inputstream_addon + '.license_type', 'com.widevine.alpha')
645         play_item.setProperty(inputstream_addon + '.manifest_type', 'mpd')
646         play_item.setProperty(inputstream_addon + '.license_key', msl_service_url + '/license?id=' + video_id + '||b{SSM}!b{SID}|')
647         play_item.setProperty(inputstream_addon + '.server_certificate', 'Cr0CCAMSEOVEukALwQ8307Y2+LVP+0MYh/HPkwUijgIwggEKAoIBAQDm875btoWUbGqQD8eAGuBlGY+Pxo8YF1LQR+Ex0pDONMet8EHslcZRBKNQ/09RZFTP0vrYimyYiBmk9GG+S0wB3CRITgweNE15cD33MQYyS3zpBd4z+sCJam2+jj1ZA4uijE2dxGC+gRBRnw9WoPyw7D8RuhGSJ95OEtzg3Ho+mEsxuE5xg9LM4+Zuro/9msz2bFgJUjQUVHo5j+k4qLWu4ObugFmc9DLIAohL58UR5k0XnvizulOHbMMxdzna9lwTw/4SALadEV/CZXBmswUtBgATDKNqjXwokohncpdsWSauH6vfS6FXwizQoZJ9TdjSGC60rUB2t+aYDm74cIuxAgMBAAE6EHRlc3QubmV0ZmxpeC5jb20SgAOE0y8yWw2Win6M2/bw7+aqVuQPwzS/YG5ySYvwCGQd0Dltr3hpik98WijUODUr6PxMn1ZYXOLo3eED6xYGM7Riza8XskRdCfF8xjj7L7/THPbixyn4mULsttSmWFhexzXnSeKqQHuoKmerqu0nu39iW3pcxDV/K7E6aaSr5ID0SCi7KRcL9BCUCz1g9c43sNj46BhMCWJSm0mx1XFDcoKZWhpj5FAgU4Q4e6f+S8eX39nf6D6SJRb4ap7Znzn7preIvmS93xWjm75I6UBVQGo6pn4qWNCgLYlGGCQCUm5tg566j+/g5jvYZkTJvbiZFwtjMW5njbSRwB3W4CrKoyxw4qsJNSaZRTKAvSjTKdqVDXV/U5HK7SaBA6iJ981/aforXbd2vZlRXO/2S+Maa2mHULzsD+S5l4/YGpSt7PnkCe25F+nAovtl/ogZgjMeEdFyd/9YMYjOS4krYmwp3yJ7m9ZzYCQ6I8RQN4x/yLlHG5RH/+WNLNUs6JAZ0fFdCmw=')
648         # TODO: Change when Kodi can handle/trnsfer defaults in hidden values in settings
649         #play_item.setProperty(inputstream_addon + '.server_certificate', self.addon.getSetting('msl_service_certificate'))
650         play_item.setProperty('inputstreamaddon', inputstream_addon)
651
652         # check if we have a bookmark e.g. start offset position
653         if int(start_offset) > 0:
654             play_item.setProperty('StartOffset', str(start_offset) + '.0')
655         return xbmcplugin.setResolvedUrl(self.plugin_handle, True, listitem=play_item)
656
657     def _generate_art_info (self, entry, li):
658         """Adds the art info from an entry to a Kodi list item
659
660         Parameters
661         ----------
662         entry : :obj:`dict` of :obj:`str`
663             Entry that should be turned into a list item
664
665         li : :obj:`XMBC.ListItem`
666             Kodi list item instance
667
668         Returns
669         -------
670         :obj:`XMBC.ListItem`
671             Kodi list item instance
672         """
673         art = {'fanart': self.default_fanart}
674         if 'boxarts' in dict(entry).keys():
675             art.update({
676                 'poster': entry['boxarts']['big'],
677                 'landscape': entry['boxarts']['big'],
678                 'thumb': entry['boxarts']['small'],
679                 'fanart': entry['boxarts']['big']
680             })
681         if 'interesting_moment' in dict(entry).keys():
682             art.update({
683                 'poster': entry['interesting_moment'],
684                 'fanart': entry['interesting_moment']
685             })
686         if 'thumb' in dict(entry).keys():
687             art.update({'thumb': entry['thumb']})
688         if 'fanart' in dict(entry).keys():
689             art.update({'fanart': entry['fanart']})
690         if 'poster' in dict(entry).keys():
691             art.update({'poster': entry['poster']})
692         li.setArt(art)
693         return li
694
695     def _generate_entry_info (self, entry, li, base_info={}):
696         """Adds the item info from an entry to a Kodi list item
697
698         Parameters
699         ----------
700         entry : :obj:`dict` of :obj:`str`
701             Entry that should be turned into a list item
702
703         li : :obj:`XMBC.ListItem`
704             Kodi list item instance
705
706         base_info : :obj:`dict` of :obj:`str`
707             Additional info that overrules the entry info
708
709         Returns
710         -------
711         :obj:`XMBC.ListItem`
712             Kodi list item instance
713         """
714         infos = base_info
715         entry_keys = entry.keys()
716         if 'cast' in entry_keys and len(entry['cast']) > 0:
717             infos.update({'cast': entry['cast']})
718         if 'creators' in entry_keys and len(entry['creators']) > 0:
719             infos.update({'writer': entry['creators'][0]})
720         if 'directors' in entry_keys and len(entry['directors']) > 0:
721             infos.update({'director': entry['directors'][0]})
722         if 'genres' in entry_keys and len(entry['genres']) > 0:
723             infos.update({'genre': entry['genres'][0]})
724         if 'maturity' in entry_keys:
725             if 'mpaa' in entry_keys:
726                 infos.update({'mpaa': entry['mpaa']})
727             else:
728                 infos.update({'mpaa': str(entry['maturity']['board']) + '-' + str(entry['maturity']['value'])})
729         if 'rating' in entry_keys:
730             infos.update({'rating': int(entry['rating']) * 2})
731         if 'synopsis' in entry_keys:
732             infos.update({'plot': entry['synopsis']})
733         if 'plot' in entry_keys:
734             infos.update({'plot': entry['plot']})
735         if 'runtime' in entry_keys:
736             infos.update({'duration': entry['runtime']})
737         if 'duration' in entry_keys:
738             infos.update({'duration': entry['duration']})
739         if 'seasons_label' in entry_keys:
740             infos.update({'season': entry['seasons_label']})
741         if 'season' in entry_keys:
742             infos.update({'season': entry['season']})
743         if 'title' in entry_keys:
744             infos.update({'title': entry['title']})
745         if 'type' in entry_keys:
746             if entry['type'] == 'movie' or entry['type'] == 'episode':
747                 li.setProperty('IsPlayable', 'true')
748         if 'mediatype' in entry_keys:
749             if entry['mediatype'] == 'movie' or entry['mediatype'] == 'episode':
750                 li.setProperty('IsPlayable', 'true')
751                 infos.update({'mediatype': entry['mediatype']})
752         if 'watched' in entry_keys:
753             infos.update({'playcount': (1, 0)[entry['watched']]})
754         if 'index' in entry_keys:
755             infos.update({'episode': entry['index']})
756         if 'episode' in entry_keys:
757             infos.update({'episode': entry['episode']})
758         if 'year' in entry_keys:
759             infos.update({'year': entry['year']})
760         if 'quality' in entry_keys:
761             quality = {'width': '960', 'height': '540'}
762             if entry['quality'] == '720':
763                 quality = {'width': '1280', 'height': '720'}
764             if entry['quality'] == '1080':
765                 quality = {'width': '1920', 'height': '1080'}
766             li.addStreamInfo('video', quality)
767         li.setInfo('video', infos)
768         return li
769
770     def _generate_context_menu_items (self, entry, li):
771         """Adds context menue items to a Kodi list item
772
773         Parameters
774         ----------
775         entry : :obj:`dict` of :obj:`str`
776             Entry that should be turned into a list item
777
778         li : :obj:`XMBC.ListItem`
779             Kodi list item instance
780         Returns
781         -------
782         :obj:`XMBC.ListItem`
783             Kodi list item instance
784         """
785         items = []
786         action = {}
787         entry_keys = entry.keys()
788
789         # action item templates
790         encoded_title = urllib.urlencode({'title': entry['title'].encode('utf-8')}) if 'title' in entry else ''
791         url_tmpl = 'XBMC.RunPlugin(' + self.base_url + '?action=%action%&id=' + str(entry['id']) + '&' + encoded_title + ')'
792         actions = [
793             ['export_to_library', self.get_local_string(30018), 'export'],
794             ['remove_from_library', self.get_local_string(30030), 'remove'],
795             ['rate_on_netflix', self.get_local_string(30019), 'rating'],
796             ['remove_from_my_list', self.get_local_string(30020), 'remove_from_list'],
797             ['add_to_my_list', self.get_local_string(30021), 'add_to_list']
798         ]
799
800         # build concrete action items
801         for action_item in actions:
802             action.update({action_item[0]: [action_item[1], url_tmpl.replace('%action%', action_item[2])]})
803
804         # add or remove the movie/show/season/episode from & to the users "My List"
805         if 'in_my_list' in entry_keys:
806             items.append(action['remove_from_my_list']) if entry['in_my_list'] else items.append(action['add_to_my_list'])
807         elif 'queue' in entry_keys:
808             items.append(action['remove_from_my_list']) if entry['queue'] else items.append(action['add_to_my_list'])
809         elif 'my_list' in entry_keys:
810             items.append(action['remove_from_my_list']) if entry['my_list'] else items.append(action['add_to_my_list'])
811         # rate the movie/show/season/episode on Netflix
812         items.append(action['rate_on_netflix'])
813
814         # add possibility to export this movie/show/season/episode to a static/local library (and to remove it)
815         if 'type' in entry_keys:
816             # add/remove movie
817             if entry['type'] == 'movie':
818                 action_type = 'remove_from_library' if self.library.movie_exists(title=entry['title'], year=entry['year']) else 'export_to_library'
819                 items.append(action[action_type])
820             # add/remove show
821             if entry['type'] == 'show' and 'title' in entry_keys:
822                 action_type = 'remove_from_library' if self.library.show_exists(title=entry['title']) else 'export_to_library'
823                 items.append(action[action_type])
824
825         # add it to the item
826         li.addContextMenuItems(items)
827         return li
828
829     def log (self, msg, level=xbmc.LOGDEBUG):
830         """Adds a log entry to the Kodi log
831
832         Parameters
833         ----------
834         msg : :obj:`str`
835             Entry that should be turned into a list item
836
837         level : :obj:`int`
838             Kodi log level
839         """
840         if isinstance(msg, unicode):
841             msg = msg.encode('utf-8')
842         xbmc.log('[%s] %s' % (self.plugin, msg.__str__()), level)
843
844     def get_local_string (self, string_id):
845         """Returns the localized version of a string
846
847         Parameters
848         ----------
849         string_id : :obj:`int`
850             ID of the string that shoudl be fetched
851
852         Returns
853         -------
854         :obj:`str`
855             Requested string or empty string
856         """
857         src = xbmc if string_id < 30000 else self.addon
858         locString = src.getLocalizedString(string_id)
859         if isinstance(locString, unicode):
860             locString = locString.encode('utf-8')
861         return locString
862
863     def get_inputstream_addon (self):
864         """Checks if the inputstream addon is installed & enabled.
865            Returns the type of the inputstream addon used or None if not found
866
867         Returns
868         -------
869         :obj:`str` or None
870             Inputstream addon or None
871         """
872         type = 'inputstream.adaptive'
873         payload = {
874             'jsonrpc': '2.0',
875             'id': 1,
876             'method': 'Addons.GetAddonDetails',
877             'params': {
878                 'addonid': type,
879                 'properties': ['enabled']
880             }
881         }
882         response = xbmc.executeJSONRPC(json.dumps(payload))
883         data = json.loads(response)
884         if not 'error' in data.keys():
885             if data['result']['addon']['enabled'] == True:
886                 return type
887         return None
888
889     def set_library (self, library):
890         """Adds an instance of the Library class
891
892         Parameters
893         ----------
894         library : :obj:`Library`
895             instance of the Library class
896         """
897         self.library = library