Exit on youtube quotes

This commit is contained in:
Maks Snegov 2024-02-02 14:37:29 -08:00
parent 32c372bb9b
commit 9a8d23592f

11
main.py
View File

@ -51,6 +51,14 @@ def get_yt_creds():
return creds return creds
def exit_on_exceeded_quota(e: HttpError):
if e.resp.status == 403:
for error in e.error_details:
if error.get('reason') == 'quotaExceeded':
print('Youtube quota exceeded, exiting')
sys.exit(1)
def read_playlists_file(): def read_playlists_file():
""" Read playlists.csv and return a dictionary of playlist names to playlist IDs """ """ Read playlists.csv and return a dictionary of playlist names to playlist IDs """
global _playlists global _playlists
@ -96,6 +104,7 @@ def get_videos(yt_api, playlist_id):
for item in response['items']: for item in response['items']:
videos.append(item) videos.append(item)
except HttpError as e: except HttpError as e:
exit_on_exceeded_quota(e)
print(f'Error getting video IDs from playlist {playlist_name}: {e}') print(f'Error getting video IDs from playlist {playlist_name}: {e}')
print(f'Fetched {fetched} videos from playlist {playlist_name}') print(f'Fetched {fetched} videos from playlist {playlist_name}')
@ -126,6 +135,7 @@ def add_video_to_playlist(yt_api, video, playlist_id,
print(f"Added video '{video_title}' [{video_id}] to playlist {playlist_name}") print(f"Added video '{video_title}' [{video_id}] to playlist {playlist_name}")
return True return True
except HttpError as e: except HttpError as e:
exit_on_exceeded_quota(e)
print(f"Error adding video '{video_title}' [{video_id}] to playlist {playlist_name}: {e}") print(f"Error adding video '{video_title}' [{video_id}] to playlist {playlist_name}: {e}")
return False return False
@ -147,6 +157,7 @@ def remove_video_from_playlist(yt_api, video, playlist_id,
f" from playlist {playlist_name}") f" from playlist {playlist_name}")
return True return True
except HttpError as e: except HttpError as e:
exit_on_exceeded_quota(e)
print(f"Error removing video '{video_title}' [{video_id}]" print(f"Error removing video '{video_title}' [{video_id}]"
f" from playlist {playlist_name}: {e}") f" from playlist {playlist_name}: {e}")
return False return False