Python brotli.decompress() Examples

The following are 28 code examples of brotli.decompress(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module brotli , or try the search function .
Example #1
Source File: __init__.py    From addon with GNU General Public License v3.0 6 votes vote down vote up
def decodeBrotli(self, resp):
        if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
            if self.allow_brotli and resp._content:
                resp._content = brotli.decompress(resp.content)
            else:
                logging.warning(
                    'You\'re running urllib3 {}, Brotli content detected, '
                    'Which requires manual decompression, '
                    'But option allow_brotli is set to False, '
                    'We will not continue to decompress.'.format(requests.packages.urllib3.__version__)
                )

        return resp

    # ------------------------------------------------------------------------------- #
    # Our hijacker request function
    # ------------------------------------------------------------------------------- # 
Example #2
Source File: test_simple_compression.py    From brotlipy with MIT License 6 votes vote down vote up
def test_streaming_compression(one_compressed_file,
                               chunk_size,
                               mode,
                               quality,
                               lgwin,
                               lgblock):
    """
    Confirm that the streaming compressor works as expected.
    """
    compressed_chunks = []
    c = brotli.Compressor(
        mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock
    )
    with open(one_compressed_file, 'rb') as f:
        while True:
            next_data = f.read(chunk_size)
            if not next_data:
                break

            compressed_chunks.append(c.compress(next_data))

    compressed_chunks.append(c.finish())
    decompressed = brotli.decompress(b''.join(compressed_chunks))
    with open(one_compressed_file, 'rb') as f:
        assert decompressed == f.read() 
Example #3
Source File: test_simple_decompression.py    From brotlipy with MIT License 6 votes vote down vote up
def test_drip_feed(simple_compressed_file):
    """
    Sending in the data one byte at a time still works.
    """
    with open(simple_compressed_file[0], 'rb') as f:
        uncompressed_data = f.read()

    with open(simple_compressed_file[1], 'rb') as f:
        compressed_data = f.read()

    outdata = []
    o = brotli.Decompressor()
    for i in range(0, len(compressed_data)):
        outdata.append(o.decompress(compressed_data[i:i+1]))

    outdata.append(o.flush())
    outdata.append(o.finish())

    assert b''.join(outdata) == uncompressed_data 
Example #4
Source File: guardLottery.py    From bilibili-live-tools with MIT License 6 votes vote down vote up
def guard_lottery(self):
        for k in range(1):
            try:
                response = await bilibili().guard_list()
                json_response = response.json()
                break
            except Exception:
                continue
        else:
            Printer().printer("连接舰长服务器失败,尝试从CDN拉取数据", "Error", "red")
            response = await bilibili().guard_list_v2()
            encrypt_content = response.json()['data']['room_info']['description'].replace('b6','').replace('</p>','').replace('<p>','')
            json_response = json.loads(brotli.decompress(encrypt.decrypt(encrypt_content)).decode())
        for i in range(0, len(json_response)):
            GuardId = json_response[i]['Id']
            if GuardId not in GuardLottery.had_gotted_guard and GuardId != 0:
                GuardLottery.had_gotted_guard.append(GuardId)
                OriginRoomId = json_response[i]['RoomId']
                await self.guard_join(OriginRoomId, GuardId)
                await asyncio.sleep(0.2) 
Example #5
Source File: __init__.py    From script.module.openscrapers with GNU General Public License v3.0 6 votes vote down vote up
def decodeBrotli(self, resp):
        if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
            if self.allow_brotli and resp._content:
                resp._content = brotli.decompress(resp.content)
            else:
                logging.warning(
                    'You\'re running urllib3 {}, Brotli content detected, '
                    'Which requires manual decompression, '
                    'But option allow_brotli is set to False, '
                    'We will not continue to decompress.'.format(requests.packages.urllib3.__version__)
                )

        return resp

    # ------------------------------------------------------------------------------- #
    # Our hijacker request function
    # ------------------------------------------------------------------------------- # 
Example #6
Source File: woff2.py    From Emoji-Tools with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, reader=None):
		if not haveBrotli:
			raise ImportError("No module named brotli")
		self.majorVersion = None
		self.minorVersion = None
		self.metaData = None
		self.privData = None
		if reader:
			self.majorVersion = reader.majorVersion
			self.minorVersion = reader.minorVersion
			if reader.metaLength:
				reader.file.seek(reader.metaOffset)
				rawData = reader.file.read(reader.metaLength)
				assert len(rawData) == reader.metaLength
				data = brotli.decompress(rawData)
				assert len(data) == reader.metaOrigLength
				self.metaData = data
			if reader.privLength:
				reader.file.seek(reader.privOffset)
				data = reader.file.read(reader.privLength)
				assert len(data) == reader.privLength
				self.privData = data 
Example #7
Source File: httpcompression.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _decode(self, body, encoding):
        if encoding == b'gzip' or encoding == b'x-gzip':
            body = gunzip(body)

        if encoding == b'deflate':
            try:
                body = zlib.decompress(body)
            except zlib.error:
                # ugly hack to work with raw deflate content that may
                # be sent by microsoft servers. For more information, see:
                # http://carsten.codimi.de/gzip.yaws/
                # http://www.port80software.com/200ok/archive/2005/10/31/868.aspx
                # http://www.gzip.org/zlib/zlib_faq.html#faq38
                body = zlib.decompress(body, -15)
        if encoding == b'br' and b'br' in ACCEPTED_ENCODINGS:
            body = brotli.decompress(body)
        return body 
Example #8
Source File: httpcompression.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def _decode(self, body, encoding):
        if encoding == b'gzip' or encoding == b'x-gzip':
            body = gunzip(body)

        if encoding == b'deflate':
            try:
                body = zlib.decompress(body)
            except zlib.error:
                # ugly hack to work with raw deflate content that may
                # be sent by microsoft servers. For more information, see:
                # http://carsten.codimi.de/gzip.yaws/
                # http://www.port80software.com/200ok/archive/2005/10/31/868.aspx
                # http://www.gzip.org/zlib/zlib_faq.html#faq38
                body = zlib.decompress(body, -15)
        if encoding == b'br' and b'br' in ACCEPTED_ENCODINGS:
            body = brotli.decompress(body)
        return body 
Example #9
Source File: compression.py    From Charcoal with MIT License 6 votes vote down vote up
def DecompressLZMA(string):
    """
    DecompressBrotli(string) -> str
    Returns the original form of the given string compressed \
using Google's brotli compression method., passed without delimiters.

    """
    number = 0
    for character in string:
        ordinal = OrdinalLookup.get(character, ord(character))
        number = number * 255 + ordinal - (ordinal > gap)
    compressed = []
    while number > 1:
        compressed = [number % 256] + compressed
        number //= 256
    return lzma.decompress(
        bytes(compressed),
        format=lzma.FORMAT_RAW,
        filters=[{'id': lzma.FILTER_LZMA2, 'preset': 9 | lzma.PRESET_EXTREME}]
    ).decode("ascii") 
Example #10
Source File: compression.py    From Charcoal with MIT License 6 votes vote down vote up
def DecompressBrotli(string):
    """
    DecompressBrotli(string) -> str
    Returns the original form of the given string compressed \
using Google's brotli compression method., passed without delimiters.

    """
    number = 0
    for character in string:
        ordinal = OrdinalLookup.get(character, ord(character))
        number = number * 255 + ordinal - (ordinal > gap)
    compressed = []
    while number > 1:
        compressed = [number % 256] + compressed
        number //= 256
    return brotli.decompress(bytes(compressed)).decode("ascii") 
Example #11
Source File: cloudscraper.py    From a4kScrapers with MIT License 6 votes vote down vote up
def decodeBrotli(self, resp):
        if requests.packages.urllib3.__version__ < '1.25.1' and resp.headers.get('Content-Encoding') == 'br':
            if self.allow_brotli and resp._content:
                resp._content = brotli.decompress(resp.content)
            else:
                logging.warning(
                    'You\'re running urllib3 {}, Brotli content detected, '
                    'Which requires manual decompression, '
                    'But option allow_brotli is set to False, '
                    'We will not continue to decompress.'.format(requests.packages.urllib3.__version__)
                )

        return resp

    # ------------------------------------------------------------------------------- #
    # Our hijacker request function
    # ------------------------------------------------------------------------------- # 
Example #12
Source File: content.py    From cocrawler with Apache License 2.0 5 votes vote down vote up
def decompress(body_bytes, content_encoding, url=None):
    content_encoding = content_encoding.lower()
    if content_encoding == 'deflate':
        try:
            return zlib.decompress(body_bytes, zlib.MAX_WBITS)  # expects header/checksum
        except Exception:
            try:
                # http://www.gzip.org/zlib/zlib_faq.html#faq38
                stats.stats_sum('content-encoding deflate fallback try', 1)
                return zlib.decompress(body_bytes, -zlib.MAX_WBITS)  # no header/checksum
            except Exception as e:
                LOGGER.debug('deflate fail for url %s: %s', url, str(e))
                stats.stats_sum('content-encoding deflate fail', 1)
                return body_bytes
    elif content_encoding == 'gzip' or content_encoding == 'x-gzip':
        try:
            return zlib.decompress(body_bytes, 16 + zlib.MAX_WBITS)
        except Exception as e:
            LOGGER.debug('gzip fail for url %s: %s', url, str(e))
            stats.stats_sum('content-encoding gzip fail', 1)
            return body_bytes
    elif content_encoding == 'br':
        try:
            return brotli.decompress(body_bytes)
        except Exception as e:
            LOGGER.debug('bz fail for url %s: %s', url, str(e))
            stats.stats_sum('content-encoding brotli fail', 1)
            return body_bytes
    else:
        # 'identity' is in the standard
        # also fairly common to have 'raw', 'none', or a charset
        return body_bytes 
Example #13
Source File: auto-checkin.py    From ssr-checkin with Apache License 2.0 5 votes vote down vote up
def checkin(url, headers, retry, proxy=False):
    try:
        response = requests.post(
            url, headers=headers, proxies=PROXY, verify=False) if proxy else requests.post(url, headers=headers)

        if response.status_code == 200:
            key = 'Content-Encoding'
            try:
                data = json.loads(brotli.decompress(response.content).decode('utf-8')) \
                    if key in response.headers and response.headers['Content-Encoding'] == 'br' \
                    else response.json()

                logging.info(u"签到成功 URL: {} {}".format(
                    extract_domain(url), data['msg']))
            except JSONDecodeError:
                logging.error(u"签到失败 URL: {}".format(extract_domain(url)))

            return

    except RequestException as e:
        logging.error(str(e))
        retry -= 1

        if retry > 0:
            time.sleep(get_randint(30, 60 * 60))
            checkin(url, headers, retry, proxy)

        logging.error(u"签到失败 URL: {}".format(extract_domain(url))) 
Example #14
Source File: woff2_test.py    From Emoji-Tools with GNU General Public License v3.0 5 votes vote down vote up
def test_incorrect_uncompressed_size(self):
		decompress_backup = brotli.decompress
		brotli.decompress = lambda data: b""  # return empty byte string
		with self.assertRaisesRegex(ttLib.TTLibError, 'unexpected size for decompressed'):
			WOFF2Reader(self.file)
		brotli.decompress = decompress_backup 
Example #15
Source File: __init__.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def request(self, method, url, *args, **kwargs):
        ourSuper = super(CloudScraper, self)
        resp = ourSuper.request(method, url, *args, **kwargs)

        if resp.headers.get('Content-Encoding') == 'br':
            if self.allow_brotli and resp._content:
                resp._content = brotli.decompress(resp.content)
            else:
                logging.warning('Brotli content detected, But option is disabled, we will not continue.')
                return resp

        # Debug request
        if self.debug:
            self.debugRequest(resp)

        # Check if Cloudflare anti-bot is on
        if self.isChallengeRequest(resp):
            if resp.request.method != 'GET':
                # Work around if the initial request is not a GET,
                # Supersede with a GET then re-request the original METHOD.
                self.request('GET', resp.url)
                resp = ourSuper.request(method, url, *args, **kwargs)
            else:
                # Solve Challenge
                resp = self.sendChallengeResponse(resp, **kwargs)

        return resp

    ########################################################################################################################################################## 
Example #16
Source File: test_simple_compression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_compressed_data_with_dictionaries(s, dictionary):
    d = brotli.Decompressor(dictionary)
    compressed = brotli.compress(s, dictionary=dictionary)
    uncompressed = d.decompress(compressed)
    assert uncompressed == s 
Example #17
Source File: test_simple_compression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_compressed_data_roundtrips(s):
    assert brotli.decompress(brotli.compress(s)) == s 
Example #18
Source File: test_simple_compression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_streaming_compression_flush(one_compressed_file,
                                     chunk_size,
                                     mode,
                                     quality,
                                     lgwin,
                                     lgblock):
    """
    Confirm that the streaming compressor works as expected, including flushes
    after each chunk.
    """
    compressed_chunks = []
    c = brotli.Compressor(
        mode=mode, quality=quality, lgwin=lgwin, lgblock=lgblock
    )
    with open(one_compressed_file, 'rb') as f:
        while True:
            next_data = f.read(chunk_size)
            if not next_data:
                break

            compressed_chunks.append(c.compress(next_data))
            compressed_chunks.append(c.flush())

    compressed_chunks.append(c.finish())
    decompressed = brotli.decompress(b''.join(compressed_chunks))
    with open(one_compressed_file, 'rb') as f:
        assert decompressed == f.read() 
Example #19
Source File: test_simple_decompression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_decompression_fails_properly_on_garbage(bogus, exception_cls):
    """
    Garbage data properly fails decompression.
    """
    with pytest.raises(exception_cls):
        brotli.decompress(bogus) 
Example #20
Source File: test_simple_decompression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_streaming_decompression_fails_properly_on_garbage(exception_cls):
    """
    Garbage data properly fails decompression.
    """
    o = brotli.Decompressor()
    with pytest.raises(exception_cls):
        o.decompress(b'some random garbage') 
Example #21
Source File: test_simple_decompression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_decompressobj(simple_compressed_file):
    with open(simple_compressed_file[0], 'rb') as f:
        uncompressed_data = f.read()

    with open(simple_compressed_file[1], 'rb') as f:
        compressed_data = f.read()

    o = brotli.Decompressor()
    data = o.decompress(compressed_data)
    data += o.flush()
    data += o.finish()

    assert data == uncompressed_data 
Example #22
Source File: test_simple_decompression.py    From brotlipy with MIT License 5 votes vote down vote up
def test_decompression(simple_compressed_file):
    """
    Decompressing files returns their original form using decompress.
    """
    with open(simple_compressed_file[0], 'rb') as f:
        uncompressed_data = f.read()

    with open(simple_compressed_file[1], 'rb') as f:
        compressed_data = f.read()

    assert brotli.decompress(compressed_data) == uncompressed_data 
Example #23
Source File: compression.py    From cloud-volume with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def brotli_decompress(content):
  if len(content) == 0:
    raise DecompressionError('File contains zero bytes.')

  return brotli.decompress(content) 
Example #24
Source File: compression.py    From cloud-volume with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def decompress(content, encoding, filename='N/A'):
  """
  Decompress file content. 

  Required: 
    content (bytes): a file to be compressed
    encoding: None (no compression) or 'gzip' or 'br'
  Optional:   
    filename (str:default:'N/A'): Used for debugging messages
  Raises: 
    NotImplementedError if an unsupported codec is specified. 
    compression.EncodeError if the encoder has an issue

  Return: decompressed content
  """
  try:
    encoding = (encoding or '').lower()
    if encoding == '':
      return content
    elif len(content) == 0:
      raise DecompressionError('File contains zero bytes: ' + str(filename))
    elif encoding == 'gzip':
      return gunzip(content)
    elif encoding == 'br':
      return brotli_decompress(content)
  except DecompressionError as err:
    print("Filename: " + str(filename))
    raise
  
  raise NotImplementedError(str(encoding) + ' is not currently supported. Supported Options: None, gzip') 
Example #25
Source File: image_url.py    From rssant with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def decode_image_url(data):
    try:
        data = base64.urlsafe_b64decode(data)
        text = brotli.decompress(data).decode('utf-8')
        return validate_image_url(json.loads(text))
    except (Invalid, json.JSONDecodeError, brotli.error, UnicodeDecodeError) as ex:
        raise ImageUrlDecodeError(str(ex)) from ex 
Example #26
Source File: http.py    From scapy with GNU General Public License v2.0 5 votes vote down vote up
def post_dissect(self, s):
        if not conf.contribs["http"]["auto_compression"]:
            return s
        encodings = self._get_encodings()
        # Un-chunkify
        if "chunked" in encodings:
            data = b""
            while s:
                length, _, body = s.partition(b"\r\n")
                try:
                    length = int(length, 16)
                except ValueError:
                    # Not a valid chunk. Ignore
                    break
                else:
                    load = body[:length]
                    if body[length:length + 2] != b"\r\n":
                        # Invalid chunk. Ignore
                        break
                    s = body[length + 2:]
                    data += load
            if not s:
                s = data
        # Decompress
        try:
            if "deflate" in encodings:
                import zlib
                s = zlib.decompress(s)
            elif "gzip" in encodings:
                s = gzip_decompress(s)
            elif "compress" in encodings:
                import lzw
                s = lzw.decompress(s)
            elif "br" in encodings and is_brotli_available:
                s = brotli.decompress(s)
        except Exception:
            # Cannot decompress - probably incomplete data
            pass
        return s 
Example #27
Source File: woff2.py    From Emoji-Tools with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, file, checkChecksums=1, fontNumber=-1):
		if not haveBrotli:
			log.error(
				'The WOFF2 decoder requires the Brotli Python extension, available at: '
				'https://github.com/google/brotli')
			raise ImportError("No module named brotli")

		self.file = file

		signature = Tag(self.file.read(4))
		if signature != b"wOF2":
			raise TTLibError("Not a WOFF2 font (bad signature)")

		self.file.seek(0)
		self.DirectoryEntry = WOFF2DirectoryEntry
		data = self.file.read(woff2DirectorySize)
		if len(data) != woff2DirectorySize:
			raise TTLibError('Not a WOFF2 font (not enough data)')
		sstruct.unpack(woff2DirectoryFormat, data, self)

		self.tables = OrderedDict()
		offset = 0
		for i in range(self.numTables):
			entry = self.DirectoryEntry()
			entry.fromFile(self.file)
			tag = Tag(entry.tag)
			self.tables[tag] = entry
			entry.offset = offset
			offset += entry.length

		totalUncompressedSize = offset
		compressedData = self.file.read(self.totalCompressedSize)
		decompressedData = brotli.decompress(compressedData)
		if len(decompressedData) != totalUncompressedSize:
			raise TTLibError(
				'unexpected size for decompressed font data: expected %d, found %d'
				% (totalUncompressedSize, len(decompressedData)))
		self.transformBuffer = BytesIO(decompressedData)

		self.file.seek(0, 2)
		if self.length != self.file.tell():
			raise TTLibError("reported 'length' doesn't match the actual file size")

		self.flavorData = WOFF2FlavorData(self)

		# make empty TTFont to store data while reconstructing tables
		self.ttFont = TTFont(recalcBBoxes=False, recalcTimestamp=False) 
Example #28
Source File: http.py    From ftw with Apache License 2.0 4 votes vote down vote up
def parse_content_encoding(self, response_headers, response_data):
        """
        Parses a response that contains Content-Encoding to retrieve
        response_data
        """
        if response_headers['content-encoding'] == 'br':
            try:
                response_data = brotli.decompress(response_data)
            except brotli.error:
                raise errors.TestError(
                    'Invalid or missing brotli data found',
                    {
                        'response_data': str(response_data),
                        'function': 'http.HttpResponse.parse_content_encoding'
                    })
        elif response_headers['content-encoding'] == 'gzip':
            buf = BytesIO(response_data)
            zipbuf = gzip.GzipFile(fileobj=buf)
            try:
                response_data = zipbuf.read()
            except IOError:
                raise errors.TestError(
                    'Invalid or missing gzip data found',
                    {
                        'response_data': str(response_data),
                        'function': 'http.HttpResponse.parse_content_encoding'
                    })
        elif response_headers['content-encoding'] == 'deflate':
            try:
                response_data = zlib.decompress(response_data, -zlib.MAX_WBITS)
            except zlib.error:
                raise errors.TestError(
                    'Invalid or missing deflate data found',
                    {
                        'response_data': str(response_data),
                        'function': 'http.HttpResponse.parse_content_encoding'
                    })
        else:
            raise errors.TestError(
                'Received unknown Content-Encoding',
                {
                    'content-encoding':
                        str(response_headers['content-encoding']),
                    'function': 'http.HttpResponse.parse_content_encoding'
                })
        return response_data