创建一个新的spotify播放列表-脚本不起作用



我是python的新手,我想通过插入Youtube音乐视频URL并在Spotify中创建一个播放列表来完成我的第一个项目,其中包含URL中的歌曲。

由于某种原因,我的脚本不起作用我想知道为什么。一旦我运行了脚本,我就得到了这个:

Initialising Spotify Client....
User authentication requires interaction with your
web browser. Once you enter your credentials and
give authorization, you will be redirected to
a url.  Paste that url you were directed to to
complete the authorization.

然后我将url复制到控制台(页面不工作,因为它是"http://localhost:8888/callback/'(,就这样;我的脚本不再运行了。

这是我的脚本-->

import json
import requests
from youtube_dl import YoutubeDL
import spotipy.util as util
import spotipy
username = '***' #personal
client_id = '***'  #personal
client_secret = '***'  #personal
redirect_uri = 'http://localhost:8888/callback/'
scope = 'playlist-modify-private'

def init_spotify_client():
print('Initialising Spotify Client....')
token = util.prompt_for_user_token(username, scope,
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri)
client_token = spotipy.Spotify(auth=token)
print('nClient initialised!n')
return client_token
class CreatePlaylist:
def __init__(self,youtube_url):
self.youtube_url = youtube_url
def info(self,youtube_url):
# use youtube_dl to collect the song name & artist name
video = YoutubeDL().extract_info(youtube_url, download=False)
song_name = video["track"]
artist = video["artist"]
def create_playlist(self):
"""Create A New Playlist"""
request_body = json.dumps({
"name": "Youtube Liked Vids",
"description": "All Liked Youtube Videos",
"public": False
})
query = "https://api.spotify.com/v1/users/{}/playlists".format(username)
response = requests.post(
query,
data=request_body,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(init_spotify_client())
}
)
response_json = response.json()
# playlist id
return response_json["id"]
def add_song_to_playlist(self,song_name,artist):
query = "https://api.spotify.com/v1/search?query=track%3A{}+artist%3A{}&type=track&offset=0&limit=20".format(
song_name,
artist
)
response = requests.get(
query,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(init_spotify_client())
}
)
response_json = response.json()
songs = response_json["tracks"]["items"]
# only use the first song
uri = songs[0]["uri"]
# create a new playlist
playlist_id = self.create_playlist()
# add all songs into new playlist
request_data = json.dumps(uri)
query = "https://api.spotify.com/v1/playlists/{}/tracks".format(playlist_id)
response = requests.post(
query,
data=request_data,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer {}".format(init_spotify_client())
}
)
response_json = response.json()
return response_json
def main():
init_spotify_client()
url = "https://www.youtube.com/watch?v=Bx5QlA8xp28"
cp = CreatePlaylist(url)
main()

请告诉我怎么了:/

您还没有调用add_song_to_playlist方法。根据我的理解,您将从info方法传递song_nameartist变量来调用add_song_to_playlist。如果是这种情况,您需要向info方法添加一个return语句,如下所示:

def info(self,youtube_url):
# use youtube_dl to collect the song name & artist name
video = YoutubeDL().extract_info(youtube_url, download=False)
song_name = video["track"]
artist = video["artist"]
return song_name, artist

main函数中,您需要调用info方法,并使用其结果来调用add_song_to_playlist:

def main():
init_spotify_client()
url = "https://www.youtube.com/watch?v=Bx5QlA8xp28"
cp = CreatePlaylist(url)
song_name, artist = cp.info()
res = cp.add_song_to_playlist(song_name, artist)

最新更新