Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style cleanup in YouTubeVideoUrl #264

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions src/YouTubeVideoUrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,19 @@ def _unthrottle_url(self, url, player_id):
del self._code_cache[n_id]
return url

def _decrypt_signature(self, s, player_id):
def _decrypt_signature_url(self, sc, player_id):
"""Turn the encrypted s field into a working signature"""
s = sc.get('s', [''])[0]
s_id = 'sig_%s_%s' % (player_id, '.'.join(str(len(p)) for p in s.split('.')))
print('[YouTubeVideoUrl] Decrypt signature', s_id)
try:
return self._extract_function(player_id, s_id)(s)
sig = self._extract_function(player_id, s_id)(s)
except Exception as ex:
print('[YouTubeVideoUrl] Signature extraction failed', ex)
if s_id in self._code_cache:
del self._code_cache[s_id]
else:
return '%s&%s=%s' % (sc['url'][0], sc['sp'][0] if 'sp' in sc else 'signature', sig)

def _parse_sig_js(self, jscode):

Expand Down Expand Up @@ -292,9 +295,7 @@ def _extract_url(self, our_format, streaming_formats, player_id, get_audio=None)
url = fmt.get('url')
if not url and 'signatureCipher' in fmt:
sc = compat_parse_qs(fmt.get('signatureCipher', ''))
sig = self._decrypt_signature(sc['s'][0], player_id)
if sig:
url = '%s&%s=%s' % (sc['url'][0], sc['sp'][0] if 'sp' in sc else 'signature', sig)
url = self._decrypt_signature_url(sc, player_id)
if url:
if '&n=' in url:
url = self._unthrottle_url(url, player_id)
Expand All @@ -321,6 +322,18 @@ def _extract_dash_audio_format(self, streaming_formats, player_id):
return url
return ''

def _extract_signature_timestamp(self):
sts = None
player_id = self._extract_player_info()
if player_id:
if player_id not in self._player_cache:
self._load_player(player_id)
sts = search(
r'(?:signatureTimestamp|sts)\s*:\s*(?P<sts>\d{5})',
self._player_cache[player_id]
).group('sts')
return sts, player_id

def _extract_player_response(self, video_id, yt_auth, client):
player_id = None
url = 'https://www.youtube.com/youtubei/v1/player?prettyPrint=false'
Expand Down Expand Up @@ -379,16 +392,9 @@ def _extract_player_response(self, video_id, yt_auth, client):
data['context']['client']['userAgent'] = USER_AGENT
headers['User-Agent'] = USER_AGENT
if client in (2, 85):
player_id = self._extract_player_info()
if player_id:
if player_id not in self._player_cache:
self._load_player(player_id)
sts = search(
r'(?:signatureTimestamp|sts)\s*:\s*(?P<sts>\d{5})',
self._player_cache[player_id]
).group('sts')
if sts:
data['playbackContext']['contentPlaybackContext']['signatureTimestamp'] = sts
sts, player_id = self._extract_signature_timestamp()
if sts:
data['playbackContext']['contentPlaybackContext']['signatureTimestamp'] = sts
headers['X-YouTube-Client-Version'] = VERSION
try:
return loads(self._download_webpage(url, data, headers)), player_id
Expand Down
2 changes: 1 addition & 1 deletion test/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_function_exceptions():
from src.YouTubeVideoUrl import YouTubeVideoUrl
ytdl = YouTubeVideoUrl()
player_id = ytdl._extract_player_info()
ytdl._decrypt_signature('', player_id)
ytdl._decrypt_signature_url({}, player_id)
ytdl._unthrottle_url('&n=a&', player_id)
ytdl._guess_encoding_from_content('', br'<meta charset=ascii>')
ytdl._guess_encoding_from_content('', b'\xff\xfe')
Loading