diff --git a/main.py b/main.py index 154621d..cec6a06 100755 --- a/main.py +++ b/main.py @@ -100,11 +100,14 @@ def get_videos(yt_api, playlist_id): 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) video_id = video['snippet']['resourceId']['videoId'] video_title = _truncate_title(video['snippet']['title']) try: + if dry_run: + print(f"Would add video '{video_title}' [{video_id}] to playlist {playlist_name}") + return True yt_api.playlistItems().insert( part='snippet', body={ @@ -124,11 +127,14 @@ def add_video_to_playlist(yt_api, video, playlist_id) -> bool: 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) video_id = video['snippet']['resourceId']['videoId'] video_title = _truncate_title(video['snippet']['title']) try: + if dry_run: + print(f"Would remove video '{video_title}' [{video_id}] from playlist {playlist_name}") + return True yt_api.playlistItems().delete( id=video['id'] ).execute() @@ -139,7 +145,7 @@ def remove_video_from_playlist(yt_api, video, playlist_id) -> bool: 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: 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]: video_id = src_video['snippet']['resourceId']['videoId'] if video_id not in dst_video_map: - add_video_to_playlist(yt_api, src_video, dst_playlist) - remove_video_from_playlist(yt_api, src_video, src_playlist) + add_video_to_playlist(yt_api, src_video, dst_playlist, dry_run) + remove_video_from_playlist(yt_api, src_video, src_playlist, dry_run) def main(): @@ -163,6 +169,10 @@ def main(): parser.add_argument( '-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() # Disable OAuthlib's HTTPS verification when running locally. @@ -178,7 +188,8 @@ def main(): creds = get_yt_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__':