From af334a8ac711cf8dd2c36cd770a51506979ae528 Mon Sep 17 00:00:00 2001 From: James Taylor Date: Thu, 27 Feb 2020 11:21:42 -0800 Subject: [PATCH] Fix subscriptions new video count when there are deleted videos It would be 30 since the old method looked to see where the latest video in the database is in the new batch of videos. New method finds the first video in the new batch which is in the database. --- youtube/subscriptions.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index 92989635..66490de9 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -531,22 +531,19 @@ def find_element(base, tag_name): with connection as cursor: # calculate how many new videos there are - row = cursor.execute('''SELECT video_id - FROM videos - INNER JOIN subscribed_channels ON videos.sql_channel_id = subscribed_channels.id - WHERE yt_channel_id=? - ORDER BY time_published DESC - LIMIT 1''', [channel_id]).fetchone() - if row is None: - number_of_new_videos = len(videos) - else: - latest_video_id = row[0] - index = 0 - for video in videos: - if video['id'] == latest_video_id: - break - index += 1 - number_of_new_videos = index + existing_vids = set(row[0] for row in cursor.execute( + '''SELECT video_id + FROM videos + INNER JOIN subscribed_channels + ON videos.sql_channel_id = subscribed_channels.id + WHERE yt_channel_id=? + ORDER BY time_published DESC + LIMIT 30''', [channel_id]).fetchall()) + number_of_new_videos = 0 + for video in videos: + if video['id'] in existing_vids: + break + number_of_new_videos += 1 is_first_check = cursor.execute('''SELECT time_last_checked FROM subscribed_channels WHERE yt_channel_id=?''', [channel_id]).fetchone()[0] in (None, 0) time_videos_retrieved = int(time.time())