Skip to content

Commit

Permalink
add 'brotli' as optional dependency (#2716)
Browse files Browse the repository at this point in the history
only send 'Accept-Encoding: br' if supported
  • Loading branch information
mikf committed Jun 29, 2022
1 parent 37453a9 commit de20cad
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Optional
- FFmpeg_: Pixiv Ugoira to WebM conversion
- yt-dlp_ or youtube-dl_: Video downloads
- PySocks_: SOCKS proxy support
- brotli_ or brotlicffi_: Brotli compression support


Installation
Expand Down Expand Up @@ -332,6 +333,8 @@ To authenticate with a ``mastodon`` instance, run *gallery-dl* with
.. _yt-dlp: https://github.com/yt-dlp/yt-dlp
.. _youtube-dl: https://ytdl-org.github.io/youtube-dl/
.. _PySocks: https://pypi.org/project/PySocks/
.. _brotli: https://github.com/google/brotli
.. _brotlicffi: https://github.com/python-hyper/brotlicffi
.. _pyOpenSSL: https://pyopenssl.org/
.. _Snapd: https://docs.snapcraft.io/installing-snapd
.. _OAuth: https://en.wikipedia.org/wiki/OAuth
Expand Down
15 changes: 13 additions & 2 deletions gallery_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ def _init_session(self):
"rv:102.0) Gecko/20100101 Firefox/102.0"))
headers["Accept"] = "*/*"
headers["Accept-Language"] = "en-US,en;q=0.5"

if BROTLI:
headers["Accept-Encoding"] = "gzip, deflate, br"
else:
headers["Accept-Encoding"] = "gzip, deflate"

custom_headers = self.config("headers")
Expand Down Expand Up @@ -718,7 +722,7 @@ def _build_requests_adapter(ssl_options, ssl_ciphers, source_address):
("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/avif,image/webp,*/*;q=0.8"),
("Accept-Language", "en-US,en;q=0.5"),
("Accept-Encoding", "gzip, deflate, br"),
("Accept-Encoding", None),
("Referer", None),
("DNT", "1"),
("Connection", "keep-alive"),
Expand All @@ -736,7 +740,7 @@ def _build_requests_adapter(ssl_options, ssl_ciphers, source_address):
("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,"
"image/webp,image/apng,*/*;q=0.8"),
("Referer", None),
("Accept-Encoding", "gzip, deflate"),
("Accept-Encoding", None),
("Accept-Language", "en-US,en;q=0.9"),
("Cookie", None),
),
Expand Down Expand Up @@ -783,6 +787,13 @@ def _build_requests_adapter(ssl_options, ssl_ciphers, source_address):
}


# detect brotli support
try:
BROTLI = requests.packages.urllib3.response.brotli is not None
except AttributeError:
BROTLI = False


# Undo automatic pyOpenSSL injection by requests
pyopenssl = config.get((), "pyopenssl", False)
if not pyopenssl:
Expand Down

0 comments on commit de20cad

Please sign in to comment.