Improve error handling surrounding most_recent_video.
This commit is contained in:
		
							parent
							
								
									b2115f88be
								
							
						
					
					
						commit
						6cc5fed930
					
				
					 4 changed files with 15 additions and 10 deletions
				
			
		|  | @ -52,6 +52,9 @@ class NoSuchChannel(YCDLException): | ||||||
| class NoSuchVideo(YCDLException): | class NoSuchVideo(YCDLException): | ||||||
|     error_message = 'Video {} does not exist.' |     error_message = 'Video {} does not exist.' | ||||||
| 
 | 
 | ||||||
|  | class NoVideos(YCDLException): | ||||||
|  |     error_message = 'Channel {} has no videos.' | ||||||
|  | 
 | ||||||
| # VIDEO ERRORS ##################################################################################### | # VIDEO ERRORS ##################################################################################### | ||||||
| 
 | 
 | ||||||
| class InvalidVideoState(YCDLException): | class InvalidVideoState(YCDLException): | ||||||
|  |  | ||||||
|  | @ -27,7 +27,10 @@ class Channel(Base): | ||||||
|         self.automark = db_row['automark'] or "pending" |         self.automark = db_row['automark'] or "pending" | ||||||
| 
 | 
 | ||||||
|     def _rss_assisted_videos(self): |     def _rss_assisted_videos(self): | ||||||
|  |         try: | ||||||
|             most_recent_video = self.get_most_recent_video_id() |             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) |         new_ids = ytrss.get_user_videos_since(self.id, most_recent_video) | ||||||
|         videos = self.ycdldb.youtube.get_videos(new_ids) |         videos = self.ycdldb.youtube.get_videos(new_ids) | ||||||
|         return videos |         return videos | ||||||
|  | @ -42,8 +45,10 @@ class Channel(Base): | ||||||
|     def get_most_recent_video_id(self): |     def get_most_recent_video_id(self): | ||||||
|         query = 'SELECT id FROM videos WHERE author_id == ? ORDER BY published DESC LIMIT 1' |         query = 'SELECT id FROM videos WHERE author_id == ? ORDER BY published DESC LIMIT 1' | ||||||
|         bindings = [self.id] |         bindings = [self.id] | ||||||
|         most_recent_video = self.ycdldb.sql_select_one(query, bindings)[0] |         row = self.ycdldb.sql_select_one(query, bindings) | ||||||
|         return most_recent_video |         if row is None: | ||||||
|  |             raise exceptions.NoVideos(self) | ||||||
|  |         return row[0] | ||||||
| 
 | 
 | ||||||
|     def has_pending(self): |     def has_pending(self): | ||||||
|         query = 'SELECT 1 FROM videos WHERE author_id == ? AND state == "pending" LIMIT 1' |         query = 'SELECT 1 FROM videos WHERE author_id == ? AND state == "pending" LIMIT 1' | ||||||
|  |  | ||||||
|  | @ -184,10 +184,10 @@ class YCDLDBChannelMixin: | ||||||
| 
 | 
 | ||||||
|         def assisted(): |         def assisted(): | ||||||
|             for channel in self.get_channels(): |             for channel in self.get_channels(): | ||||||
|                 most_recent_video = channel.get_most_recent_video_id() |  | ||||||
|                 try: |                 try: | ||||||
|  |                     most_recent_video = channel.get_most_recent_video_id() | ||||||
|                     new_ids = ytrss.get_user_videos_since(channel.id, most_recent_video) |                     new_ids = ytrss.get_user_videos_since(channel.id, most_recent_video) | ||||||
|                 except exceptions.RSSAssistFailed: |                 except (exceptions.NoVideos, exceptions.RSSAssistFailed): | ||||||
|                     traditional(channel) |                     traditional(channel) | ||||||
|                     continue |                     continue | ||||||
|                 yield from new_ids |                 yield from new_ids | ||||||
|  |  | ||||||
|  | @ -20,15 +20,12 @@ def get_user_videos(uid): | ||||||
|     try: |     try: | ||||||
|         return _get_user_videos(uid) |         return _get_user_videos(uid) | ||||||
|     except Exception: |     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): | def get_user_videos_since(uid, most_recent_video): | ||||||
|     video_ids = get_user_videos(uid) |     video_ids = get_user_videos(uid) | ||||||
|     try: |     try: | ||||||
|         index = video_ids.index(most_recent_video) |         index = video_ids.index(most_recent_video) | ||||||
|         # log.debug(f'RSS contained {most_recent_video}.') |  | ||||||
|     except ValueError: |     except ValueError: | ||||||
|         message = f'RSS didn\'t contain {most_recent_video}.' |         raise exceptions.RSSAssistFailed(f'RSS didn\'t contain {most_recent_video}.') | ||||||
|         # log.debug(message) |  | ||||||
|         raise exceptions.RSSAssistFailed(message) |  | ||||||
|     return video_ids[:index] |     return video_ids[:index] | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue