Skip to content
This repository has been archived by the owner on Oct 13, 2024. It is now read-only.

Commit

Permalink
yt-dl: build fallback url
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenigneArcher committed Jul 30, 2024
1 parent 20c9f0b commit 81df3d2
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion Contents/Code/youtube_dl_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import logging
import json
import os
import re
import tempfile
import time
import urlparse

# plex debugging
try:
Expand All @@ -28,6 +30,32 @@
plugin_logger = logging.getLogger(plugin_identifier)


def build_fallback_playback_url(playback_url):
# type: (str) -> Optional[str]
"""
Build a fallback URL for a YouTube video.
Parameters
----------
playback_url : str
The playback URL of the YouTube video.
"""
query = youtube_dl.utils.parse_qs(url=playback_url)
mn = query.get('mn', [''])[0]
fvip = query.get('fvip', [''])[0]

mn = youtube_dl.utils.str_or_none(mn, '').split(',')
if len(mn) > 1 and mn[1] and fvip:
fmt_url_parsed = urlparse.urlparse(playback_url)
new_netloc = re.sub(
r'\d+', fvip, fmt_url_parsed.netloc.split('---')[0]) + '---' + mn[1] + '.googlevideo.com'

return youtube_dl.utils.update_url_query(
url=fmt_url_parsed._replace(netloc=new_netloc).geturl(),
query={'fallback_count': '1'},
)


def ns_bool(value):
# type: (bool) -> str
"""
Expand Down Expand Up @@ -165,8 +193,26 @@ def process_youtube(url):

if audio_url:
validate = requests.get(url=audio_url, stream=True)
if validate.status_code != 200:
if validate.status_code == 200:
return audio_url
else:
Log.Warn('Failed to validate audio URL for video {}'.format(url))

# build a fallback URL
fallback_url = build_fallback_playback_url(playback_url=audio_url)
if fallback_url:
audio_url = fallback_url
Log.Warn('Trying fallback URL for video {}'.format(url))
validate = requests.get(url=audio_url, stream=True)
if validate.status_code == 200:
return audio_url

Check warning on line 208 in Contents/Code/youtube_dl_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/youtube_dl_helper.py#L208

Added line #L208 was not covered by tests
else:
Log.Warn('Failed to validate fallback URL for video {}'.format(url))
audio_url = None
else:
Log.Warn('Failed to build fallback URL for video {}'.format(url))
audio_url = None

Check warning on line 214 in Contents/Code/youtube_dl_helper.py

View check run for this annotation

Codecov / codecov/patch

Contents/Code/youtube_dl_helper.py#L213-L214

Added lines #L213 - L214 were not covered by tests

count += 1
continue

Expand Down

0 comments on commit 81df3d2

Please sign in to comment.