From 2140f489192f98284811e7cbaba8b6fa0e552bf7 Mon Sep 17 00:00:00 2001 From: Astound Date: Mon, 22 Jan 2024 05:52:44 +0800 Subject: Subscriptions: Use playlist method to get channel videos Use the UU (user uploads) playlist since it includes streams --- youtube/playlist.py | 8 +++++--- youtube/subscriptions.py | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/youtube/playlist.py b/youtube/playlist.py index a2ff80f..83d530c 100644 --- a/youtube/playlist.py +++ b/youtube/playlist.py @@ -28,7 +28,8 @@ def playlist_ctoken(playlist_id, offset, include_shorts=True): return base64.urlsafe_b64encode(pointless_nest).decode('ascii') -def playlist_first_page(playlist_id, report_text="Retrieved playlist", use_mobile=False): +def playlist_first_page(playlist_id, report_text="Retrieved playlist", + use_mobile=False): if use_mobile: url = 'https://m.youtube.com/playlist?list=' + playlist_id + '&pbj=1' content = util.fetch_url( @@ -47,7 +48,8 @@ def playlist_first_page(playlist_id, report_text="Retrieved playlist", use_mobil return content -def get_videos(playlist_id, page, include_shorts=True, use_mobile=False): +def get_videos(playlist_id, page, include_shorts=True, use_mobile=False, + report_text='Retrieved playlist'): # mobile requests return 20 videos per page if use_mobile: page_size = 20 @@ -62,7 +64,7 @@ def get_videos(playlist_id, page, include_shorts=True, use_mobile=False): include_shorts=include_shorts) url += "&pbj=1" content = util.fetch_url( - url, headers, report_text="Retrieved playlist", + url, headers, report_text=report_text, debug_name='playlist_videos' ) diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index 242510b..ea544a3 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -1,4 +1,4 @@ -from youtube import util, yt_data_extract, channel, local_playlist +from youtube import util, yt_data_extract, channel, local_playlist, playlist from youtube import yt_app import settings @@ -463,7 +463,20 @@ def _get_atoma_feed(channel_id): def _get_channel_videos_first_page(channel_id, channel_status_name): try: - return channel.get_channel_first_page(channel_id=channel_id) + # First try the playlist method + pl_json = playlist.get_videos('UU' + channel_id[2:], 1, + include_shorts=False, report_text=None) + pl_info = yt_data_extract.extract_playlist_info(pl_json) + if pl_info.get('items'): + pl_info['items'] = pl_info['items'][0:30] + return pl_info + + # Try the channel api method + channel_json = channel.get_channel_first_page(channel_id=channel_id) + channel_info = yt_data_extract.extract_channel_info( + json.loads(channel_json), 'videos' + ) + return channel_info except util.FetchError as e: if e.code == '429' and settings.route_tor: error_message = ('Error checking channel ' + channel_status_name @@ -497,7 +510,7 @@ def _get_upstream_videos(channel_id): ) gevent.joinall(tasks) - channel_tab, feed = tasks[0].value, tasks[1].value + channel_info, feed = tasks[0].value, tasks[1].value # extract published times from atoma feed times_published = {} @@ -535,9 +548,8 @@ def _get_upstream_videos(channel_id): except defusedxml.ElementTree.ParseError: print('Failed to read atoma feed for ' + channel_status_name) - if channel_tab is None: # there was an error + if channel_info is None: # there was an error return - channel_info = yt_data_extract.extract_channel_info(json.loads(channel_tab), 'videos') if channel_info['error']: print('Error checking channel ' + channel_status_name + ': ' + channel_info['error']) return -- cgit v1.2.3