X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=resources%2Flib%2FNetflixHttpSubRessourceHandler.py;h=b1bad7a518d5534e8159ef2b9b9135121acb473e;hb=4992e07aa489c7b2da627ab57769c4b0b03c6e62;hp=4e7e1dd0b2659b399edb319b3fac80009d546335;hpb=dfc7738380f5ce833063ce74e2c0ab9b82b84eae;p=plugin.video.netflix.git diff --git a/resources/lib/NetflixHttpSubRessourceHandler.py b/resources/lib/NetflixHttpSubRessourceHandler.py index 4e7e1dd..b1bad7a 100644 --- a/resources/lib/NetflixHttpSubRessourceHandler.py +++ b/resources/lib/NetflixHttpSubRessourceHandler.py @@ -3,6 +3,9 @@ # Module: NetflixHttpSubRessourceHandler # Created on: 07.03.2017 +from urllib2 import urlopen, URLError +from time import sleep + class NetflixHttpSubRessourceHandler: """ Represents the callable internal server routes & translates/executes them to requests for Netflix""" @@ -22,19 +25,29 @@ class NetflixHttpSubRessourceHandler: self.kodi_helper = kodi_helper self.netflix_session = netflix_session self.credentials = self.kodi_helper.get_credentials() + self.profiles = [] + self.prefetch_login() self.video_list_cache = {} self.lolomo = None - # check if we have stored credentials, if so, do the login before the user requests it - # if that is done, we cache the profiles - if self.credentials['email'] != '' and self.credentials['password'] != '': - if self.netflix_session.is_logged_in(account=self.credentials): - self.netflix_session.refresh_session_data(account=self.credentials) + def prefetch_login (self): + """Check if we have stored credentials. + If so, do the login before the user requests it + If that is done, we cache the profiles + """ + if self._network_availble(): + if self.credentials['email'] != '' and self.credentials['password'] != '': + if self.netflix_session.is_logged_in(account=self.credentials): + self.netflix_session.refresh_session_data(account=self.credentials) + self.profiles = self.netflix_session.profiles + else: + self.netflix_session.login(account=self.credentials) + self.profiles = self.netflix_session.profiles else: - self.netflix_session.login(account=self.credentials) - self.profiles = self.netflix_session.profiles + self.profiles = [] else: - self.profiles = [] + sleep(1) + self.prefetch_login() def is_logged_in (self, params): """Existing login proxy function @@ -141,28 +154,11 @@ class NetflixHttpSubRessourceHandler: self.kodi_helper.log('Serving cached list for user: ' + self.netflix_session.user_data['guid']) return cached_list video_list_ids_raw = self.netflix_session.fetch_video_list_ids() + if 'error' in video_list_ids_raw: return video_list_ids_raw return self.netflix_session.parse_video_list_ids(response_data=video_list_ids_raw) - def fetch_video_list_ids_for_kids (self, params): - """Video list ids proxy function (thanks to Netflix that we need to use a different API for Kids profiles) - - Parameters - ---------- - params : :obj:`dict` of :obj:`str` - Request params - - Returns - ------- - :obj:`list` - Transformed response of the remote call - """ - if self.lolomo == None: - self.lolomo = self.netflix_session.get_lolomo_for_kids() - response = self.netflix_session.fetch_lists_for_kids(lolomo=self.lolomo) - return response - def fetch_video_list (self, params): """Video list proxy function @@ -366,3 +362,16 @@ class NetflixHttpSubRessourceHandler: if 'error' in raw_search_contents: return raw_search_contents return self.netflix_session.parse_video_list(response_data=raw_search_contents) + + def _network_availble(self): + """Check if the network is available + Returns + ------- + bool + Network can be accessed + """ + try: + urlopen('http://216.58.192.142', timeout=1) + return True + except URLError as err: + return False