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()
25 self.video_list_cache = {}
27 # check if we have stored credentials, if so, do the login before the user requests it
28 # if that is done, we cache the profiles
29 if self.credentials['email'] != '' and self.credentials['password'] != '':
30 if self.netflix_session.is_logged_in(account=self.credentials):
31 self.netflix_session.refresh_session_data(account=self.credentials)
33 self.netflix_session.login(account=self.credentials)
34 self.profiles = self.netflix_session.profiles
38 def is_logged_in (self, params):
39 """Existing login proxy function
43 params : :obj:`dict` of :obj:`str`
48 :obj:`Requests.Response`
49 Response of the remote call
51 if self.credentials['email'] == '' or self.credentials['password'] == '':
53 return self.netflix_session.is_logged_in(account=self.credentials)
55 def logout (self, params):
56 """Logout proxy function
60 params : :obj:`dict` of :obj:`str`
65 :obj:`Requests.Response`
66 Response of the remote call
69 self.credentials = {'email': '', 'password': ''}
70 return self.netflix_session.logout()
72 def login (self, params):
73 """Logout proxy function
77 params : :obj:`dict` of :obj:`str`
82 :obj:`Requests.Response`
83 Response of the remote call
85 email = params.get('email', [''])[0]
86 password = params.get('password', [''])[0]
87 if email != '' and password != '':
88 self.credentials = {'email': email, 'password': password}
89 _ret = self.netflix_session.login(account=self.credentials)
90 self.profiles = self.netflix_session.profiles
94 def list_profiles (self, params):
95 """Returns the cached list of profiles
99 params : :obj:`dict` of :obj:`str`
104 :obj:`dict` of :obj:`str`
109 def get_esn (self, params):
110 """ESN getter function
114 params : :obj:`dict` of :obj:`str`
122 return self.netflix_session.esn
124 def fetch_video_list_ids (self, params):
125 """Video list ids proxy function (caches video lists)
129 params : :obj:`dict` of :obj:`str`
135 Transformed response of the remote call
137 cached_list = self.video_list_cache.get(self.netflix_session.user_data['guid'], None)
138 if cached_list != None:
139 self.kodi_helper.log('Serving cached list for user: ' + self.netflix_session.user_data['guid'])
141 video_list_ids_raw = self.netflix_session.fetch_video_list_ids()
142 if 'error' in video_list_ids_raw:
143 return video_list_ids_raw
144 return self.netflix_session.parse_video_list_ids(response_data=video_list_ids_raw)
146 def fetch_video_list (self, params):
147 """Video list proxy function
151 params : :obj:`dict` of :obj:`str`
157 Transformed response of the remote call
159 list_id = params.get('list_id', [''])[0]
160 raw_video_list = self.netflix_session.fetch_video_list(list_id=list_id)
161 if 'error' in raw_video_list:
162 return raw_video_list
163 # parse the video list ids
164 if 'videos' in raw_video_list.get('value', {}).keys():
165 return self.netflix_session.parse_video_list(response_data=raw_video_list)
168 def fetch_episodes_by_season (self, params):
169 """Episodes for season proxy function
173 params : :obj:`dict` of :obj:`str`
179 Transformed response of the remote call
181 raw_episode_list = self.netflix_session.fetch_episodes_by_season(season_id=params.get('season_id')[0])
182 if 'error' in raw_episode_list:
183 return raw_episode_list
184 return self.netflix_session.parse_episodes_by_season(response_data=raw_episode_list)
186 def fetch_seasons_for_show (self, params):
187 """Season for show proxy function
191 params : :obj:`dict` of :obj:`str`
197 Transformed response of the remote call
199 show_id = params.get('show_id', [''])[0]
200 raw_season_list = self.netflix_session.fetch_seasons_for_show(id=show_id)
201 if 'error' in raw_season_list:
202 return raw_season_list
203 # check if we have sesons, announced shows that are not available yet have none
204 if 'seasons' not in raw_season_list.get('value', {}):
206 return self.netflix_session.parse_seasons(id=show_id, response_data=raw_season_list)
208 def rate_video (self, params):
209 """Video rating proxy function
213 params : :obj:`dict` of :obj:`str`
218 :obj:`Requests.Response`
219 Response of the remote call
221 video_id = params.get('video_id', [''])[0]
222 rating = params.get('rating', [''])[0]
223 return self.netflix_session.rate_video(video_id=video_id, rating=rating)
225 def remove_from_list (self, params):
226 """Remove from my list proxy function
230 params : :obj:`dict` of :obj:`str`
235 :obj:`Requests.Response`
236 Response of the remote call
238 video_id = params.get('video_id', [''])[0]
239 return self.netflix_session.remove_from_list(video_id=video_id)
241 def add_to_list (self, params):
242 """Add to my list proxy function
246 params : :obj:`dict` of :obj:`str`
251 :obj:`Requests.Response`
252 Response of the remote call
254 video_id = params.get('video_id', [''])[0]
255 return self.netflix_session.add_to_list(video_id=video_id)
257 def fetch_metadata (self, params):
258 """Metadata proxy function
262 params : :obj:`dict` of :obj:`str`
267 :obj:`Requests.Response`
268 Response of the remote call
270 video_id = params.get('video_id', [''])[0]
271 return self.netflix_session.fetch_metadata(id=video_id)
273 def switch_profile (self, params):
274 """Switch profile proxy function
278 params : :obj:`dict` of :obj:`str`
283 :obj:`Requests.Response`
284 Response of the remote call
286 profile_id = params.get('profile_id', [''])[0]
287 return self.netflix_session.switch_profile(profile_id=profile_id, account=self.credentials)
289 def get_user_data (self, params):
290 """User data getter function
294 params : :obj:`dict` of :obj:`str`
302 return self.netflix_session.user_data
304 def search (self, params):
305 """Search proxy function
309 params : :obj:`dict` of :obj:`str`
315 Transformed response of the remote call
317 term = params.get('term', [''])[0]
318 has_search_results = False
319 raw_search_results = self.netflix_session.fetch_search_results(search_str=term)
320 # check for any errors
321 if 'error' in raw_search_results:
322 return raw_search_results
324 # determine if we found something
325 if 'search' in raw_search_results['value']:
326 for key in raw_search_results['value']['search'].keys():
327 if self.netflix_session._is_size_key(key=key) == False:
328 has_search_results = raw_search_results['value']['search'][key]['titles']['length'] > 0
329 if has_search_results == False:
330 if raw_search_results['value']['search'][key].get('suggestions', False) != False:
331 for entry in raw_search_results['value']['search'][key]['suggestions']:
332 if self.netflix_session._is_size_key(key=entry) == False:
333 if raw_search_results['value']['search'][key]['suggestions'][entry]['relatedvideos']['length'] > 0:
334 has_search_results = True
336 # display that we haven't found a thing
337 if has_search_results == False:
340 # list the search results
341 search_results = self.netflix_session.parse_search_results(response_data=raw_search_results)
342 # add more menaingful data to the search results
343 raw_search_contents = self.netflix_session.fetch_video_list_information(video_ids=search_results.keys())
344 # check for any errors
345 if 'error' in raw_search_contents:
346 return raw_search_contents
347 return self.netflix_session.parse_video_list(response_data=raw_search_contents)