Add ytInitialPlayerResponse checker to let user easily know problem

This commit is contained in:
Wanlin Wang 王万霖 2024-07-25 17:20:26 +08:00 committed by GitHub
parent 899e2b6b2b
commit 4ab02216cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -175,6 +175,16 @@ class YouTube(VideoExtractor):
pass
# FIXME: show DASH stream sizes (by default) for playlist videos
def check_playability_response(self, ytInitialPlayerResponse):
STATUS_OK = "OK"
playerResponseStatus = ytInitialPlayerResponse["playabilityStatus"]["status"]
if playerResponseStatus != STATUS_OK:
reason = ytInitialPlayerResponse["playabilityStatus"].get("reason", "")
raise AssertionError(
f"Server refused to provide video details. Returned status: {playerResponseStatus}, reason: {reason}."
)
def prepare(self, **kwargs):
assert self.url or self.vid
@ -202,6 +212,7 @@ class YouTube(VideoExtractor):
logging.debug('Loading ytInitialPlayerResponse...')
ytInitialPlayerResponse = json.loads(re.search('ytInitialPlayerResponse\s*=\s*([^\n]+?});(\n|</script>|var )', video_page).group(1))
self.check_playability_response(ytInitialPlayerResponse)
# Get the video title
self.title = ytInitialPlayerResponse["videoDetails"]["title"]