robocyp.py improve error handling
This commit is contained in:
parent
56f589b858
commit
bed1581cf0
46
robocyp.py
46
robocyp.py
@ -3,6 +3,7 @@ import argparse
|
|||||||
import csv
|
import csv
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Optional, Iterable
|
||||||
|
|
||||||
from google.auth.transport.requests import Request
|
from google.auth.transport.requests import Request
|
||||||
from google.auth.exceptions import RefreshError
|
from google.auth.exceptions import RefreshError
|
||||||
@ -21,6 +22,11 @@ DEFAULT_FORMAT = "bestvideo[height<=720][ext=mp4]+bestaudio/best[height<=720]"
|
|||||||
program_dir = os.path.dirname(os.path.realpath(__file__))
|
program_dir = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorReasons:
|
||||||
|
QUOTA_EXCEEDED = 'quotaExceeded'
|
||||||
|
PLAYLIST_ITEMS_NOT_ACCESSIBLE = 'playlistItemsNotAccessible'
|
||||||
|
|
||||||
|
|
||||||
def _truncate_title(title: str, length: int = 30) -> str:
|
def _truncate_title(title: str, length: int = 30) -> str:
|
||||||
if length <= 0:
|
if length <= 0:
|
||||||
return ""
|
return ""
|
||||||
@ -71,12 +77,15 @@ def get_yt_creds():
|
|||||||
return creds
|
return creds
|
||||||
|
|
||||||
|
|
||||||
def exit_on_exceeded_quota(e: HttpError):
|
def handle_http_error(
|
||||||
if e.resp.status == 403:
|
e: HttpError,
|
||||||
for error in e.error_details:
|
msg: Optional[str] = None,
|
||||||
if error.get('reason') == 'quotaExceeded':
|
reasons_to_abort: Optional[Iterable[str]] = (ErrorReasons.QUOTA_EXCEEDED,)
|
||||||
print('Youtube quota exceeded, exiting')
|
):
|
||||||
sys.exit(1)
|
for error in e.error_details:
|
||||||
|
print(f"{msg}: {error.get('message')}")
|
||||||
|
if error.get('reason') in reasons_to_abort:
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def read_playlists_file():
|
def read_playlists_file():
|
||||||
@ -127,8 +136,7 @@ def list_playlist(yt_api, playlist_id: str):
|
|||||||
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)
|
handle_http_error(e, f'Error getting video IDs from playlist {playlist_name}')
|
||||||
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}')
|
||||||
return videos
|
return videos
|
||||||
@ -162,9 +170,11 @@ def add_video_to_playlist(yt_api, video_id: str, playlist_id: str,
|
|||||||
f" to playlist {playlist_name}")
|
f" to playlist {playlist_name}")
|
||||||
return True
|
return True
|
||||||
except HttpError as e:
|
except HttpError as e:
|
||||||
exit_on_exceeded_quota(e)
|
msg = (f"Error adding video '{video_title}' [{video_id}]"
|
||||||
print(f"Error adding video '{video_title}' [{video_id}]"
|
f" to playlist {playlist_name}")
|
||||||
f" to playlist {playlist_name}: {e}")
|
reasons_to_abort = (ErrorReasons.QUOTA_EXCEEDED,
|
||||||
|
ErrorReasons.PLAYLIST_ITEMS_NOT_ACCESSIBLE)
|
||||||
|
handle_http_error(e, msg, reasons_to_abort)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -186,9 +196,9 @@ def remove_video_from_playlist(yt_api, plitem_id: str, playlist_id: str,
|
|||||||
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)
|
msg = (f"Error removing video '{video_title}' [{video_id}]"
|
||||||
print(f"Error removing video '{video_title}' [{video_id}]"
|
f" from playlist {playlist_name}")
|
||||||
f" from playlist {playlist_name}: {e}")
|
handle_http_error(e, msg)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -233,8 +243,8 @@ def get_video_info(youtube, video_id: str):
|
|||||||
id=video_id
|
id=video_id
|
||||||
).execute()
|
).execute()
|
||||||
except HttpError as e:
|
except HttpError as e:
|
||||||
exit_on_exceeded_quota(e)
|
msg = f'Error getting video info for {video_id}'
|
||||||
print(f'Error getting video {video_id}: {e}')
|
handle_http_error(e, msg)
|
||||||
return None
|
return None
|
||||||
if not response['items']:
|
if not response['items']:
|
||||||
print(f'Video {video_id} not found')
|
print(f'Video {video_id} not found')
|
||||||
@ -249,8 +259,8 @@ def get_playlistitem_info(youtube, playlistitem_id: str):
|
|||||||
id=playlistitem_id
|
id=playlistitem_id
|
||||||
).execute()
|
).execute()
|
||||||
except HttpError as e:
|
except HttpError as e:
|
||||||
exit_on_exceeded_quota(e)
|
msg = f'Error getting playlist item {playlistitem_id}'
|
||||||
print(f'Error getting playlist item {playlistitem_id}: {e}')
|
handle_http_error(e, msg)
|
||||||
return None
|
return None
|
||||||
if not response['items']:
|
if not response['items']:
|
||||||
print(f'Playlist item {playlistitem_id} not found')
|
print(f'Playlist item {playlistitem_id} not found')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user