Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect and parse new intl-xx urls #349

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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