예제 #1
0
    def traktRequest(self, path, data=None, headers=None, url=None, method='GET',count=0):
        if None == url:
            url = self.api_url + path
        else:
            url = url + path
        
        count = count + 1
        
        if None == headers:
            headers = self.headers
        
        if None == sickbeard.TRAKT_ACCESS_TOKEN:
            logger.log(u'You must get a Trakt TOKEN. Check your Trakt settings', logger.WARNING)
            raise traktAuthException(e)
            
        headers['Authorization'] = 'Bearer ' + sickbeard.TRAKT_ACCESS_TOKEN

        try:
            resp = requests.request(method, url, headers=headers, timeout=self.timeout,
                data=json.dumps(data) if data else [], verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except requests.RequestException as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u'Retrying trakt api request: %s' % path, logger.WARNING)
                return self.traktRequest(path, data, headers, method)
            elif code == 401:
                logger.log(u'Unauthorized. Please check your Trakt settings', logger.WARNING)
                if self.traktToken(refresh=True,count=count):
                    return self.traktRequest(path, data, url, method)
                raise traktAuthException(e)
            elif code in (500,501,503,504,520,521,522):
                #http://docs.trakt.apiary.io/#introduction/status-codes
                logger.log(u'Trakt may have some issues and it\'s unavailable. Try again later please', logger.WARNING)
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #2
0
    def traktRequest(self, url, data=None):
        base_url = self.protocol + 'api.trakt.tv/%s' % url.replace('%APIKEY%', self.apikey).replace('%USER%',
                                                                                                    self.username)

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.get(base_url,
                                auth=HTTPBasicAuth(self.username, self.password),
                                data=data if data else [])

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except (requests.HTTPError, requests.ConnectionError) as e:
            if e.response.status_code == 401:
                raise traktAuthException(e)
            elif e.response.status_code == 503:
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #3
0
 def validateAccount(self):
     if hasattr(self, 'token'):
         del(self.token)
     data = {
         'login': self.username,
         'password': self.password
     }
     try:
         resp = requests.request('POST', self.api_url+"auth/login", headers=self.headers,
             data=json.dumps(data), timeout=self.timeout, verify=self.verify)
         resp.raise_for_status()
         resp = resp.json()
     except requests.RequestException as e:
         code = getattr(e.response, 'status_code', None)
         if not code:
             # This is pretty much a fatal error if there is no status_code
             # It means there basically was no response at all
             raise traktException(e)
         elif code == 502:
             # Retry the request, cloudflare had a proxying issue
             logger.log(u"Retrying trakt api request: auth/login", logger.WARNING)
             return self.validateAccount()
         elif code == 401:
             logger.log(u"Unauthorized. Please check your Trakt settings", logger.WARNING)
             raise traktAuthException(e)
         elif code in (500,501,503,504,520,521,522):
             #http://docs.trakt.apiary.io/#introduction/status-codes
             logger.log(u"Trakt may have some issues and it's unavailable. Try again later please", logger.WARNING)
             raise traktServerBusy(e)
         else:
             raise traktException(e)
     if 'token' in resp:
         self.token = resp['token']
         return True
     return False
예제 #4
0
 def validateAccount(self):
     if hasattr(self, 'token'):
         del(self.token)
     data = {
         'login': self.username,
         'password': self.password
     }
     try:
         resp = requests.request('POST', self.api_url+"auth/login", headers=self.headers,
             data=json.dumps(data), timeout=self.timeout, verify=self.verify)
         resp.raise_for_status()
         resp = resp.json()
     except (requests.HTTPError, requests.ConnectionError) as e:
         code = getattr(e.response, 'status_code', None)
         if not code:
             # This is pretty much a fatal error if there is no status_code
             # It means there basically was no response at all
             raise traktException(e)
         elif code == 502:
             # Retry the request, cloudflare had a proxying issue
             logger.log(u"Retrying trakt api request: auth/login", logger.WARNING)
             return self.validateAccount()
         elif code == 401:
             raise traktAuthException(e)
         elif code == 503:
             raise traktServerBusy(e)
         else:
             raise traktException(e)
     if 'token' in resp:
         self.token = resp['token']
         return True
     return False
예제 #5
0
    def traktRequest(self, url, data=None, method='GET'):
        base_url = self.protocol + 'api.trakt.tv/%s' % url.replace(
            '%APIKEY%', self.apikey).replace('%USER%', self.username)

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.request(method,
                                    base_url,
                                    auth=HTTPBasicAuth(self.username,
                                                       self.password),
                                    data=data if data else [])

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except (requests.HTTPError, requests.ConnectionError) as e:
            if e.response.status_code == 401:
                raise traktAuthException(e)
            elif e.response.status_code == 503:
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #6
0
    def trakt_request(self, path, data=None, headers=None, url=None, method='GET', count=0):

        if None is sickbeard.TRAKT_TOKEN:
            logger.log(u'You must get a Trakt token. Check your Trakt settings', logger.WARNING)
            return {}

        headers = headers or self.headers
        url = url or self.api_url
        count += 1

        headers['Authorization'] = 'Bearer ' + sickbeard.TRAKT_TOKEN

        try:
            resp = self.session.request(method, url + path, headers=headers, timeout=self.timeout,
                                        data=json.dumps(data) if data else [], verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except requests.RequestException as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                if 'timed out' in e:
                    logger.log(u'Timeout connecting to Trakt. Try to increase timeout value in Trakt settings', logger.WARNING)                      
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all                    
                else:
                    logger.log(u'Could not connect to Trakt. Error: {0}'.format(e), logger.WARNING)                
            elif 502 == code:
                # Retry the request, Cloudflare had a proxying issue
                logger.log(u'Retrying trakt api request: %s' % path, logger.WARNING)
                return self.trakt_request(path, data, headers, url, method, count=count)
            elif 401 == code:
                if self.trakt_token(refresh=True, count=count):
                    sickbeard.save_config()
                    return self.trakt_request(path, data, headers, url, method, count=count)
                else:
                    logger.log(u'Unauthorized. Please check your Trakt settings', logger.WARNING)
                    raise traktAuthException()
            elif code in (500, 501, 503, 504, 520, 521, 522):
                # http://docs.trakt.apiary.io/#introduction/status-codes
                logger.log(u'Trakt may have some issues and it\'s unavailable. Try again later please', logger.WARNING)
            elif 404 == code:
                logger.log(u'Trakt error (404) the resource does not exist: %s' % url + path, logger.WARNING)
            else:
                logger.log(u'Could not connect to Trakt. Code error: {0}'.format(code), logger.ERROR)
            return {}

        # check and confirm Trakt call did not fail
        if isinstance(resp, dict) and 'failure' == resp.get('status', None):
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #7
0
 def validateAccount(self):
     if hasattr(self, 'token'):
         del (self.token)
     data = {'login': self.username, 'password': self.password}
     try:
         resp = requests.request('POST',
                                 self.api_url + "auth/login",
                                 headers=self.headers,
                                 data=json.dumps(data),
                                 timeout=self.timeout,
                                 verify=self.verify)
         resp.raise_for_status()
         resp = resp.json()
     except (requests.HTTPError, requests.ConnectionError) as e:
         code = getattr(e.response, 'status_code', None)
         if not code:
             # This is pretty much a fatal error if there is no status_code
             # It means there basically was no response at all
             raise traktException(e)
         elif code == 502:
             # Retry the request, cloudflare had a proxying issue
             logger.log(u"Retrying trakt api request: auth/login",
                        logger.WARNING)
             return self.validateAccount()
         elif code == 401:
             raise traktAuthException(e)
         elif code == 503:
             raise traktServerBusy(e)
         else:
             raise traktException(e)
     if 'token' in resp:
         self.token = resp['token']
         return True
     return False
예제 #8
0
    def traktRequest(self, path, data=None, method='GET'):
        url = self.api_url + path
        headers = self.headers
        if not getattr(self, 'token', None):
            self.validateAccount()
        headers['trakt-user-login'] = self.username
        headers['trakt-user-token'] = self.token

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.request(method,
                                    url,
                                    headers=headers,
                                    timeout=self.timeout,
                                    data=json.dumps(data) if data else [],
                                    verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except requests.RequestException as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u"Retrying trakt api request: %s" % path,
                           logger.WARNING)
                return self.traktRequest(path, data, method)
            elif code == 401:
                logger.log(u"Unauthorized. Please check your Trakt settings",
                           logger.WARNING)
                raise traktAuthException(e)
            elif code in (500, 501, 503, 504, 520, 521, 522):
                #http://docs.trakt.apiary.io/#introduction/status-codes
                logger.log(
                    u"Trakt may have some issues and it's unavailable. Try again later please",
                    logger.WARNING)
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #9
0
    def traktRequest(self, path, data=None, method='GET'):
        url = self.api_url + path
        headers = self.headers
        if not getattr(self, 'token', None):
            self.validateAccount()
        headers['trakt-user-login'] = self.username
        headers['trakt-user-token'] = self.token

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.request(method,
                                    url,
                                    headers=headers,
                                    timeout=self.timeout,
                                    data=json.dumps(data) if data else [],
                                    verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except (requests.HTTPError, requests.ConnectionError) as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u"Retrying trakt api request: %s" % path,
                           logger.WARNING)
                return self.traktRequest(path, data, method)
            elif code == 401:
                raise traktAuthException(e)
            elif code == 503:
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #10
0
    def traktRequest(self, path, data=None, method='GET'):
        url = self.api_url + path
        headers = self.headers
        if not getattr(self, 'token', None):
            self.validateAccount()
        headers['trakt-user-login'] = self.username
        headers['trakt-user-token'] = self.token

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.request(method, url, headers=headers, timeout=self.timeout,
                data=json.dumps(data) if data else [], verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except requests.RequestException as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u"Retrying trakt api request: %s" % path, logger.WARNING)
                return self.traktRequest(path, data, method)
            elif code == 401:
                logger.log(u"Unauthorized. Please check your Trakt settings", logger.WARNING)
                raise traktAuthException(e)
            elif code in (500,501,503,504,520,521,522):
                #http://docs.trakt.apiary.io/#introduction/status-codes
                logger.log(u"Trakt may have some issues and it's unavailable. Try again later please", logger.WARNING)
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #11
0
    def traktRequest(self, path, data=None, method='GET'):
        url = self.api_url + path
        headers = self.headers
        if not getattr(self, 'token', None):
            self.validateAccount()
        headers['trakt-user-login'] = self.username
        headers['trakt-user-token'] = self.token

        # request the URL from trakt and parse the result as json
        try:
            resp = requests.request(method, url, headers=headers, timeout=self.timeout,
                data=json.dumps(data) if data else [], verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except (requests.HTTPError, requests.ConnectionError) as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u"Retrying trakt api request: %s" % path, logger.WARNING)
                return self.traktRequest(path, data, method)
            elif code == 401:
                raise traktAuthException(e)
            elif code == 503:
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp
예제 #12
0
 def validateAccount(self):
     if hasattr(self, 'token'):
         del (self.token)
     data = {'login': self.username, 'password': self.password}
     try:
         resp = requests.request('POST',
                                 self.api_url + "auth/login",
                                 headers=self.headers,
                                 data=json.dumps(data),
                                 timeout=self.timeout,
                                 verify=self.verify)
         resp.raise_for_status()
         resp = resp.json()
     except requests.RequestException as e:
         code = getattr(e.response, 'status_code', None)
         if not code:
             # This is pretty much a fatal error if there is no status_code
             # It means there basically was no response at all
             raise traktException(e)
         elif code == 502:
             # Retry the request, cloudflare had a proxying issue
             logger.log(u"Retrying trakt api request: auth/login",
                        logger.WARNING)
             return self.validateAccount()
         elif code == 401:
             logger.log(u"Unauthorized. Please check your Trakt settings",
                        logger.WARNING)
             raise traktAuthException(e)
         elif code in (500, 501, 503, 504, 520, 521, 522):
             #http://docs.trakt.apiary.io/#introduction/status-codes
             logger.log(
                 u"Trakt may have some issues and it's unavailable. Try again later please",
                 logger.WARNING)
             raise traktServerBusy(e)
         else:
             raise traktException(e)
     if 'token' in resp:
         self.token = resp['token']
         return True
     return False
예제 #13
0
파일: trakt.py 프로젝트: lhmiranda/SickRage
    def traktRequest(self,
                     path,
                     data=None,
                     headers=None,
                     url=None,
                     method='GET',
                     count=0):
        if None == url:
            url = self.api_url + path
        else:
            url = url + path

        count = count + 1

        if None == headers:
            headers = self.headers

        if None == sickbeard.TRAKT_ACCESS_TOKEN:
            logger.log(
                u'You must get a Trakt TOKEN. Check your Trakt settings',
                logger.WARNING)
            raise traktAuthException(e)

        headers['Authorization'] = 'Bearer ' + sickbeard.TRAKT_ACCESS_TOKEN

        try:
            resp = requests.request(method,
                                    url,
                                    headers=headers,
                                    timeout=self.timeout,
                                    data=json.dumps(data) if data else [],
                                    verify=self.verify)

            # check for http errors and raise if any are present
            resp.raise_for_status()

            # convert response to json
            resp = resp.json()
        except requests.RequestException as e:
            code = getattr(e.response, 'status_code', None)
            if not code:
                # This is pretty much a fatal error if there is no status_code
                # It means there basically was no response at all
                raise traktException(e)
            elif code == 502:
                # Retry the request, cloudflare had a proxying issue
                logger.log(u'Retrying trakt api request: %s' % path,
                           logger.WARNING)
                return self.traktRequest(path, data, headers, method)
            elif code == 401:
                logger.log(u'Unauthorized. Please check your Trakt settings',
                           logger.WARNING)
                if self.traktToken(refresh=True, count=count):
                    return self.traktRequest(path, data, url, method)
                raise traktAuthException(e)
            elif code in (500, 501, 503, 504, 520, 521, 522):
                #http://docs.trakt.apiary.io/#introduction/status-codes
                logger.log(
                    u'Trakt may have some issues and it\'s unavailable. Try again later please',
                    logger.WARNING)
                raise traktServerBusy(e)
            else:
                raise traktException(e)

        # check and confirm trakt call did not fail
        if isinstance(resp, dict) and resp.get('status', False) == 'failure':
            if 'message' in resp:
                raise traktException(resp['message'])
            if 'error' in resp:
                raise traktException(resp['error'])
            else:
                raise traktException('Unknown Error')

        return resp