""" Basic Spotify synchronization manager. Author(s): eeriemyxi (GitHub) License: GPL 3.0 """ import pathlib, subprocess, sys, logging, argparse SCRIPT_PATH = pathlib.Path(__file__).parent logging.basicConfig( level=logging.INFO, filename=SCRIPT_PATH / "spotify-sync.log", format="%(asctime)s: %(message)s", ) logger = logging.getLogger(__name__) parser = argparse.ArgumentParser(description="Basic Spotify synchronization manager.") parser.add_argument( "--duplicate-removal-method", help="Delete duplicates after syncing. NONE by default. Uses czkawka. " "See `czkawka dup --help`", default="NONE", dest="removal_method", ) args = parser.parse_args() args.removal_method = args.removal_method.upper() logger.info("Script started with arguments: %s.", repr(sys.argv)) with open(SCRIPT_PATH / "playlists-to-sync.txt") as file: for line_c, line in enumerate(file.readlines(), 1): line = line.strip() if line[0] == "#": sys.stderr.write(f"Skipping line #{line_c}.\n") logger.warning(f"Skipping line #{line_c}.") continue links, folder_name = line.split(";", 1) links = links.split() logger.info(f"Downloading to folder {folder_name} from links: {links}.") playlist_path = SCRIPT_PATH / folder_name playlist_path.mkdir(parents=True, exist_ok=True) sync_file = playlist_path / "spotify-sync.spotdl" if sync_file.exists(): subprocess.run( ["spotdl", "sync", sync_file.name], cwd=playlist_path, shell=True ) else: subprocess.run( ["spotdl", "sync", " ".join(links), "--save-file", sync_file.name], cwd=playlist_path, shell=True, ) logger.info(f"Done synching playlists to folder {folder_name}.") if args.removal_method != "NONE": logger.info( "Deleting duplicates with method '%s' in '%s'.", args.removal_method, SCRIPT_PATH, ) out = subprocess.run( [ "czkawka", "dup", "-D", args.removal_method, "--directories", str(SCRIPT_PATH), ], shell=True, capture_output=True, ) logger.info( "Deletion complete with return code %s; stdout: %s; stderr: %s", out.returncode, out.stdout, out.stderr, )