2016-03-26 5 views
2

Spotipyを使用するアーティストとアルバムの名前を指定してトラックを一覧表示しようとしています。アルバムからSpotipyのアルバムIDを取得する

これは非常に単純ですが、トラックIDを取得するためにalbumIDを取得する方法はわかりません。

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

...しか動作しません。

これまでのところ私はこれを持っています。それが正常に選択されたアーティストのためにアルバムを取得します(ハード、彼らは唯一の3枚のアルバムを持っていると私はしたくなかった以外には特別な理由のために、ここで「Phosgore」としてコード化された辞書が殺到取得するために):

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

答えて

3

ゲットアルバムのuri.album_tracks()メソッドに渡し:

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

プリント:作品

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

#1 alecxe!しかし、私がポイントを授与する前に、album_idがどういうもので、どのように動作しているのか教えてください。 PythonはUnicodeだと言っていますが、それは4つの項目のリストのようです。ありがとうございます –

+0

@GhoulFoolええ、私は、APIのドキュメントを見分けることが本当にIDとURIの形式を説明していると思います。https://developer.spotify.com/web-api/user-guide/#spotify-uris-andをご覧ください。 -ids。喜んで助けてください。ありがとう。 – alecxe

関連する問題