Add --dry-run CLI parameter

This commit is contained in:
Maks Snegov 2024-02-02 11:54:56 -08:00
parent 4f6ff53b6f
commit 2e7002cdd4

23
main.py
View File

@ -100,11 +100,14 @@ def get_videos(yt_api, playlist_id):
return videos return videos
def add_video_to_playlist(yt_api, video, playlist_id) -> bool: def add_video_to_playlist(yt_api, video, playlist_id, dry_run: bool = False) -> bool:
playlist_name = get_playlist_name(playlist_id) playlist_name = get_playlist_name(playlist_id)
video_id = video['snippet']['resourceId']['videoId'] video_id = video['snippet']['resourceId']['videoId']
video_title = _truncate_title(video['snippet']['title']) video_title = _truncate_title(video['snippet']['title'])
try: try:
if dry_run:
print(f"Would add video '{video_title}' [{video_id}] to playlist {playlist_name}")
return True
yt_api.playlistItems().insert( yt_api.playlistItems().insert(
part='snippet', part='snippet',
body={ body={
@ -124,11 +127,14 @@ def add_video_to_playlist(yt_api, video, playlist_id) -> bool:
return False return False
def remove_video_from_playlist(yt_api, video, playlist_id) -> bool: def remove_video_from_playlist(yt_api, video, playlist_id, dry_run: bool = False) -> bool:
playlist_name = get_playlist_name(playlist_id) playlist_name = get_playlist_name(playlist_id)
video_id = video['snippet']['resourceId']['videoId'] video_id = video['snippet']['resourceId']['videoId']
video_title = _truncate_title(video['snippet']['title']) video_title = _truncate_title(video['snippet']['title'])
try: try:
if dry_run:
print(f"Would remove video '{video_title}' [{video_id}] from playlist {playlist_name}")
return True
yt_api.playlistItems().delete( yt_api.playlistItems().delete(
id=video['id'] id=video['id']
).execute() ).execute()
@ -139,7 +145,7 @@ def remove_video_from_playlist(yt_api, video, playlist_id) -> bool:
return False return False
def move_all_videos(yt_api, src_playlist: str, dst_playlist: str, limit: int = -1): def move_all_videos(yt_api, src_playlist: str, dst_playlist: str, limit: int = -1, dry_run: bool = False):
if limit < 0: if limit < 0:
limit = len(src_playlist) limit = len(src_playlist)
@ -152,8 +158,8 @@ def move_all_videos(yt_api, src_playlist: str, dst_playlist: str, limit: int = -
for src_video in src_videos[:limit]: for src_video in src_videos[:limit]:
video_id = src_video['snippet']['resourceId']['videoId'] video_id = src_video['snippet']['resourceId']['videoId']
if video_id not in dst_video_map: if video_id not in dst_video_map:
add_video_to_playlist(yt_api, src_video, dst_playlist) add_video_to_playlist(yt_api, src_video, dst_playlist, dry_run)
remove_video_from_playlist(yt_api, src_video, src_playlist) remove_video_from_playlist(yt_api, src_video, src_playlist, dry_run)
def main(): def main():
@ -163,6 +169,10 @@ def main():
parser.add_argument( parser.add_argument(
'-l', '--limit', help='Limit number of videos to move', type=int, default=-1 '-l', '--limit', help='Limit number of videos to move', type=int, default=-1
) )
parser.add_argument(
'-n', '--dry-run', action='store_true',
help='Dry run, do not send changes to YoutubeAPI'
)
args = parser.parse_args() args = parser.parse_args()
# Disable OAuthlib's HTTPS verification when running locally. # Disable OAuthlib's HTTPS verification when running locally.
@ -178,7 +188,8 @@ def main():
creds = get_yt_creds() creds = get_yt_creds()
youtube = build(api_service_name, api_version, credentials=creds) youtube = build(api_service_name, api_version, credentials=creds)
move_all_videos(youtube, src_playlist, dst_playlist, limit=args.limit) move_all_videos(youtube, src_playlist, dst_playlist,
limit=args.limit, dry_run=args.dry_run)
if __name__ == '__main__': if __name__ == '__main__':