2016-03-26 14 views
2

Spotipy'yi kullanma Şarkıları ve albümü adı verilen parçaları listelemeye çalışıyorum.Get Spotipy albüm kimliği albümden

Bu oldukça basit olmalı, ancak şarkı listesini almak için albüm kimliğini nasıl alacağımı bilmiyorum. Bunun gibi bir şey olacağını düşündüm:

... sadece bu çalışmaz.

Şimdiye dek elime geçenler. Başarıyla seçilen sanatçının albümleri alacak (sert onlar sadece üç albümü ve istemediğim başka belli bir nedenden dolayı burada "Phosgore" olarak kodlanmış sözlükle gömülmek üzere):

#!/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") 

cevap

3

alın albümün uri ve .album_tracks() yöntemine geçmek:

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']) 

Baskılar: çalışır

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! Ama puanları vermeden önce lütfen albüm_id'in tam olarak ne olduğunu ve nasıl çalıştığını söyleyebilirsiniz. Python, unicode olduğunu söylüyor ama 4 öğenin bir listesi gibi görünüyor. Teşekkürler –

+0

@GhoulFool evet, spotify API belgelerinin kimlikleri ve URI'lerin formatını açıklamakta gerçekten iyi olduğunu düşünüyorum, lütfen https://developer.spotify.com/web-api/user-guide/#spotify-uris-and adresini ziyaret edin. -ids. Yardım etmekten memnun oldum. Teşekkürler. – alecxe