]>
jfr.im git - yt-dlp.git/blob - yt_dlp/utils/networking.py
e6515ec8eecfe8d76035a500b25f7728ec2b1224
6 from ._utils
import remove_start
9 def random_user_agent():
10 _USER_AGENT_TPL
= 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Safari/537.36'
51 return _USER_AGENT_TPL
% random
.choice(_CHROME_VERSIONS
)
54 class HTTPHeaderDict(collections
.UserDict
, dict):
56 Store and access keys case-insensitively.
57 The constructor can take multiple dicts, in which keys in the latter are prioritised.
60 def __init__(self
, *args
, **kwargs
):
67 def __setitem__(self
, key
, value
):
68 super().__setitem
__(key
.title(), str(value
))
70 def __getitem__(self
, key
):
71 return super().__getitem
__(key
.title())
73 def __delitem__(self
, key
):
74 super().__delitem
__(key
.title())
76 def __contains__(self
, key
):
77 return super().__contains
__(key
.title() if isinstance(key
, str) else key
)
80 std_headers
= HTTPHeaderDict({
81 'User-Agent': random_user_agent(),
82 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
83 'Accept-Language': 'en-us,en;q=0.5',
84 'Sec-Fetch-Mode': 'navigate',
88 def clean_proxies(proxies
: dict, headers
: HTTPHeaderDict
):
89 req_proxy
= headers
.pop('Ytdl-Request-Proxy', None)
91 proxies
.clear() # XXX: compat: Ytdl-Request-Proxy takes preference over everything, including NO_PROXY
92 proxies
['all'] = req_proxy
93 for proxy_key
, proxy_url
in proxies
.items():
94 if proxy_url
== '__noproxy__':
95 proxies
[proxy_key
] = None
97 if proxy_key
== 'no': # special case
99 if proxy_url
is not None:
100 # Ensure proxies without a scheme are http.
102 proxy_scheme
= urllib
.request
._parse
_proxy
(proxy_url
)[0]
104 # Ignore invalid proxy URLs. Sometimes these may be introduced through environment
105 # variables unrelated to proxy settings - e.g. Colab `COLAB_LANGUAGE_SERVER_PROXY`.
106 # If the proxy is going to be used, the Request Handler proxy validation will handle it.
108 if proxy_scheme
is None:
109 proxies
[proxy_key
] = 'http://' + remove_start(proxy_url
, '//')
112 'socks5': 'socks5h', # compat: socks5 was treated as socks5h
113 'socks': 'socks4' # compat: non-standard
115 if proxy_scheme
in replace_scheme
:
116 proxies
[proxy_key
] = urllib
.parse
.urlunparse(
117 urllib
.parse
.urlparse(proxy_url
)._replace
(scheme
=replace_scheme
[proxy_scheme
]))
120 def clean_headers(headers
: HTTPHeaderDict
):
121 if 'Youtubedl-No-Compression' in headers
: # compat
122 del headers
['Youtubedl-No-Compression']
123 headers
['Accept-Encoding'] = 'identity'