我没有看到一个完整的很多信息current_user_playing_track()
的Spotipy,所以希望有人能够帮助我在这方面。
import spotipy
import spotipy.util as util
from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth
import json
import requests
cid = 'my client id'
secret = 'my client secrets'
redirect_uri = 'http://localhost:7777/callback'
username = 'my username'
scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, cid, secret, redirect_uri)
spotify = spotipy.Spotify(token)
def get_current_song():
# Get track information
track = spotify.current_user_playing_track()
print(json.dumps(track, sort_keys=True, indent=4))
artist = json.dumps(track['item']['album']['artists'][0]['name'])
track = json.dumps(track['item']['name'])
return artist, track
def main():
artist, track = get_current_song()
if artist !="":
print("Currently playing " + artist + " - " + track + "--TRACK ID--")
if __name__ == '__main__':
main()
这工作没有麻烦,但我想要的轨道ID,这样我就可以使另一个函数来分析音频,但当我添加这个
def get_current_song():
# Get track information
track = spotify.current_user_playing_track()
print(json.dumps(track, sort_keys=True, indent=4))
artist = json.dumps(track['item']['album']['artists'][0]['name'])
track = json.dumps(track['item']['name'])
track_id = json.dumps(track['item']['id'])
return artist, track
我得到这个错误
Traceback (most recent call last):
File "C:\Users\Me\AppData\Local\Programs\Python\Python311\Projects\spotipy\kasify.py", line 32, in <module>
main()
File "C:\Users\Me\AppData\Local\Programs\Python\Python311\Projects\spotipy\kasify.py", line 27, in main
artist, track = get_current_song()
^^^^^^^^^^^^^^^^^^
File "C:\Users\Me\AppData\Local\Programs\Python\Python311\Projects\spotipy\kasify.py", line 23, in get_current_song
track_id = json.dumps(track['item']['id'])
~~~~~^^^^^^^^
TypeError: string indices must be integers, not 'str'
1条答案
按热度按时间5gfr0r5j1#
我修改了你的代码删除了不必要的东西。