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