Skip to content

Commit

Permalink
detect and parse new intl urls (#349)
Browse files Browse the repository at this point in the history
- rewrite fetch_tracks to use id instead of url

Co-authored-by: Sathyajith Bhat <[email protected]>
  • Loading branch information
Mattia98 and SathyaBhat authored Jul 12, 2023
1 parent efe5f7f commit de6ebe7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 11 additions & 10 deletions spotify_dl/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from rich.progress import Progress


def fetch_tracks(sp, item_type, url):
def fetch_tracks(sp, item_type, item_id):
"""
Fetches tracks from the provided URL.
Fetches tracks from the provided item_id.
:param sp: Spotify client
:param item_type: Type of item being requested for: album/playlist/track
:param url: URL of the item
:param item_id: id of the item
:return Dictionary of song and artist
"""
songs_list = []
Expand All @@ -21,7 +21,7 @@ def fetch_tracks(sp, item_type, url):
songs_task = progress.add_task(description="Fetching songs from playlist..")
while True:
items = sp.playlist_items(
playlist_id=url,
playlist_id=item_id,
fields="items.track.name,items.track.artists(name, uri),"
"items.track.album(name, release_date, total_tracks, images),"
"items.track.track_number,total, next,offset,"
Expand Down Expand Up @@ -111,8 +111,8 @@ def fetch_tracks(sp, item_type, url):
description="Fetching songs from the album.."
)
while True:
album_info = sp.album(album_id=url)
items = sp.album_tracks(album_id=url, offset=offset)
album_info = sp.album(album_id=item_id)
items = sp.album_tracks(album_id=item_id, offset=offset)
total_songs = items.get("total")
track_album = album_info.get("name")
track_year = (
Expand Down Expand Up @@ -168,7 +168,7 @@ def fetch_tracks(sp, item_type, url):
break

elif item_type == "track":
items = sp.track(track_id=url)
items = sp.track(track_id=item_id)
track_name = items.get("name")
album_info = items.get("album")
track_artist = ", ".join([artist["name"] for artist in items["artists"]])
Expand Down Expand Up @@ -219,9 +219,10 @@ def parse_spotify_url(url):
if url.startswith("spotify:"):
log.error("Spotify URI was provided instead of a playlist/album/track URL.")
sys.exit(1)
parsed_url = url.replace("https://open.spotify.com/", "").split("?")[0]
item_type = parsed_url.split("/")[0]
item_id = parsed_url.split("/")[1]
parsed_url = url.replace("https://open.spotify.com/", "").split("?")[0].split("/")
index_adjustment = 1 if parsed_url[0].startswith("intl") else 0
item_type = parsed_url[0+index_adjustment]
item_id = parsed_url[1+index_adjustment]
return item_type, item_id


Expand Down
2 changes: 1 addition & 1 deletion spotify_dl/spotify_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def spotify_dl():
)
url_dict["save_path"].mkdir(parents=True, exist_ok=True)
log.info("Saving songs to %s directory", directory_name)
url_dict["songs"] = fetch_tracks(sp, item_type, url)
url_dict["songs"] = fetch_tracks(sp, item_type, item_id)
url_data["urls"].append(url_dict.copy())
if args.dump_json is True:
dump_json(url_dict["songs"])
Expand Down

0 comments on commit de6ebe7

Please sign in to comment.