From 86bb312d6ddcea2992f7f5f6ac2b47203cd730ce Mon Sep 17 00:00:00 2001 From: Astound Date: Mon, 22 Jan 2024 06:03:16 +0800 Subject: Subscriptions: Fix exceptions when videos are missing upload dates E.g. line 548, AttributeError: 'NoneType' object has no attribute 'lower' When upload dates are unavailable, make ones up which give the correct video order --- youtube/subscriptions.py | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'youtube') diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index e37ac28..b5f3c83 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -568,13 +568,39 @@ def _get_upstream_videos(channel_id): if video_item['id'] in times_published: video_item['time_published'] = times_published[video_item['id']] video_item['is_time_published_exact'] = True - else: + elif video_item.get('time_published'): video_item['is_time_published_exact'] = False try: video_item['time_published'] = youtube_timestamp_to_posix(video_item['time_published']) - i # subtract a few seconds off the videos so they will be in the right order - except KeyError: + except Exception: print(video_item) + else: + video_item['is_time_published_exact'] = False + video_item['time_published'] = None + video_item['channel_id'] = channel_id + if len(videos) > 1: + # Go back and fill in any videos that don't have a time published + # using the time published of the surrounding ones + for i in range(len(videos)-1): + if (videos[i+1]['time_published'] is None + and videos[i]['time_published'] is not None + ): + videos[i+1]['time_published'] = videos[i]['time_published'] - 1 + for i in reversed(range(1,len(videos))): + if (videos[i-1]['time_published'] is None + and videos[i]['time_published'] is not None + ): + videos[i-1]['time_published'] = videos[i]['time_published'] + 1 + # Special case: none of the videos have a time published. + # In this case, make something up + if videos and videos[0]['time_published'] is None: + assert all(v['time_published'] is None for v in videos) + now = time.time() + for i in range(len(videos)): + # 1 month between videos + videos[i]['time_published'] = now - i*3600*24*30 + video_item['channel_id'] = channel_id if len(videos) == 0: -- cgit v1.2.3