2 # -*- coding: utf-8 -*-
3 # Module: NetflixHttpSubRessourceHandler
4 # Created on: 07.03.2017
6 from urllib2 import urlopen, URLError
9 class NetflixHttpSubRessourceHandler:
10 """ Represents the callable internal server routes & translates/executes them to requests for Netflix"""
12 def __init__ (self, kodi_helper, netflix_session):
13 """Sets up credentials & video_list_cache cache
14 Assigns the netflix_session/kodi_helper instacnes
15 Does the initial login if we have user data
19 kodi_helper : :obj:`KodiHelper`
20 instance of the KodiHelper class
22 netflix_session : :obj:`NetflixSession`
23 instance of the NetflixSession class
25 self.kodi_helper = kodi_helper
26 self.netflix_session = netflix_session
27 self.credentials = self.kodi_helper.get_credentials()
30 self.video_list_cache = {}
33 def prefetch_login (self):
34 """Check if we have stored credentials.
35 If so, do the login before the user requests it
36 If that is done, we cache the profiles
38 if self._network_availble():
39 if self.credentials['email'] != '' and self.credentials['password'] != '':
40 if self.netflix_session.is_logged_in(account=self.credentials):
41 self.netflix_session.refresh_session_data(account=self.credentials)
42 self.profiles = self.netflix_session.profiles
44 self.netflix_session.login(account=self.credentials)
45 self.profiles = self.netflix_session.profiles
52 def is_logged_in (self, params):
53 """Existing login proxy function
57 params : :obj:`dict` of :obj:`str`
62 :obj:`Requests.Response`
63 Response of the remote call
65 if self.credentials['email'] == '' or self.credentials['password'] == '':
67 return self.netflix_session.is_logged_in(account=self.credentials)
69 def logout (self, params):
70 """Logout proxy function
74 params : :obj:`dict` of :obj:`str`
79 :obj:`Requests.Response`
80 Response of the remote call
83 self.credentials = {'email': '', 'password': ''}
85 return self.netflix_session.logout()
87 def login (self, params):
88 """Logout proxy function
92 params : :obj:`dict` of :obj:`str`
97 :obj:`Requests.Response`
98 Response of the remote call
100 email = params.get('email', [''])[0]
101 password = params.get('password', [''])[0]
102 if email != '' and password != '':
103 self.credentials = {'email': email, 'password': password}
104 _ret = self.netflix_session.login(account=self.credentials)
105 self.profiles = self.netflix_session.profiles
109 def list_profiles (self, params):
110 """Returns the cached list of profiles
114 params : :obj:`dict` of :obj:`str`
119 :obj:`dict` of :obj:`str`
124 def get_esn (self, params):
125 """ESN getter function
129 params : :obj:`dict` of :obj:`str`
137 return self.netflix_session.esn
139 def fetch_video_list_ids (self, params):
140 """Video list ids proxy function (caches video lists)
144 params : :obj:`dict` of :obj:`str`
150 Transformed response of the remote call
152 cached_list = self.video_list_cache.get(self.netflix_session.user_data['guid'], None)
153 if cached_list != None:
154 self.kodi_helper.log('Serving cached list for user: ' + self.netflix_session.user_data['guid'])
156 video_list_ids_raw = self.netflix_session.fetch_video_list_ids()
158 if 'error' in video_list_ids_raw:
159 return video_list_ids_raw
160 return self.netflix_session.parse_video_list_ids(response_data=video_list_ids_raw)
162 def fetch_video_list (self, params):
163 """Video list proxy function
167 params : :obj:`dict` of :obj:`str`
173 Transformed response of the remote call
175 list_id = params.get('list_id', [''])[0]
176 raw_video_list = self.netflix_session.fetch_video_list(list_id=list_id)
177 if 'error' in raw_video_list:
178 return raw_video_list
179 # parse the video list ids
180 if 'videos' in raw_video_list.get('value', {}).keys():
181 return self.netflix_session.parse_video_list(response_data=raw_video_list)
184 def fetch_episodes_by_season (self, params):
185 """Episodes for season proxy function
189 params : :obj:`dict` of :obj:`str`
195 Transformed response of the remote call
197 raw_episode_list = self.netflix_session.fetch_episodes_by_season(season_id=params.get('season_id')[0])
198 if 'error' in raw_episode_list:
199 return raw_episode_list
200 return self.netflix_session.parse_episodes_by_season(response_data=raw_episode_list)
202 def fetch_seasons_for_show (self, params):
203 """Season for show proxy function
207 params : :obj:`dict` of :obj:`str`
213 Transformed response of the remote call
215 show_id = params.get('show_id', [''])[0]
216 raw_season_list = self.netflix_session.fetch_seasons_for_show(id=show_id)
217 if 'error' in raw_season_list:
218 return raw_season_list
219 # check if we have sesons, announced shows that are not available yet have none
220 if 'seasons' not in raw_season_list.get('value', {}):
222 return self.netflix_session.parse_seasons(id=show_id, response_data=raw_season_list)
224 def rate_video (self, params):
225 """Video rating proxy function
229 params : :obj:`dict` of :obj:`str`
234 :obj:`Requests.Response`
235 Response of the remote call
237 video_id = params.get('video_id', [''])[0]
238 rating = params.get('rating', [''])[0]
239 return self.netflix_session.rate_video(video_id=video_id, rating=rating)
241 def remove_from_list (self, params):
242 """Remove from 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.remove_from_list(video_id=video_id)
257 def add_to_list (self, params):
258 """Add to my list 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.add_to_list(video_id=video_id)
273 def fetch_metadata (self, params):
274 """Metadata proxy function
278 params : :obj:`dict` of :obj:`str`
283 :obj:`Requests.Response`
284 Response of the remote call
286 video_id = params.get('video_id', [''])[0]
287 return self.netflix_session.fetch_metadata(id=video_id)
289 def switch_profile (self, params):
290 """Switch profile proxy function
294 params : :obj:`dict` of :obj:`str`
299 :obj:`Requests.Response`
300 Response of the remote call
302 profile_id = params.get('profile_id', [''])[0]
304 return self.netflix_session.switch_profile(profile_id=profile_id, account=self.credentials)
306 def get_user_data (self, params):
307 """User data getter function
311 params : :obj:`dict` of :obj:`str`
319 return self.netflix_session.user_data
321 def search (self, params):
322 """Search proxy function
326 params : :obj:`dict` of :obj:`str`
332 Transformed response of the remote call
334 term = params.get('term', [''])[0]
335 has_search_results = False
336 raw_search_results = self.netflix_session.fetch_search_results(search_str=term)
337 # check for any errors
338 if 'error' in raw_search_results:
339 return raw_search_results
341 # determine if we found something
342 if 'search' in raw_search_results['value']:
343 for key in raw_search_results['value']['search'].keys():
344 if self.netflix_session._is_size_key(key=key) == False:
345 has_search_results = raw_search_results['value']['search'][key]['titles']['length'] > 0
346 if has_search_results == False:
347 if raw_search_results['value']['search'][key].get('suggestions', False) != False:
348 for entry in raw_search_results['value']['search'][key]['suggestions']:
349 if self.netflix_session._is_size_key(key=entry) == False:
350 if raw_search_results['value']['search'][key]['suggestions'][entry]['relatedvideos']['length'] > 0:
351 has_search_results = True
353 # display that we haven't found a thing
354 if has_search_results == False:
357 # list the search results
358 search_results = self.netflix_session.parse_search_results(response_data=raw_search_results)
359 # add more menaingful data to the search results
360 raw_search_contents = self.netflix_session.fetch_video_list_information(video_ids=search_results.keys())
361 # check for any errors
362 if 'error' in raw_search_contents:
363 return raw_search_contents
364 return self.netflix_session.parse_video_list(response_data=raw_search_contents)
366 def _network_availble(self):
367 """Check if the network is available
371 Network can be accessed
374 urlopen('http://216.58.192.142', timeout=1)
376 except URLError as err: