@@ -139,6 +139,9 @@ def __init__(self, url):
139
139
self .title = None
140
140
self .video_id = None
141
141
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 )
142
145
143
146
def get_author (self ):
144
147
if self .author is None :
@@ -228,14 +231,30 @@ def _create_adaptive_streams(self, info, streams):
228
231
229
232
return streams
230
233
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 = "/" )
232
239
240
+ def _find_video_id (self , url ):
233
241
m = _url_re .match (url )
234
- if m .group ("video_id" ):
242
+ if m and m .group ("video_id" ):
235
243
log .debug ("Video ID from URL" )
236
244
return m .group ("video_id" )
237
245
238
246
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
+
239
258
datam = _ytdata_re .search (res .text )
240
259
if datam :
241
260
data = parse_json (datam .group (1 ))
0 commit comments