Skip to content

Commit

Permalink
implement '"user-agent": "browser"' (#2636)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikf committed Nov 13, 2022
1 parent 70c7fbe commit 9f06e79
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,9 @@ Default
Description
User-Agent header value to be used for HTTP requests.

Setting this value to ``"browser"`` will try to automatically detect
and use the User-Agent used by the system's default browser.

Note: This option has no effect on `pixiv` extractors,
as these need specific values to function correctly.

Expand All @@ -520,7 +523,8 @@ extractor.*.browser
Type
``string``
Default
``"firefox"`` for ``patreon``, ``null`` everywhere else
* ``"firefox"`` for ``patreon``, ``mangapark``, and ``mangasee``
* ``null`` everywhere else
Example
* ``"chrome:macos"``
Description
Expand Down
42 changes: 38 additions & 4 deletions gallery_dl/extractor/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import threading
from requests.adapters import HTTPAdapter
from .message import Message
from .. import config, text, util, exception
from .. import config, text, util, cache, exception


class Extractor():
Expand Down Expand Up @@ -262,9 +262,13 @@ def _init_session(self):
ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1)
ssl_ciphers = SSL_CIPHERS[browser]
else:
headers["User-Agent"] = self.config("user-agent", (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
"rv:102.0) Gecko/20100101 Firefox/102.0"))
useragent = self.config("user-agent")
if useragent is None:
useragent = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64; "
"rv:102.0) Gecko/20100101 Firefox/102.0")
elif useragent == "browser":
useragent = _browser_useragent()
headers["User-Agent"] = useragent
headers["Accept"] = "*/*"
headers["Accept-Language"] = "en-US,en;q=0.5"

Expand Down Expand Up @@ -724,6 +728,36 @@ def _build_requests_adapter(ssl_options, ssl_ciphers, source_address):
return adapter


@cache.cache(maxage=86400)
def _browser_useragent():
"""Get User-Agent header from default browser"""
import webbrowser
import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("127.0.0.1", 6414))
server.listen(1)

webbrowser.open("http://127.0.0.1:6414/user-agent")

client = server.accept()[0]
server.close()

for line in client.recv(1024).split(b"\r\n"):
key, _, value = line.partition(b":")
if key.strip().lower() == b"user-agent":
useragent = value.strip()
break
else:
useragent = b""

client.send(b"HTTP/1.1 200 OK\r\n\r\n" + useragent)
client.close()

return useragent.decode()


_adapter_cache = {}
_browser_cookies = {}

Expand Down

0 comments on commit 9f06e79

Please sign in to comment.