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
44 def is_logged_in (self, params):
45 """Existing login proxy function
49 params : :obj:`dict` of :obj:`str`
54 :obj:`Requests.Response`
55 Response of the remote call
57 if self.credentials['email'] == '' or self.credentials['password'] == '':
59 return self.netflix_session.is_logged_in(account=self.credentials)
61 def logout (self, params):
62 """Logout proxy function
66 params : :obj:`dict` of :obj:`str`
71 :obj:`Requests.Response`
72 Response of the remote call
75 self.credentials = {'email': '', 'password': ''}
76 return self.netflix_session.logout()
78 def login (self, params):
79 """Logout proxy function
83 params : :obj:`dict` of :obj:`str`
88 :obj:`Requests.Response`
89 Response of the remote call
91 email = params.get('email', [''])[0]
92 password = params.get('password', [''])[0]
93 if email != '' and password != '':
94 self.credentials = {'email': email, 'password': password}
95 _ret = self.netflix_session.login(account=self.credentials)
96 self.profiles = self.netflix_session.profiles
100 def list_profiles (self, params):
101 """Returns the cached list of profiles
105 params : :obj:`dict` of :obj:`str`
110 :obj:`dict` of :obj:`str`
115 def get_esn (self, params):
116 """ESN getter function
120 params : :obj:`dict` of :obj:`str`
128 return self.netflix_session.esn
130 def fetch_video_list_ids (self, params):
131 """Video list ids proxy function (caches video lists)
135 params : :obj:`dict` of :obj:`str`
141 Transformed response of the remote call
143 cached_list = self.video_list_cache.get(self.netflix_session.user_data['guid'], None)
144 if cached_list != None:
145 self.kodi_helper.log('Serving cached list for user: ' + self.netflix_session.user_data['guid'])
147 video_list_ids_raw = self.netflix_session.fetch_video_list_ids()
149 if 'error' in video_list_ids_raw:
150 return video_list_ids_raw
151 return self.netflix_session.parse_video_list_ids(response_data=video_list_ids_raw)
153 def fetch_video_list (self, params):
154 """Video list proxy function
158 params : :obj:`dict` of :obj:`str`
164 Transformed response of the remote call
166 list_id = params.get('list_id', [''])[0]
167 raw_video_list = self.netflix_session.fetch_video_list(list_id=list_id)
168 if 'error' in raw_video_list:
169 return raw_video_list
170 # parse the video list ids
171 if 'videos' in raw_video_list.get('value', {}).keys():
172 return self.netflix_session.parse_video_list(response_data=raw_video_list)
175 def fetch_episodes_by_season (self, params):
176 """Episodes for season proxy function
180 params : :obj:`dict` of :obj:`str`
186 Transformed response of the remote call
188 raw_episode_list = self.netflix_session.fetch_episodes_by_season(season_id=params.get('season_id')[0])
189 if 'error' in raw_episode_list:
190 return raw_episode_list
191 return self.netflix_session.parse_episodes_by_season(response_data=raw_episode_list)
193 def fetch_seasons_for_show (self, params):
194 """Season for show proxy function
198 params : :obj:`dict` of :obj:`str`
204 Transformed response of the remote call
206 show_id = params.get('show_id', [''])[0]
207 raw_season_list = self.netflix_session.fetch_seasons_for_show(id=show_id)
208 if 'error' in raw_season_list:
209 return raw_season_list
210 # check if we have sesons, announced shows that are not available yet have none
211 if 'seasons' not in raw_season_list.get('value', {}):
213 return self.netflix_session.parse_seasons(id=show_id, response_data=raw_season_list)
215 def rate_video (self, params):
216 """Video rating proxy function
220 params : :obj:`dict` of :obj:`str`
225 :obj:`Requests.Response`
226 Response of the remote call
228 video_id = params.get('video_id', [''])[0]
229 rating = params.get('rating', [''])[0]
230 return self.netflix_session.rate_video(video_id=video_id, rating=rating)
232 def remove_from_list (self, params):
233 """Remove from my list proxy function
237 params : :obj:`dict` of :obj:`str`
242 :obj:`Requests.Response`
243 Response of the remote call
245 video_id = params.get('video_id', [''])[0]
246 return self.netflix_session.remove_from_list(video_id=video_id)
248 def add_to_list (self, params):
249 """Add to my list proxy function
253 params : :obj:`dict` of :obj:`str`
258 :obj:`Requests.Response`
259 Response of the remote call
261 video_id = params.get('video_id', [''])[0]
262 return self.netflix_session.add_to_list(video_id=video_id)
264 def fetch_metadata (self, params):
265 """Metadata proxy function
269 params : :obj:`dict` of :obj:`str`
274 :obj:`Requests.Response`
275 Response of the remote call
277 video_id = params.get('video_id', [''])[0]
278 return self.netflix_session.fetch_metadata(id=video_id)
280 def switch_profile (self, params):
281 """Switch profile proxy function
285 params : :obj:`dict` of :obj:`str`
290 :obj:`Requests.Response`
291 Response of the remote call
293 profile_id = params.get('profile_id', [''])[0]
294 return self.netflix_session.switch_profile(profile_id=profile_id, account=self.credentials)
296 def get_user_data (self, params):
297 """User data getter function
301 params : :obj:`dict` of :obj:`str`
309 return self.netflix_session.user_data
311 def search (self, params):
312 """Search proxy function
316 params : :obj:`dict` of :obj:`str`
322 Transformed response of the remote call
324 term = params.get('term', [''])[0]
325 has_search_results = False
326 raw_search_results = self.netflix_session.fetch_search_results(search_str=term)
327 # check for any errors
328 if 'error' in raw_search_results:
329 return raw_search_results
331 # determine if we found something
332 if 'search' in raw_search_results['value']:
333 for key in raw_search_results['value']['search'].keys():
334 if self.netflix_session._is_size_key(key=key) == False:
335 has_search_results = raw_search_results['value']['search'][key]['titles']['length'] > 0
336 if has_search_results == False:
337 if raw_search_results['value']['search'][key].get('suggestions', False) != False:
338 for entry in raw_search_results['value']['search'][key]['suggestions']:
339 if self.netflix_session._is_size_key(key=entry) == False:
340 if raw_search_results['value']['search'][key]['suggestions'][entry]['relatedvideos']['length'] > 0:
341 has_search_results = True
343 # display that we haven't found a thing
344 if has_search_results == False:
347 # list the search results
348 search_results = self.netflix_session.parse_search_results(response_data=raw_search_results)
349 # add more menaingful data to the search results
350 raw_search_contents = self.netflix_session.fetch_video_list_information(video_ids=search_results.keys())
351 # check for any errors
352 if 'error' in raw_search_contents:
353 return raw_search_contents
354 return self.netflix_session.parse_video_list(response_data=raw_search_contents)