From 9faaea8186920af2203e38f3dfab82df8c34d386 Mon Sep 17 00:00:00 2001 From: Maks Snegov Date: Sat, 3 Feb 2024 15:06:10 -0800 Subject: [PATCH] Handle cases with unavailable videos --- main.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index b1bf9b1..0d7abd8 100755 --- a/main.py +++ b/main.py @@ -118,6 +118,8 @@ def add_video_to_playlist(yt_api, video_id: str, playlist_id: str, dry_run: bool = False) -> bool: playlist_name = get_playlist_name(playlist_id) video_info = get_video_info(yt_api, video_id) + if not video_info: + return False video_title = _truncate_title(video_info['snippet']['title']) try: if dry_run: @@ -147,6 +149,8 @@ def remove_video_from_playlist(yt_api, plitem_id: str, playlist_id: str, dry_run: bool = False) -> bool: playlist_name = get_playlist_name(playlist_id) plitem_info = get_playlistitem_info(yt_api, plitem_id) + if not plitem_info: + return False video_title = _truncate_title(plitem_info['snippet']['title']) video_id = plitem_info['snippet']['resourceId']['videoId'] try: @@ -199,11 +203,14 @@ def get_video_info(youtube, video_id: str): part="snippet", id=video_id ).execute() - return response['items'][0] except HttpError as e: exit_on_exceeded_quota(e) print(f'Error getting video {video_id}: {e}') return None + if not response['items']: + print(f'Video {video_id} not found') + return None + return response['items'][0] def get_playlistitem_info(youtube, playlistitem_id: str): @@ -212,11 +219,14 @@ def get_playlistitem_info(youtube, playlistitem_id: str): part="snippet", id=playlistitem_id ).execute() - return response['items'][0] except HttpError as e: exit_on_exceeded_quota(e) print(f'Error getting playlist item {playlistitem_id}: {e}') return None + if not response['items']: + print(f'Playlist item {playlistitem_id} not found') + return None + return response['items'][0] def main(): @@ -307,7 +317,6 @@ def main(): continue add_video_to_playlist(youtube, video_id, playlist, args.dry_run) - elif args.command == "dups": processed = 0 playlist_id = get_playlist_id(args.playlist)