Log video titles
This commit is contained in:
parent
6f4cdc66c7
commit
4f6ff53b6f
19
main.py
19
main.py
@ -13,6 +13,15 @@ from googleapiclient.errors import HttpError
|
||||
_playlists = {}
|
||||
|
||||
|
||||
def _truncate_title(title: str, length: int = 30) -> str:
|
||||
if length <= 0:
|
||||
return ""
|
||||
if length <= 3:
|
||||
return title[:length]
|
||||
if len(title) <= length:
|
||||
return title
|
||||
return title[:length-3] + (title[length:] and '...')
|
||||
|
||||
def get_yt_creds():
|
||||
""" Get YouTube API credentials """
|
||||
creds = None
|
||||
@ -94,6 +103,7 @@ def get_videos(yt_api, playlist_id):
|
||||
def add_video_to_playlist(yt_api, video, playlist_id) -> bool:
|
||||
playlist_name = get_playlist_name(playlist_id)
|
||||
video_id = video['snippet']['resourceId']['videoId']
|
||||
video_title = _truncate_title(video['snippet']['title'])
|
||||
try:
|
||||
yt_api.playlistItems().insert(
|
||||
part='snippet',
|
||||
@ -107,24 +117,25 @@ def add_video_to_playlist(yt_api, video, playlist_id) -> bool:
|
||||
}
|
||||
}
|
||||
).execute()
|
||||
print(f'Added video {video_id} to playlist {playlist_name}')
|
||||
print(f"Added video '{video_title}' [{video_id}] to playlist {playlist_name}")
|
||||
return True
|
||||
except HttpError as e:
|
||||
print(f'Error adding video {video_id} to playlist {playlist_name}: {e}')
|
||||
print(f"Error adding video '{video_title}' [{video_id}] to playlist {playlist_name}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def remove_video_from_playlist(yt_api, video, playlist_id) -> bool:
|
||||
playlist_name = get_playlist_name(playlist_id)
|
||||
video_id = video['snippet']['resourceId']['videoId']
|
||||
video_title = _truncate_title(video['snippet']['title'])
|
||||
try:
|
||||
yt_api.playlistItems().delete(
|
||||
id=video['id']
|
||||
).execute()
|
||||
print(f'Removed video {video_id} from playlist {playlist_name}')
|
||||
print(f"Removed video '{video_title}' [{video_id}] from playlist {playlist_name}")
|
||||
return True
|
||||
except HttpError as e:
|
||||
print(f'Error removing video {video_id} from playlist {playlist_name}: {e}')
|
||||
print(f"Error removing video '{video_title}' [{video_id}] from playlist {playlist_name}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user