Skip to content

Commit 8f14dff

Browse files
committed
plugins.youtube: fix consent issue
based on streamlink#3672
1 parent e0d8229 commit 8f14dff

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/streamlink/plugins/youtube.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ def __init__(self, url):
139139
self.title = None
140140
self.video_id = None
141141
self.session.http.headers.update({'User-Agent': useragents.CHROME})
142+
consent = self.cache.get("consent_ck")
143+
if consent is not None:
144+
self.set_consent_ck(consent)
142145

143146
def get_author(self):
144147
if self.author is None:
@@ -228,14 +231,30 @@ def _create_adaptive_streams(self, info, streams):
228231

229232
return streams
230233

231-
def _find_video_id(self, url):
234+
def set_consent_ck(self, consent):
235+
self.session.http.cookies.set(
236+
'CONSENT',
237+
consent,
238+
domain='.youtube.com', path="/")
232239

240+
def _find_video_id(self, url):
233241
m = _url_re.match(url)
234-
if m.group("video_id"):
242+
if m and m.group("video_id"):
235243
log.debug("Video ID from URL")
236244
return m.group("video_id")
237245

238246
res = self.session.http.get(url)
247+
if urlparse(res.url).netloc == "consent.youtube.com":
248+
c_data = {}
249+
for _i in itertags(res.text, "input"):
250+
if _i.attributes.get("type") == "hidden":
251+
c_data[_i.attributes.get("name")] = _i.attributes.get("value")
252+
log.debug("c_data_keys: {}".format(', '.join(c_data.keys())))
253+
res = self.session.http.post("https://consent.youtube.com/s", data=c_data)
254+
consent = self.session.http.cookies.get('CONSENT', domain='.youtube.com')
255+
if 'YES' in consent:
256+
self.cache.set("consent_ck", consent)
257+
239258
datam = _ytdata_re.search(res.text)
240259
if datam:
241260
data = parse_json(datam.group(1))

0 commit comments

Comments
 (0)