From 562ef29153ccc6eb5eea6ac03c7a8c9edef2960c Mon Sep 17 00:00:00 2001 From: Joel Carlos Date: Wed, 18 Sep 2024 12:44:59 +0800 Subject: [PATCH] Append to playlist if its already existing --- spotify2ytmusic/backend.py | 29 +++++++++++++++++------------ spotify2ytmusic/gui.py | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/spotify2ytmusic/backend.py b/spotify2ytmusic/backend.py index ce4dfd7..0a97a34 100644 --- a/spotify2ytmusic/backend.py +++ b/spotify2ytmusic/backend.py @@ -453,7 +453,7 @@ def copy_playlist( ytmusic_playlist_id = get_playlist_id_by_name(yt, pl_name) print(f"Looking up playlist '{pl_name}': id={ytmusic_playlist_id}") - if ytmusic_playlist_id is None: + if not ytmusic_playlist_id or ytmusic_playlist_id is None: if pl_name == "": print("No playlist name or ID provided, creating playlist...") spotify_pls: dict = load_playlists_json() @@ -461,18 +461,23 @@ def copy_playlist( if len(pl.keys()) > 3 and pl["id"] == spotify_playlist_id: pl_name = pl["name"] - ytmusic_playlist_id = _ytmusic_create_playlist( - yt, - title=pl_name, - description=pl_name, - privacy_status=privacy_status, - ) + ytmusic_playlist_id = get_playlist_id_by_name(yt, pl_name) - # create_playlist returns a dict if there was an error - if isinstance(ytmusic_playlist_id, dict): - print(f"ERROR: Failed to create playlist: {ytmusic_playlist_id}") - sys.exit(1) - print(f"NOTE: Created playlist '{pl_name}' with ID: {ytmusic_playlist_id}") + if not ytmusic_playlist_id: + ytmusic_playlist_id = _ytmusic_create_playlist( + yt, + title=pl_name, + description=pl_name, + privacy_status=privacy_status, + ) + + # create_playlist returns a dict if there was an error + if isinstance(ytmusic_playlist_id, dict): + print(f"ERROR: Failed to create playlist: {ytmusic_playlist_id}") + sys.exit(1) + print(f"NOTE: Created playlist '{pl_name}' with ID: {ytmusic_playlist_id}") + else: + print(f"NOTE: '{pl_name}' already exists with ID: {ytmusic_playlist_id}. Appending...") copier( iter_spotify_playlist( diff --git a/spotify2ytmusic/gui.py b/spotify2ytmusic/gui.py index a755693..eb681f8 100644 --- a/spotify2ytmusic/gui.py +++ b/spotify2ytmusic/gui.py @@ -216,7 +216,7 @@ def __init__(self) -> None: command=lambda: self.call_func( func=backend.copy_playlist, args=( - self.spotify_playlist_id.get(), + self.spotify_playlist_id.get().strip(), self.yt_playlist_id.get(), "utf-8", False,