MCPcopy
hub / github.com/SathyaBhat/spotify-dl / fetch_tracks

Function fetch_tracks

spotify_dl/spotify.py:7–230  ·  view source on GitHub ↗

Fetches tracks from the provided item_id. :param sp: Spotify client :param item_type: Type of item being requested for: album/playlist/track :param item_id: id of the item :return Dictionary of song and artist

(sp, item_type, item_id)

Source from the content-addressed store, hash-verified

5
6
7def fetch_tracks(sp, item_type, item_id):
8 """
9 Fetches tracks from the provided item_id.
10 :param sp: Spotify client
11 :param item_type: Type of item being requested for: album/playlist/track
12 :param item_id: id of the item
13 :return Dictionary of song and artist
14 """
15 songs_list = []
16 offset = 0
17 songs_fetched = 0
18
19 if item_type == "playlist":
20 with Progress() as progress:
21 songs_task = progress.add_task(description="Fetching songs from playlist..")
22 while True:
23 items = sp.playlist_items(
24 playlist_id=item_id,
25 fields="items.track.name,items.track.artists(name, uri),"
26 "items.track.album(name, release_date, total_tracks, images),"
27 "items.track.track_number,total, next,offset,"
28 "items.track.id",
29 additional_types=["track"],
30 offset=offset,
31 )
32 total_songs = items.get("total")
33 track_info_task = progress.add_task(
34 description="Fetching track info", total=len(items["items"])
35 )
36 for item in items["items"]:
37 track_info = item.get("track")
38 # If the user has a podcast in their playlist, there will be no track
39 # Without this conditional, the program will fail later on when the metadata is fetched
40 if track_info is None:
41 offset += 1
42 continue
43 track_album_info = track_info.get("album")
44 track_num = track_info.get("track_number")
45 track_name = track_info.get("name")
46 spotify_id = track_info.get("id")
47 try:
48 track_audio_data = sp.audio_analysis(spotify_id)
49 tempo = track_audio_data.get("track").get("tempo")
50 except:
51 log.error("Couldn't fetch audio analysis for %s", track_name)
52 tempo = None
53 track_artist = ", ".join(
54 [artist["name"] for artist in track_info.get("artists")]
55 )
56 if track_album_info:
57 track_album = track_album_info.get("name")
58 track_year = (
59 track_album_info.get("release_date")[:4]
60 if track_album_info.get("release_date")
61 else ""
62 )
63 album_total = track_album_info.get("total_tracks")
64 if len(item["track"]["album"]["images"]) > 0:

Calls

no outgoing calls