Handle cases with unavailable videos
This commit is contained in:
parent
07d28c6d4f
commit
9faaea8186
15
main.py
15
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user