aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcoletdjnz <colethedj@protonmail.com>2021-06-11 09:02:57 +1200
committerGitHub <noreply@github.com>2021-06-10 21:02:57 +0000
commit1974e99f4b03a0a8b91bdc75b90c167445d3c229 (patch)
tree592843a315edbe27c1de7ceea148ee4841b0418b
parent0181adefc6dfb560761461b567e6bbb6718dcf29 (diff)
downloadhypervideo-pre-1974e99f4b03a0a8b91bdc75b90c167445d3c229.tar.lz
hypervideo-pre-1974e99f4b03a0a8b91bdc75b90c167445d3c229.tar.xz
hypervideo-pre-1974e99f4b03a0a8b91bdc75b90c167445d3c229.zip
[youtube] Improve SAPISID cookie handling (closes #393) (#395)
Author: colethedj
-rw-r--r--yt_dlp/extractor/youtube.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.py
index e2a174ae1..f88c2f727 100644
--- a/yt_dlp/extractor/youtube.py
+++ b/yt_dlp/extractor/youtube.py
@@ -301,12 +301,22 @@ class YoutubeBaseInfoExtractor(InfoExtractor):
_YT_INITIAL_BOUNDARY_RE = r'(?:var\s+meta|</script|\n)'
def _generate_sapisidhash_header(self):
- sapisid_cookie = self._get_cookies('https://www.youtube.com').get('SAPISID')
+ # Sometimes SAPISID cookie isn't present but __Secure-3PAPISID is.
+ # See: https://github.com/yt-dlp/yt-dlp/issues/393
+ yt_cookies = self._get_cookies('https://www.youtube.com')
+ sapisid_cookie = dict_get(
+ yt_cookies, ('__Secure-3PAPISID', 'SAPISID'))
if sapisid_cookie is None:
return
time_now = round(time.time())
- sapisidhash = hashlib.sha1((str(time_now) + " " + sapisid_cookie.value + " " + "https://www.youtube.com").encode("utf-8")).hexdigest()
- return "SAPISIDHASH %s_%s" % (time_now, sapisidhash)
+ # SAPISID cookie is required if not already present
+ if not yt_cookies.get('SAPISID'):
+ self._set_cookie(
+ '.youtube.com', 'SAPISID', sapisid_cookie.value, secure=True, expire_time=time_now + 3600)
+ # SAPISIDHASH algorithm from https://stackoverflow.com/a/32065323
+ sapisidhash = hashlib.sha1(
+ f'{time_now} {sapisid_cookie.value} https://www.youtube.com'.encode('utf-8')).hexdigest()
+ return f'SAPISIDHASH {time_now}_{sapisidhash}'
def _call_api(self, ep, query, video_id, fatal=True, headers=None,
note='Downloading API JSON', errnote='Unable to download API page',