Esempio n. 1
0
    def _request(self, method, url, lang=None, retries=3, **kwargs):
        self.config['headers'].update({'Content-type': 'application/json'})
        self.config['headers']['Authorization'] = 'Bearer {}'.format(self.jwt_token)
        self.config['headers'].update({'Accept-Language': lang or self.config['language']})
        self.config['headers'].update({'Accept': 'application/vnd.thetvdb.v{}'.format(self.config['api']['version'])})

        for i in range(0, retries):
            try:
                # get response from theTVDB
                resp = WebSession(cache=self.config['cache_enabled']).request(
                    method, urljoin(self.config['api']['base'], url), headers=self.config['headers'],
                    timeout=sickrage.app.config.indexer_timeout, **kwargs
                )
            except requests.exceptions.HTTPError as e:
                status_code = e.response.status_code
                error_message = e.response.text

                if 'application/json' in e.response.headers.get('content-type', ''):
                    error_message = e.response.json().get('Error', error_message)

                if status_code == 401:
                    raise tvdb_unauthorized(error_message)

                if i < retries - 1:
                    continue

                raise tvdb_error(error_message)

            return to_lowercase(resp.json())
Esempio n. 2
0
    def _request(self, method, url, lang=None, **kwargs):
        self.config['headers'].update({'Content-type': 'application/json'})
        self.config['headers']['Authorization'] = 'Bearer {}'.format(
            self.jwt_token)
        self.config['headers'].update(
            {'Accept-Language': lang or self.config['language']})

        # get response from theTVDB
        try:
            resp = WebSession(cache=self.config['cache_enabled']).request(
                method,
                urlparse.urljoin(self.config['api']['base'], url),
                headers=self.config['headers'],
                timeout=sickrage.app.config.indexer_timeout,
                **kwargs)
        except Exception as e:
            raise tvdb_error(str(e))

        # handle requests exceptions
        try:
            if resp.status_code == 401:
                raise tvdb_unauthorized(resp.json()['Error'])
            elif resp.status_code >= 400:
                raise tvdb_error(resp.json()['Error'])
        except JSONDecodeError:
            try:
                resp.raise_for_status()
            except RequestException as e:
                raise tvdb_error(str(e))

        return to_lowercase(resp.json())
Esempio n. 3
0
    def _request(self, method, url, lang=None, **kwargs):
        self.config['headers'].update({'Content-type': 'application/json'})
        self.config['headers']['Authorization'] = 'Bearer {}'.format(self.jwt_token)
        self.config['headers'].update({'Accept-Language': lang or self.config['language']})

        # get response from theTVDB
        try:
            resp = WebSession(cache=self.config['cache_enabled']).request(
                method, urlparse.urljoin(self.config['api']['base'], url), headers=self.config['headers'],
                timeout=sickrage.app.config.indexer_timeout, **kwargs
            )
        except Exception as e:
            raise tvdb_error(str(e))

        # handle requests exceptions
        try:
            if resp.status_code == 401:
                raise tvdb_unauthorized(resp.json()['Error'])
            elif resp.status_code >= 400:
                raise tvdb_error(resp.json()['Error'])
        except JSONDecodeError:
            try:
                resp.raise_for_status()
            except RequestException as e:
                raise tvdb_error(str(e))

        return to_lowercase(resp.json())
Esempio n. 4
0
    def _request(self, method, url, **kwargs):
        self.config['headers'].update({'Content-type': 'application/json'})

        if self.config['apitoken']:
            self.config['headers']['authorization'] = 'Bearer {}'.format(
                self.config['apitoken'])
        if self.config['language']:
            self.config['headers'].update(
                {'Accept-Language': self.config['language']})

        # get response from theTVDB
        try:
            resp = sickrage.srCore.srWebSession.request(
                method,
                urlparse.urljoin(self.config['api']['base'], url),
                cache=self.config['cache_enabled'],
                headers=self.config['headers'],
                timeout=sickrage.srCore.srConfig.INDEXER_TIMEOUT,
                **kwargs)
        except Exception as e:
            raise tvdb_error(e.message)

        # handle requests exceptions
        if resp.status_code == 401:
            raise tvdb_unauthorized(resp.json()['Error'])
        elif resp.status_code >= 400:
            raise tvdb_error(resp.json()['Error'])

        return to_lowercase(resp.json())
Esempio n. 5
0
    def _request(self, method, url, lang=None, **kwargs):
        self.config['headers'].update(
            {'Authorization': 'Bearer {}'.format(self.jwt_token)})
        self.config['headers'].update({'Content-type': 'application/json'})
        self.config['headers'].update(
            {'Accept-Language': lang or self.config['language']})
        self.config['headers'].update({
            'Accept':
            'application/vnd.thetvdb.v{}'.format(self.config['api']['version'])
        })

        if not self.health:
            return None

        resp = WebSession(cache=self.config['cache_enabled']).request(
            method,
            urljoin(self.config['api']['base'], url),
            headers=self.config['headers'],
            timeout=sickrage.app.config.indexer_timeout,
            verify=False,
            **kwargs)

        if resp and resp.content:
            try:
                data = resp.json()
            except ValueError:
                sickrage.app.log.debug("Unable to parse data from TheTVDB")
                return None

            return to_lowercase(data)

        if resp is not None:
            if resp.status_code == 401:
                raise tvdb_unauthorized(resp.text)
            elif resp.status_code == 504:
                sickrage.app.log.debug("Unable to connect to TheTVDB")
                return None

            if 'application/json' in resp.headers.get('content-type', ''):
                err_msg = resp.json().get('Error', resp.text)
                sickrage.app.log.debug(
                    "Unable to get data from TheTVDB, Code: {code} Error: {err_msg!r}"
                    .format(code=resp.status_code, err_msg=err_msg))
                return None