コード例 #1
0
 def timeout_error(self, err):
     """ Log timeout error
     If two timeouts occur in x3 the timeout limit then set connection error
     e.g. if timeout limit is 10s then two timeouts within 30s trigger connection error
     """
     kodi_log(u'ConnectionTimeOut: {}'.format(err), 1)
     if get_timestamp(self.req_timeout_err):
         self.connection_error(err, msg_affix='timeout')
     self.req_timeout_err = set_timestamp(self.timeout * 3)
     get_property(self.req_timeout_err_prop, self.req_timeout_err)
コード例 #2
0
    def get_api_request(self, request=None, postdata=None, headers=None):
        """
        Make the request to the API by passing a url request string
        """
        # Connection error in last minute for this api so don't keep trying
        if get_timestamp(self.req_connect_err):
            return
        if get_timestamp(self.req_500_err.get(request)):
            return

        # Get response
        response = self.get_simple_api_request(request, postdata, headers)
        if response is None or not response.status_code:
            return

        # Some error checking
        if not response.status_code == requests.codes.ok and try_int(
                response.status_code) >= 400:  # Error Checking
            # 500 code is server error which usually indicates Trakt is down
            # In this case let's set a connection error and suppress retries for a minute
            if response.status_code == 500:
                self.fivehundred_error(request)
            # 429 is too many requests code so suppress retries for a minute
            elif response.status_code == 429:
                self.connection_error(429)
            # Don't write 400 Bad Request error to log
            # 401 == OAuth / API key required
            elif try_int(response.status_code) > 400:
                log_level = 2 if try_int(response.status_code) in [404] else 1
                kodi_log([
                    u'HTTP Error Code: {}'.format(
                        response.status_code), u'\nRequest: {}'.format(
                            request.replace(self.req_api_key, ''
                                            ) if request else None),
                    u'\nPostdata: {}'.format(postdata) if postdata else '',
                    u'\nHeaders: {}'.format(headers) if headers else '',
                    u'\nResponse: {}'.format(response) if response else ''
                ], log_level)
            return

        # Return our response
        return response
コード例 #3
0
    def authorize(self, login=False):
        # Already got authorization so return credentials
        if self.authorization:
            return self.authorization

        # Get our saved credentials from previous login
        token = self.get_stored_token()
        if token.get('access_token'):
            self.authorization = token
            self.headers['Authorization'] = u'Bearer {0}'.format(
                self.authorization.get('access_token'))

        # No saved credentials and user trying to use a feature that requires authorization so ask them to login
        elif login:
            if not self.attempted_login and xbmcgui.Dialog().yesno(
                    self.dialog_noapikey_header,
                    self.dialog_noapikey_text,
                    nolabel=xbmc.getLocalizedString(222),
                    yeslabel=xbmc.getLocalizedString(186)):
                self.login()
            self.attempted_login = True

        # First time authorization in this session so let's confirm
        if self.authorization and get_property('TraktIsAuth') != 'True':
            if not get_timestamp(
                    get_property('TraktRefreshTimeStamp', is_type=float) or 0):
                # Check if we can get a response from user account
                kodi_log('Checking Trakt authorization', 2)
                response = self.get_simple_api_request(
                    'https://api.trakt.tv/sync/last_activities',
                    headers=self.headers)
                # 401 is unauthorized error code so let's try refreshing the token
                if not response or response.status_code == 401:
                    kodi_log('Trakt unauthorized!', 2)
                    self.authorization = self.refresh_token()
                # Authorization confirmed so let's set a window property for future reference in this session
                if self.authorization:
                    kodi_log('Trakt user account authorized', 1)
                    get_property('TraktIsAuth', 'True')

        return self.authorization
コード例 #4
0
 def timeout_error(self, err):
     """ Log timeout error - if two in one minute set connection error """
     if get_timestamp(self.req_timeout_err):
         self.connection_error(err)
     self.req_timeout_err = set_timestamp()
     get_property(self.req_timeout_err_prop, self.req_timeout_err)