Пример #1
0
    def get(self, page):
        response = self.fetch(page)

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code,
                                    ("Unknown", "Unknown"))

            log.warning('Request failed: %s - "%s" (code: %s)', name, desc,
                        response.status_code)
            return None

        # Parse response, return data
        content_type = response.headers.get('content-type')

        if content_type and content_type.startswith('application/json'):
            # Try parse json response
            try:
                data = response.json()
            except Exception as e:
                log.warning('Unable to parse page: %s', e)
                return None
        else:
            log.warning('Received a page with an invalid content type: %r',
                        content_type)
            return None

        return data
Пример #2
0
    def get(self, page):
        response = self.fetch(page)

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code, ("Unknown", "Unknown"))

            log.warning('Request failed: %s - "%s" (code: %s)', name, desc, response.status_code)
            return None

        # Parse response, return data
        content_type = response.headers.get('content-type')

        if content_type and content_type.startswith('application/json'):
            # Try parse json response
            try:
                data = response.json()
            except Exception as e:
                log.warning('Unable to parse page: %s', e)
                return None
        else:
            log.warning('Received a page with an invalid content type: %r', content_type)
            return None

        return data
Пример #3
0
    def __init__(self, response):
        self.response = response
        self.status_code = response.status_code if response is not None else None

        self.error = ERRORS.get(self.status_code, ("Unknown", "Unknown"))

        # Call super class with message
        super(RequestError, self).__init__('%s - "%s"' % self.error)
Пример #4
0
    def get_data(self, response, exceptions=False, pagination=False, parse=True):
        if response is None:
            return None

        # Return response, if parse=False
        if not parse:
            return response

        # Check status code, log any errors
        error = False

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code, ("Unknown", "Unknown"))

            log.warning('Request failed: %s - "%s" (code: %s)', name, desc, response.status_code)

            if exceptions:
                # Raise an exception (including the response for further processing)
                if response.status_code >= 500:
                    raise ServerError(response)
                else:
                    raise ClientError(response)

            # Set error flag
            error = True

        # Return `None` if we encountered an error, return response data
        if error:
            return None

        # Check for pagination response
        page_count = try_convert(response.headers.get('x-pagination-page-count'), int)

        if page_count and page_count > 1:
            if pagination:
                return PaginationIterator(self.client, response)

            warnings.warn('Unhandled pagination response, more pages can be returned with `pagination=True`', stacklevel=3)

        # Parse response, return data
        content_type = response.headers.get('content-type')

        if content_type and content_type.startswith('application/json'):
            # Try parse json response
            try:
                data = response.json()
            except Exception as e:
                log.warning('unable to parse JSON response: %s', e)
                return None
        else:
            log.debug('response returned content-type: %r, falling back to raw data', content_type)

            # Fallback to raw content
            data = response.content

        return data
Пример #5
0
    def get_data(response, exceptions=False, parse=True):
        if response is None:
            return None

        # Return response, if parse=False
        if not parse:
            return response

        # Parse response, return data
        content_type = response.headers.get('content-type')

        if content_type and content_type.startswith('application/json'):
            # Try parse json response
            try:
                data = response.json()
            except Exception as e:
                log.warning('unable to parse JSON response: %s', e)
                return None
        else:
            log.debug(
                'response returned content-type: %r, falling back to raw data',
                content_type)

            # Fallback to raw content
            data = response.content

        # Check status code, log any errors
        error = False

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code,
                                    ("Unknown", "Unknown"))

            log.warning('request failed: %s - "%s" (code: %s)', name, desc,
                        response.status_code)

            if exceptions:
                # Raise an exception (including the response for further processing)
                if response.status_code >= 500:
                    raise ServerError(response)
                else:
                    raise ClientError(response)

            # Set error flag
            error = True

        # Return `None` if we encountered an error, return response data
        if error:
            return None

        return data
Пример #6
0
    def get_data(response, exceptions=False, parse=True):
        if response is None:
            return None

        # Return response, if parse=False
        if not parse:
            return response

        # Check status code, log any errors
        error = False

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code, ("Unknown", "Unknown"))

            log.warning('request failed: %s - "%s" (code: %s)', name, desc, response.status_code)

            if exceptions:
                # Raise an exception (including the response for further processing)
                if response.status_code >= 500:
                    raise ServerError(response)
                else:
                    raise ClientError(response)

            # Set error flag
            error = True

        # Return `None` if we encountered an error, return response data
        if error:
            return None

        # Parse response, return data
        content_type = response.headers.get("content-type")

        if content_type and content_type.startswith("application/json"):
            # Try parse json response
            try:
                data = response.json()
            except Exception as e:
                log.warning("unable to parse JSON response: %s", e)
                return None
        else:
            log.debug("response returned content-type: %r, falling back to raw data", content_type)

            # Fallback to raw content
            data = response.content

        return data
Пример #7
0
                log.warning('unable to parse JSON response: %s', e)
                return None
        else:
            log.debug(
                'response returned "%s" content, falling back to raw data',
                response.headers['content-type'])

            # Fallback to raw content
            data = response.content

        # Check status code, log any errors
        error = False

        if response.status_code < 200 or response.status_code >= 300:
            # Lookup status code in trakt error definitions
            name, desc = ERRORS.get(response.status_code,
                                    ("Unknown", "Unknown"))

            log.warning('request failed: %s - "%s" (code: %s)', name, desc,
                        response.status_code)

            # Set error flag
            error = True

        # Return `None` if we encountered an error, return response data
        if error:
            return None

        return data

    @staticmethod
    def media_mapper(store, media, items, **kwargs):