Spotipy 的当前播放列表

问题描述 投票:0回答:1

我正在尝试使用 Spotipy 循环播放当前播放列表中的曲目。

然而,我有两个绊脚石:

1,即将从 Spotipy 中找出信息的格式

sp.current_user_playing_track()
- 我尝试了一个 JSON 漂亮的打印,但没有用,因为它“无效”

2、从当前曲目访问当前播放列表

这是我目前所拥有的:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util

MY_ID   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SECRET = "000000000000000000000000000000000"

# Authorise
spoti = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=MY_ID,
                                                           client_secret=SECRET))
spoti.trace = False

username = "xxxxxxxxxxxxxxxxxxx"
scope    = "user-read-currently-playing"
redirect_uri = "http://localhost:8888/callback/"
token = util.prompt_for_user_token(username, scope, client_id=MY_ID, client_secret=SECRET, redirect_uri=redirect_uri)

# sp as spotify - makes it easier
sp = spotipy.Spotify(auth=token)

current_track = sp.current_user_playing_track()
song_artist = current_track["item"]["artists"][0]["name"]
print(song_artist)

## trying to get current playlist
# current_playlist = current_track["item"]["playlist"] # wrong, but I know that now
# current_playlist = current_track["item"]["playlist"][0] # wrong, but I know that now
# current_playlist = current_track["context"]["playlist"] # keyError: "playlist"
# current_playlist = current_track["context"] # no print
# current_playlist = current_track["external_urls"] # keyError: "external_urls"
python-3.x spotify spotipy
1个回答
0
投票

你的代码有效,我用我的设置信息测试了它。 我认为您在开发人员仪表板中丢失或设置不正确。

但如前所述,“Ximzend”混合了客户端凭证流和授权代码流。

授权码流程 授权码流程 vs. 客户端凭证流程

我的版本代码会更好。 它不是必需的用户名。我需要登录用户。

作为 get-songs-v1.py

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util

MY_ID   = "<your client id>"
SECRET = "<your client secret>"

# Authorise
spoti = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=MY_ID,
                                                           client_secret=SECRET))
spoti.trace = False

username = "<your user name>"
scope    = "user-read-currently-playing"
redirect_uri = "<your redirect URI>"
token = util.prompt_for_user_token(username, scope, client_id=MY_ID, client_secret=SECRET, redirect_uri=redirect_uri)

# sp as spotify - makes it easier
sp = spotipy.Spotify(auth=token)

current_track = sp.current_user_playing_track()
song = current_track["item"]
song_artist = current_track["item"]["artists"][0]["name"]
print(song_artist," - ", song['name'])

结果 v1.py

我从仪表板复制我的凭证信息。

从我的个人资料用户帐户复制用户名

简易版v2

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id = '<your client id>',
    client_secret= '<your client secret>',
    redirect_uri='<your redirect URI>',
    scope='user-read-currently-playing'))

def Get_Current_Song():
    response_current_track = sp.current_user_playing_track()
    if response_current_track  is not None and response_current_track["item"] is not None:
        song = response_current_track["item"]
        print(song['artists'][0]['name'], " - ", song['name'])

Get_Current_Song()

v2的结果

#2 获取歌曲名称和艺术家姓名。

获取艺术家姓名<- [item']['artists'][0]['name'] get song name <- [item']['album']['artist'][0]['name']

带有 Python 调试的 VS 代码。

或邮递员

[
表示数组,您需要通过索引零访问。

© www.soinside.com 2019 - 2024. All rights reserved.