From 6cc5fed93007a4f226129e53c08e05246e254c43 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 2 Oct 2020 12:31:39 -0700 Subject: [PATCH] Improve error handling surrounding most_recent_video. --- ycdl/exceptions.py | 3 +++ ycdl/objects.py | 11 ++++++++--- ycdl/ycdldb.py | 4 ++-- ycdl/ytrss.py | 7 ++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/ycdl/exceptions.py b/ycdl/exceptions.py index 7c18b7e..d177f13 100644 --- a/ycdl/exceptions.py +++ b/ycdl/exceptions.py @@ -52,6 +52,9 @@ class NoSuchChannel(YCDLException): class NoSuchVideo(YCDLException): error_message = 'Video {} does not exist.' +class NoVideos(YCDLException): + error_message = 'Channel {} has no videos.' + # VIDEO ERRORS ##################################################################################### class InvalidVideoState(YCDLException): diff --git a/ycdl/objects.py b/ycdl/objects.py index 2f1f4c0..94dae6c 100644 --- a/ycdl/objects.py +++ b/ycdl/objects.py @@ -27,7 +27,10 @@ class Channel(Base): self.automark = db_row['automark'] or "pending" def _rss_assisted_videos(self): - most_recent_video = self.get_most_recent_video_id() + try: + most_recent_video = self.get_most_recent_video_id() + except exceptions.NoVideos as exc: + raise exceptions.RSSAssistFailed(f'Channel has no videos to reference.') from exc new_ids = ytrss.get_user_videos_since(self.id, most_recent_video) videos = self.ycdldb.youtube.get_videos(new_ids) return videos @@ -42,8 +45,10 @@ class Channel(Base): def get_most_recent_video_id(self): query = 'SELECT id FROM videos WHERE author_id == ? ORDER BY published DESC LIMIT 1' bindings = [self.id] - most_recent_video = self.ycdldb.sql_select_one(query, bindings)[0] - return most_recent_video + row = self.ycdldb.sql_select_one(query, bindings) + if row is None: + raise exceptions.NoVideos(self) + return row[0] def has_pending(self): query = 'SELECT 1 FROM videos WHERE author_id == ? AND state == "pending" LIMIT 1' diff --git a/ycdl/ycdldb.py b/ycdl/ycdldb.py index df26b58..02f4291 100644 --- a/ycdl/ycdldb.py +++ b/ycdl/ycdldb.py @@ -184,10 +184,10 @@ class YCDLDBChannelMixin: def assisted(): for channel in self.get_channels(): - most_recent_video = channel.get_most_recent_video_id() try: + most_recent_video = channel.get_most_recent_video_id() new_ids = ytrss.get_user_videos_since(channel.id, most_recent_video) - except exceptions.RSSAssistFailed: + except (exceptions.NoVideos, exceptions.RSSAssistFailed): traditional(channel) continue yield from new_ids diff --git a/ycdl/ytrss.py b/ycdl/ytrss.py index 0efe6a3..c6db7ad 100644 --- a/ycdl/ytrss.py +++ b/ycdl/ytrss.py @@ -20,15 +20,12 @@ def get_user_videos(uid): try: return _get_user_videos(uid) except Exception: - raise exceptions.RSSAssistFailed() from exc + raise exceptions.RSSAssistFailed(f'Failed to fetch RSS videos.') from exc def get_user_videos_since(uid, most_recent_video): video_ids = get_user_videos(uid) try: index = video_ids.index(most_recent_video) - # log.debug(f'RSS contained {most_recent_video}.') except ValueError: - message = f'RSS didn\'t contain {most_recent_video}.' - # log.debug(message) - raise exceptions.RSSAssistFailed(message) + raise exceptions.RSSAssistFailed(f'RSS didn\'t contain {most_recent_video}.') return video_ids[:index]