英文:
Current playlist with Spotipy
问题
I'm trying to loop over the tracks in the current playlist using Spotipy.
然而,我遇到了两个障碍:
-
试图理解从Spotipy的
sp.current_user_playing_track()
中获取的信息格式 - 我尝试了JSON格式化打印,但由于"无效"而未成功。 -
访问当前歌曲所在的播放列表
这是我目前的代码:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
MY_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SECRET = "000000000000000000000000000000000"
# 授权
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 = "my_callback"
token = util.prompt_for_user_token(username, scope, client_id=MY_ID, client_secret=SECRET, redirect_uri=redirect_uri)
# sp 作为 Spotify - 使它更容易
sp = spotipy.Spotify(auth=token)
current_track = sp.current_user_playing_track()
song_artist = current_track["item"]["artists"][0]["name"]
print(song_artist)
## 试图获取当前播放列表
# current_playlist = current_track["item"]["playlist"] # 错误,但我现在知道了
# current_playlist = current_track["item"]["playlist"][0] # 错误,但我现在知道了
# current_playlist = current_track["context"]["playlist"] # 错误,但我现在知道了
# current_playlist = current_track["context"] # 无打印
# current_playlist = current_track["external_urls"] # 键错误:"external_urls"
(Note: I've translated the code comments for clarity.)
英文:
I'm trying to loop over the tracks in the current playlist using Spotipy.
However, I've got two stumbling blocks:
1, Being about to work out the format of information from Spotipy sp.current_user_playing_track()
- I tried a JSON pretty print which didn't work as it was "invalid"
2, Accessing the current playlist from the current track
This is what I have so far:
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 = "my_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"
答案1
得分: 1
Your code works, I tested it with my setup information.
我测试了你的代码,它可以正常工作。
I think you missing your credentials or not set up correctly in your the developer dashboard.
我认为你可能遗漏了你的凭据或在开发者仪表板中设置不正确。
But as mentioned 'Ximzend' mixed Client Credential Flow and Authorization Code Flow.
但如 'Ximzend' 所提到的,混合了客户端凭据流和授权码流。
Authorization Code Flow
授权码流 Authorization Code Flow vs. Client Credentials Flow
and it would be better my version code.
并且我的版本代码会更好。
It is not a necessary username. I need to log in the user.
这不是一个必要的用户名。我需要登录用户。
as get-songs-v1.py
作为 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'])
Result v1.py
I copy my credential information from the dashboard.
username copy from my user account of profile
Simple version v2
简化版本 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()
Result of v2
#2 Get songs name and artist name.
get artist name <- ['item']['artists'][0]['name']
get song name <- ['item']['album']['artist'][0]['name']
VS code with debugging of Python.
VS Code 中用于调试Python的代码。
OR Postman
或者使用 Postman
how to get token in here
[
means array, you need to access by index zero.
英文:
Your code works, I tested it with my setup information.
I think you missing your credentials or not set up correctly in your the developer dashboard.
But as mentioned 'Ximzend' mixed Client Credential Flow and Authorization Code Flow.
Authorization Code Flow
Authorization Code Flow vs. Client Credentials Flow
and it would be better my version code.
It is not a necessary username. I need to log in the user.
as 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'])
Result v1.py
I copy my credential information from the dashboard.
username copy from my user account of profile
Simple version 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()
Result of v2
#2 Get songs name and artist name.
get artist name <- ['item']['artists'][0]['name']
get song name <- ['item']['album']['artist'][0]['name']
VS code with debugging of Python.
OR Postman
how to get token in here
[
means array, you need to access by index zero.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论