Improve error handling surrounding most_recent_video.

This commit is contained in:
voussoir 2020-10-02 12:31:39 -07:00
parent b2115f88be
commit 6cc5fed930
4 changed files with 15 additions and 10 deletions

View file

@ -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):

View file

@ -27,7 +27,10 @@ class Channel(Base):
self.automark = db_row['automark'] or "pending"
def _rss_assisted_videos(self):
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'

View file

@ -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

View file

@ -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]