diff options
-rw-r--r-- | youtube/channel.py | 8 | ||||
-rw-r--r-- | youtube/subscriptions.py | 34 |
2 files changed, 36 insertions, 6 deletions
diff --git a/youtube/channel.py b/youtube/channel.py index 9d0532a..de75eaa 100644 --- a/youtube/channel.py +++ b/youtube/channel.py @@ -83,13 +83,15 @@ def channel_ctoken(channel_id, page, sort, tab, view=1): return base64.urlsafe_b64encode(pointless_nest).decode('ascii') -def get_channel_tab(channel_id, page="1", sort=3, tab='videos', view=1): +def get_channel_tab(channel_id, page="1", sort=3, tab='videos', view=1, print_status=True): ctoken = channel_ctoken(channel_id, page, sort, tab, view).replace('=', '%3D') url = "https://www.youtube.com/browse_ajax?ctoken=" + ctoken - print("Sending channel tab ajax request") + if print_status: + print("Sending channel tab ajax request") content = util.fetch_url(url, util.desktop_ua + headers_1, debug_name='channel_tab') - print("Finished recieving channel tab response") + if print_status: + print("Finished recieving channel tab response") return content diff --git a/youtube/subscriptions.py b/youtube/subscriptions.py index 27cc5c7..739b2c5 100644 --- a/youtube/subscriptions.py +++ b/youtube/subscriptions.py @@ -354,13 +354,15 @@ def check_channels_if_necessary(channel_ids): def _get_upstream_videos(channel_id): try: - print("Checking channel: " + channel_names[channel_id]) + channel_status_name = channel_names[channel_id] except KeyError: - print("Checking channel " + channel_id) + channel_status_name = channel_id + + print("Checking channel: " + channel_status_name) videos = [] - channel_videos = channel.extract_info(json.loads(channel.get_channel_tab(channel_id)), 'videos')['items'] + channel_videos = channel.extract_info(json.loads(channel.get_channel_tab(channel_id, print_status=False)), 'videos')['items'] for i, video_item in enumerate(channel_videos): if 'description' not in video_item: video_item['description'] = '' @@ -387,6 +389,24 @@ def _get_upstream_videos(channel_id): with open_database() as connection: 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[1] == latest_video_id: + break + index += 1 + number_of_new_videos = index + cursor.executemany('''INSERT OR IGNORE INTO videos (sql_channel_id, video_id, title, duration, time_published, description) VALUES ((SELECT id FROM subscribed_channels WHERE yt_channel_id=?), ?, ?, ?, ?, ?)''', videos) cursor.execute('''UPDATE subscribed_channels @@ -397,6 +417,14 @@ def _get_upstream_videos(channel_id): if not _is_muted(cursor, channel_id): autocheck_job_application.put({'channel_id': channel_id, 'channel_name': channel_names[channel_id], 'next_check_time': next_check_time}) + if number_of_new_videos == 0: + print('No new videos from ' + channel_status_name) + elif number_of_new_videos == 1: + print('1 new video from ' + channel_status_name) + else: + print(str(number_of_new_videos) + ' new videos from ' + channel_status_name) + + def check_all_channels(): with open_database() as connection: |