[youtube:tab] Show alerts only from the final webpage

This commit is contained in:
pukkandan 2021-05-17 18:30:50 +05:30
parent 6911e11edd
commit 95c01b6c16
No known key found for this signature in database
GPG key ID: 0F00D95A001F4698

View file

@ -3395,27 +3395,27 @@ class YoutubeTabIE(YoutubeBaseInfoExtractor):
self._extract_mix_playlist(playlist, playlist_id, data, webpage), self._extract_mix_playlist(playlist, playlist_id, data, webpage),
playlist_id=playlist_id, playlist_title=title) playlist_id=playlist_id, playlist_title=title)
def _extract_alerts(self, data, expected=False): @staticmethod
def _extract_alerts(data):
def _real_extract_alerts(): for alert_dict in try_get(data, lambda x: x['alerts'], list) or []:
for alert_dict in try_get(data, lambda x: x['alerts'], list) or []: if not isinstance(alert_dict, dict):
if not isinstance(alert_dict, dict): continue
for alert in alert_dict.values():
alert_type = alert.get('type')
if not alert_type:
continue continue
for alert in alert_dict.values(): message = try_get(alert, lambda x: x['text']['simpleText'], compat_str) or ''
alert_type = alert.get('type') if message:
if not alert_type: yield alert_type, message
continue for run in try_get(alert, lambda x: x['text']['runs'], list) or []:
message = try_get(alert, lambda x: x['text']['simpleText'], compat_str) or '' message += try_get(run, lambda x: x['text'], compat_str)
if message: if message:
yield alert_type, message yield alert_type, message
for run in try_get(alert, lambda x: x['text']['runs'], list) or []:
message += try_get(run, lambda x: x['text'], compat_str)
if message:
yield alert_type, message
def _report_alerts(self, alerts, expected=True):
errors = [] errors = []
warnings = [] warnings = []
for alert_type, alert_message in _real_extract_alerts(): for alert_type, alert_message in alerts:
if alert_type.lower() == 'error': if alert_type.lower() == 'error':
errors.append([alert_type, alert_message]) errors.append([alert_type, alert_message])
else: else:
@ -3426,6 +3426,9 @@ class YoutubeTabIE(YoutubeBaseInfoExtractor):
if errors: if errors:
raise ExtractorError('YouTube said: %s' % errors[-1][1], expected=expected) raise ExtractorError('YouTube said: %s' % errors[-1][1], expected=expected)
def _extract_and_report_alerts(self, data, *args, **kwargs):
return self._report_alerts(self._extract_alerts(data), *args, **kwargs)
def _reload_with_unavailable_videos(self, item_id, data, webpage): def _reload_with_unavailable_videos(self, item_id, data, webpage):
""" """
Get playlist with unavailable videos if the 'show unavailable videos' button exists. Get playlist with unavailable videos if the 'show unavailable videos' button exists.
@ -3504,7 +3507,7 @@ class YoutubeTabIE(YoutubeBaseInfoExtractor):
else: else:
# Youtube may send alerts if there was an issue with the continuation page # Youtube may send alerts if there was an issue with the continuation page
self._extract_alerts(response, expected=False) self._extract_and_report_alerts(response, expected=False)
if not check_get_keys or dict_get(response, check_get_keys): if not check_get_keys or dict_get(response, check_get_keys):
break break
# Youtube sometimes sends incomplete data # Youtube sometimes sends incomplete data
@ -3532,9 +3535,10 @@ class YoutubeTabIE(YoutubeBaseInfoExtractor):
url, item_id, url, item_id,
'Downloading webpage%s' % (' (retry #%d)' % count if count else '')) 'Downloading webpage%s' % (' (retry #%d)' % count if count else ''))
data = self._extract_yt_initial_data(item_id, webpage) data = self._extract_yt_initial_data(item_id, webpage)
self._extract_alerts(data, expected=True)
if data.get('contents') or data.get('currentVideoEndpoint'): if data.get('contents') or data.get('currentVideoEndpoint'):
break break
# Extract alerts here only when there is error
self._extract_and_report_alerts(data)
if count >= retries: if count >= retries:
raise ExtractorError(last_error) raise ExtractorError(last_error)
return webpage, data return webpage, data
@ -3594,6 +3598,7 @@ class YoutubeTabIE(YoutubeBaseInfoExtractor):
# YouTube sometimes provides a button to reload playlist with unavailable videos. # YouTube sometimes provides a button to reload playlist with unavailable videos.
if 'no-youtube-unavailable-videos' not in compat_opts: if 'no-youtube-unavailable-videos' not in compat_opts:
data = self._reload_with_unavailable_videos(item_id, data, webpage) or data data = self._reload_with_unavailable_videos(item_id, data, webpage) or data
self._extract_and_report_alerts(data)
tabs = try_get( tabs = try_get(
data, lambda x: x['contents']['twoColumnBrowseResultsRenderer']['tabs'], list) data, lambda x: x['contents']['twoColumnBrowseResultsRenderer']['tabs'], list)