Current playlist with Spotipy

huangapple go评论61阅读模式
英文:

Current playlist with Spotipy

问题

I'm trying to loop over the tracks in the current playlist using 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"

# 授权
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

Current playlist with Spotipy

I copy my credential information from the dashboard.

Current playlist with Spotipy

username copy from my user account of profile
Current playlist with Spotipy

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

Current playlist with Spotipy

#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的代码。
Current playlist with Spotipy

OR Postman
或者使用 Postman
how to get token in here

[ means array, you need to access by index zero.

Current playlist with Spotipy

英文:

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   = &quot;&lt;your client id&gt;&quot;
SECRET = &quot;&lt;your client secret&gt;&quot;

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

username = &quot;&lt;your user name&gt;&quot;
scope    = &quot;user-read-currently-playing&quot;
redirect_uri = &quot;&lt;your redirect URI&gt;&quot;
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[&quot;item&quot;]
song_artist = current_track[&quot;item&quot;][&quot;artists&quot;][0][&quot;name&quot;]
print(song_artist,&quot; - &quot;, song[&#39;name&#39;])

Result v1.py

Current playlist with Spotipy

I copy my credential information from the dashboard.

Current playlist with Spotipy

username copy from my user account of profile
Current playlist with Spotipy

Simple version v2

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id = &#39;&lt;your client id&gt;&#39;,
    client_secret= &#39;&lt;your client secret&gt;&#39;,
    redirect_uri=&#39;&lt;your redirect URI&gt;&#39;,
    scope=&#39;user-read-currently-playing&#39;))

def Get_Current_Song():
    response_current_track = sp.current_user_playing_track()
    if response_current_track  is not None and response_current_track[&quot;item&quot;] is not None:
        song = response_current_track[&quot;item&quot;]
        print(song[&#39;artists&#39;][0][&#39;name&#39;], &quot; - &quot;, song[&#39;name&#39;])

Get_Current_Song()

Result of v2

Current playlist with Spotipy

#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.
Current playlist with Spotipy

OR Postman
how to get token in here

[ means array, you need to access by index zero.

Current playlist with Spotipy

huangapple
  • 本文由 发表于 2023年5月6日 17:04:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188057.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定