2016-03-26 9 views
2

Mit Spotipy versuche ich die Titel aufzulisten, indem ich den Namen des Künstlers und des Albums angegeben habe.Erhalte Spottip Album ID aus dem Album

Das sollte ziemlich einfach sein, aber ich weiß nicht, wie man die AlbumID bekommt, um die Tracklist zu bekommen. Ich dachte, es wäre etwas wie:

sp.album_tracks(q = "album:" + album, type = "album") 

... nur das funktioniert nicht.

Hier ist, was ich bisher habe. Es wird erfolgreich die Alben für ausgewählte Künstler (hart hier als „Phosgore“ codiert ohne besonderen Grund außer sie nur drei Alben haben und ich wollte nicht mit Wörterbuch überschwemmt werden):

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

# shows album from trackname 

import sys 
import spotipy 

def get_albums_from_artist_name(name): 

    results = sp.search(q = "artist:" + name, type = "artist") 
    items = results["artists"]["items"] 
    if len(items) == 0: 
    return None 
    else: 
    d = items[0] 

    # get artistID and artist name from dict 
    artID = d["id"]  # 3Cf1GbbU9uHlS3veYiAK2x 
    aName = d["name"] # Phosgore 

    artistUri = "spotify:artist:" + artID 

    results = sp.artist_albums(artistUri, album_type = "album") 
    albums = results["items"] 
    while results["next"]: 
    results = sp.next(results) 
    albums.extend(results["items"]) 

    unique = set() # ignore duplicate albums 
    for album in albums: 
    name = album["name"] 
    if not name in unique: 
     unique.add(name) # unique is a set 

    print ("\nAlbums by %s:\n" %(aName)) 
    unique = list(unique) 
    for i in range(0, len(unique)): 
    print unique[i] 

    # let's return a list instead of a set 
    return list(unique) 

#------------------------------------------------ 
def get_tracks_from_album(album): 
    tracks = [] 
    # results = sp.album_tracks(q = "album:" + album, type = "album") 
    # don't know how to get album id 
    # list tracks here 


sp = spotipy.Spotify() 
sp.trace = False 

ask = "Phosgore" 

artistAlbums = get_albums_from_artist_name(ask) 

get_tracks_from_album("Pestbringer") 

Antwort

3

Get uri des Albums und übergibt es an die .album_tracks() Methode:

import spotipy 

sp = spotipy.Spotify() 
sp.trace = False 

# find album by name 
album = "Pestbringer" 
results = sp.search(q = "album:" + album, type = "album") 

# get the first album uri 
album_id = results['albums']['items'][0]['uri'] 

# get album tracks 
tracks = sp.album_tracks(album_id) 
for track in tracks['items']: 
    print(track['name']) 

Drucke:

Embrace Our Gift 
Here Comes the Pain 
Pestbringer 
Dein Licht 
Aggression Incarnate 
Countdown to Destruction 
Nightmare 
Noise Monsters 
Lobotomy 
The Holy Inquisition 
Tote Musikanten 
+0

# alecxe, das funktioniert! Aber bevor ich die Punkte vergebe, kannst du mir bitte sagen, was genau das album_id ist und wie es funktioniert. Python sagt, es ist Unicode, aber es sieht aus wie eine Liste von 4 Elementen. Danke –

+0

@GhoulFool yeah, ich denke, dass die Spotify-API-Dokumentation das Format von IDs und URIs sehr gut erklärt, siehe https://developer.spotify.com/web-api/user-guide/#spotify-uris-and -ids. Froh, dass ich Helfen kann. Vielen Dank. – alecxe

Verwandte Themen