[common] implement '"user-agent": "+PRESET"'

support using a 'User-Agent' header preset, e.g. "+firefox"
This commit is contained in:
Mike Fährmann
2026-01-03 19:23:50 +01:00
parent c745a57bd7
commit 2cfe2b3b04
2 changed files with 23 additions and 3 deletions

View File

@@ -817,15 +817,24 @@ Default
Example
* ``"curl/8.14.1"``
* ``"browser"``
* ``"@chrome"``
* ``"+chrome"``
* ``"@/opt/ChromeBrowser/bin/chrome"``
Description
User-Agent header value used for HTTP requests.
Setting this value to ``"browser"`` will try to automatically detect
and use the ``User-Agent`` header of the system's default browser.
Setting this value to ``"@BROWSER"``, e.g. ``"@chrome"``, will try to automatically detect
and use the ``User-Agent`` header of this installed browser.
| Starting this value with a ``+``
will use the latest ``User-Agent`` header of this preset target,
e.g. ``"+ff"``.
| (Supported values:
``firefox`` | ``ff`` | ``chrome`` | ``cr`` | ``gallery-dl`` | ``gdl``)
| Starting this value with an ``@``
will try to automatically detect and use the ``User-Agent`` header
of this installed browser,
| e.g. ``"@C:/Program Files/Zen Browser/zen-browser.exe"``.
extractor.*.browser

View File

@@ -475,6 +475,17 @@ class Extractor():
headers["User-Agent"] = _browser_useragent(None)
elif custom_ua[0] == "@":
headers["User-Agent"] = _browser_useragent(custom_ua[1:])
elif custom_ua[0] == "+":
custom_ua = custom_ua[1:].lower()
if custom_ua in {"firefox", "ff"}:
headers["User-Agent"] = util.USERAGENT_FIREFOX
elif custom_ua in {"chrome", "cr"}:
headers["User-Agent"] = util.USERAGENT_CHROME
elif custom_ua in {"gallery-dl", "gallerydl", "gdl"}:
headers["User-Agent"] = util.USERAGENT_GALLERYDL
else:
self.log.warning(
"Unsupported User-Agent preset '%s'", custom_ua)
elif self.useragent is Extractor.useragent and not self.browser or \
custom_ua is not config.get(("extractor",), "user-agent"):
headers["User-Agent"] = custom_ua