Skip to content

Commit fce8beb

Browse files
committed
added suport for playlist URLs and URIs
1 parent b785efb commit fce8beb

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

exportify-cli.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import os
6+
import re
67
import sys
78
from pathlib import Path
89
from typing import Any
@@ -48,7 +49,7 @@ def validate_config(config: configparser.ConfigParser) -> bool:
4849
missing = [k for k in required if not spotify_cfg.get(k, "").strip()]
4950
if missing:
5051
logger.error(
51-
f"Missing or empty keys in [spotify] section: {', '.join(missing)}"
52+
f"Missing or empty keys in [spotify] section: {', '.join(missing)}",
5253
)
5354
return False
5455
redirect = spotify_cfg["redirect_uri"].strip()
@@ -124,6 +125,13 @@ def load_config(config_path: Path) -> configparser.ConfigParser:
124125
return config
125126

126127

128+
def clean_playlist_input(playlist: list[str]) -> None:
129+
"""Detect playlist URLs or URIs and convert them to IDs."""
130+
for i, p in enumerate(playlist):
131+
playlist[i] = re.sub(r"^.*playlists?/([a-zA-Z0-9]{22}).*$", r"\1", p)
132+
playlist[i] = playlist[i].replace("spotify:playlist:", "")
133+
134+
127135
def init_spotify_client(cfg: configparser.ConfigParser) -> spotipy.Spotify:
128136
"""Initialize Spotify client with OAuth manager."""
129137
creds = cfg["spotify"]
@@ -182,6 +190,8 @@ def __init__(
182190
self.include_uris = include_uris
183191
self.external_ids = external_ids
184192
self.with_bar_flag = with_bar_flag
193+
self.exported_playlists = 0
194+
self.exported_tracks = 0
185195

186196
def _fetch_all_items(
187197
self,
@@ -412,6 +422,8 @@ def export_playlist(self, playlist: dict, output_dir: Path) -> None:
412422
export_data.append(record)
413423

414424
write_file(filepath, export_data, self.file_format)
425+
self.exported_playlists += 1
426+
self.exported_tracks += len(export_data)
415427
click.echo(
416428
f"Exported {len(export_data)} tracks from '{name}' to {filepath}",
417429
)
@@ -453,7 +465,7 @@ def export_playlist(self, playlist: dict, output_dir: Path) -> None:
453465
"-p",
454466
"playlist",
455467
multiple=True,
456-
help="Names or IDs of playlists to export",
468+
help="Names, URLs or IDs of playlists to export",
457469
)
458470
@click.option(
459471
"--list",
@@ -522,6 +534,9 @@ def main(
522534
with_bar_flag=bar_flag,
523535
)
524536

537+
playlist = list(playlist)
538+
clean_playlist_input(playlist)
539+
525540
fetched_playlists = exporter.get_playlists()
526541

527542
if list_only:
@@ -588,6 +603,13 @@ def main(
588603
for pl in targets:
589604
exporter.export_playlist(pl, out_dir)
590605

606+
if exporter.exported_playlists > 1:
607+
click.echo(
608+
f"Successfully exported {exporter.exported_tracks} tracks "
609+
f"from {exporter.exported_playlists} playlists.",
610+
)
611+
sys.exit(0)
612+
591613

592614
if __name__ == "__main__":
593615
main()

0 commit comments

Comments
 (0)