2 # -*- coding: utf-8 -*-
3 # Module: NetflixHttpSubRessourceHandler
4 # Created on: 07.03.2017
6 class NetflixHttpSubRessourceHandler:
7 """ Represents the callable internal server routes & translates/executes them to requests for Netflix"""
9 def __init__ (self, kodi_helper, netflix_session):
10 """Sets up credentials & video_list_cache cache
11 Assigns the netflix_session/kodi_helper instacnes
12 Does the initial login if we have user data
16 kodi_helper : :obj:`KodiHelper`
17 instance of the KodiHelper class
19 netflix_session : :obj:`NetflixSession`
20 instance of the NetflixSession class
22 self.kodi_helper = kodi_helper
23 self.netflix_session = netflix_session
24 self.credentials = self.kodi_helper.get_credentials()
26 self.video_list_cache = {}
29 def prefetch_login (self):
30 """Check if we have stored credentials.
31 If so, do the login before the user requests it
32 If that is done, we cache the profiles
34 if self.credentials['email'] != '' and self.credentials['password'] != '':
35 if self.netflix_session.is_logged_in(account=self.credentials):
36 self.netflix_session.refresh_session_data(account=self.credentials)
37 self.profiles = self.netflix_session.profiles
39 self.netflix_session.login(account=self.credentials)
40 self.profiles = self.netflix_session.profiles
43 self.kodi_helper.set_esn(self.netflix_session.esn)
45 def is_logged_in (self, params):
46 """Existing login proxy function
50 params : :obj:`dict` of :obj:`str`
55 :obj:`Requests.Response`
56 Response of the remote call
58 if self.credentials['email'] == '' or self.credentials['password'] == '':
60 return self.netflix_session.is_logged_in(account=self.credentials)
62 def logout (self, params):
63 """Logout proxy function
67 params : :obj:`dict` of :obj:`str`
72 :obj:`Requests.Response`
73 Response of the remote call
76 self.credentials = {'email': '', 'password': ''}
77 return self.netflix_session.logout()
79 def login (self, params):
80 """Logout proxy function
84 params : :obj:`dict` of :obj:`str`
89 :obj:`Requests.Response`
90 Response of the remote call
92 email = params.get('email', [''])[0]
93 password = params.get('password', [''])[0]
94 if email != '' and password != '':
95 self.credentials = {'email': email, 'password': password}
96 _ret = self.netflix_session.login(account=self.credentials)
97 self.profiles = self.netflix_session.profiles
101 def list_profiles (self, params):
102 """Returns the cached list of profiles
106 params : :obj:`dict` of :obj:`str`
111 :obj:`dict` of :obj:`str`
116 def get_esn (self, params):
117 """ESN getter function
121 params : :obj:`dict` of :obj:`str`
129 return self.netflix_session.esn
131 def fetch_video_list_ids (self, params):
132 """Video list ids proxy function (caches video lists)
136 params : :obj:`dict` of :obj:`str`
142 Transformed response of the remote call
144 cached_list = self.video_list_cache.get(self.netflix_session.user_data['guid'], None)
145 if cached_list != None:
146 self.kodi_helper.log('Serving cached list for user: ' + self.netflix_session.user_data['guid'])
148 video_list_ids_raw = self.netflix_session.fetch_video_list_ids()
150 if 'error' in video_list_ids_raw:
151 return video_list_ids_raw
152 return self.netflix_session.parse_video_list_ids(response_data=video_list_ids_raw)
154 def fetch_video_list (self, params):
155 """Video list proxy function
159 params : :obj:`dict` of :obj:`str`
165 Transformed response of the remote call
167 list_id = params.get('list_id', [''])[0]
168 raw_video_list = self.netflix_session.fetch_video_list(list_id=list_id)
169 if 'error' in raw_video_list:
170 return raw_video_list
171 # parse the video list ids
172 if 'videos' in raw_video_list.get('value', {}).keys():
173 return self.netflix_session.parse_video_list(response_data=raw_video_list)
176 def fetch_episodes_by_season (self, params):
177 """Episodes for season proxy function
181 params : :obj:`dict` of :obj:`str`
187 Transformed response of the remote call
189 raw_episode_list = self.netflix_session.fetch_episodes_by_season(season_id=params.get('season_id')[0])
190 if 'error' in raw_episode_list:
191 return raw_episode_list
192 return self.netflix_session.parse_episodes_by_season(response_data=raw_episode_list)
194 def fetch_seasons_for_show (self, params):
195 """Season for show proxy function
199 params : :obj:`dict` of :obj:`str`
205 Transformed response of the remote call
207 show_id = params.get('show_id', [''])[0]
208 raw_season_list = self.netflix_session.fetch_seasons_for_show(id=show_id)
209 if 'error' in raw_season_list:
210 return raw_season_list
211 # check if we have sesons, announced shows that are not available yet have none
212 if 'seasons' not in raw_season_list.get('value', {}):
214 return self.netflix_session.parse_seasons(id=show_id, response_data=raw_season_list)
216 def rate_video (self, params):
217 """Video rating proxy function
221 params : :obj:`dict` of :obj:`str`
226 :obj:`Requests.Response`
227 Response of the remote call
229 video_id = params.get('video_id', [''])[0]
230 rating = params.get('rating', [''])[0]
231 return self.netflix_session.rate_video(video_id=video_id, rating=rating)
233 def remove_from_list (self, params):
234 """Remove from my list proxy function
238 params : :obj:`dict` of :obj:`str`
243 :obj:`Requests.Response`
244 Response of the remote call
246 video_id = params.get('video_id', [''])[0]
247 return self.netflix_session.remove_from_list(video_id=video_id)
249 def add_to_list (self, params):
250 """Add to my list proxy function
254 params : :obj:`dict` of :obj:`str`
259 :obj:`Requests.Response`
260 Response of the remote call
262 video_id = params.get('video_id', [''])[0]
263 return self.netflix_session.add_to_list(video_id=video_id)
265 def fetch_metadata (self, params):
266 """Metadata proxy function
270 params : :obj:`dict` of :obj:`str`
275 :obj:`Requests.Response`
276 Response of the remote call
278 video_id = params.get('video_id', [''])[0]
279 return self.netflix_session.fetch_metadata(id=video_id)
281 def switch_profile (self, params):
282 """Switch profile proxy function
286 params : :obj:`dict` of :obj:`str`
291 :obj:`Requests.Response`
292 Response of the remote call
294 profile_id = params.get('profile_id', [''])[0]
295 return self.netflix_session.switch_profile(profile_id=profile_id, account=self.credentials)
297 def get_user_data (self, params):
298 """User data getter function
302 params : :obj:`dict` of :obj:`str`
310 return self.netflix_session.user_data
312 def search (self, params):
313 """Search proxy function
317 params : :obj:`dict` of :obj:`str`
323 Transformed response of the remote call
325 term = params.get('term', [''])[0]
326 has_search_results = False
327 raw_search_results = self.netflix_session.fetch_search_results(search_str=term)
328 # check for any errors
329 if 'error' in raw_search_results:
330 return raw_search_results
332 # determine if we found something
333 if 'search' in raw_search_results['value']:
334 for key in raw_search_results['value']['search'].keys():
335 if self.netflix_session._is_size_key(key=key) == False:
336 has_search_results = raw_search_results['value']['search'][key]['titles']['length'] > 0
337 if has_search_results == False:
338 if raw_search_results['value']['search'][key].get('suggestions', False) != False:
339 for entry in raw_search_results['value']['search'][key]['suggestions']:
340 if self.netflix_session._is_size_key(key=entry) == False:
341 if raw_search_results['value']['search'][key]['suggestions'][entry]['relatedvideos']['length'] > 0:
342 has_search_results = True
344 # display that we haven't found a thing
345 if has_search_results == False:
348 # list the search results
349 search_results = self.netflix_session.parse_search_results(response_data=raw_search_results)
350 # add more menaingful data to the search results
351 raw_search_contents = self.netflix_session.fetch_video_list_information(video_ids=search_results.keys())
352 # check for any errors
353 if 'error' in raw_search_contents:
354 return raw_search_contents
355 return self.netflix_session.parse_video_list(response_data=raw_search_contents)