读取成功 spotify 的响应 api post(创建播放列表)
read response from successful spotify api post (create playlist)
抱歉,我认为这是非常基本的,但我花了一段时间试图找到答案,但我无法解决。
我正在使用 spotify api / spotipy 来制作播放列表,它可以工作并且播放列表已创建,但我想检索 playlist_id 这样我就可以向其中添加曲目,但我不知道如何从 api.
获取任何响应信息
def make_playlist(name='python_play'):
user = sp.me()['id']
sp.user_playlist_create(user=user,name=name)
response = make_playlist()
pprint (response)
returns "None"
我以为我会调用 response.text 或 repsonse.contents 或类似的东西,但响应是 none 类型,我不能从中调用任何东西?
我一直很高兴地从 api 中检索信息,然后使用该信息通过 sqlalchemy 填充数据库,但我不明白在创建播放列表时如何实际获得响应。
api 成功调用 info/db 人口的示例:
def get_artists(num=1):
'''takes num, retrieves num artists, returns list of dicts'''
if num <=50:
limit = num
else:
limit = 50
artist_list = []
after = 0
for offset in range(0, num, 50):
response = sp.current_user_followed_artists(limit=limit, after=after)
for artist in response['artists']['items']:
artist_name = artist['name']
artist_id = artist['id']
artist_dict={"artist name": artist_name,
"artist id": artist_id}
artist_list.append(artist_dict)
after = artist_id
return artist_list
def new_artist(artist_list):
'''takes list of artists, writes to db'''
session = Session()
artist_list = artist_list
for artist in artist_list:
new_artist = Artist(artist_name=artist['artist name'], artist_id=artist['artist id'])
session.merge(new_artist)
session.commit()
session.close()
def populate_artists(num=1000):
'''takes num_artists, calls get_artists, calls new_artist'''
artist_list = get_artists(num)
new_artist(artist_list)
return artist_list
artist_list = populate_artists(10)
你实际上 return
根本不是你的 make_plalist()
函数中的任何东西。
我找到了一种通过播放列表名称搜索播放列表 ID 的解决方法,但我确定必须有一种方法可以在我创建播放列表时简单地对 ID 进行 Spotify return?
def GetPlaylistID(playlist_name):
playlist_id = ''
playlists = sp.current_user_playlists()
for playlist in playlists['items']: # iterate through playlists I follow
if playlist['name'] == playlist_name: # filter for newly created playlist
playlist_id = playlist['id']
return playlist_id
def make_playlist(playlist_name='python_play'):
user = sp.me()['id']
sp.user_playlist_create(user=user,name=playlist_name)
playlist_id = GetPlaylistID(playlist_name)
return playlist_id
@StayPerfect 的意思是,您忘记了 return 第 3 行中 sp.user_playlist_create() 的响应:
def make_playlist(name='python_play'):
user = sp.me()['id']
return sp.user_playlist_create(user=user,name=name)
response = make_playlist()
pprint (response)
抱歉,我认为这是非常基本的,但我花了一段时间试图找到答案,但我无法解决。
我正在使用 spotify api / spotipy 来制作播放列表,它可以工作并且播放列表已创建,但我想检索 playlist_id 这样我就可以向其中添加曲目,但我不知道如何从 api.
获取任何响应信息def make_playlist(name='python_play'):
user = sp.me()['id']
sp.user_playlist_create(user=user,name=name)
response = make_playlist()
pprint (response)
returns "None"
我以为我会调用 response.text 或 repsonse.contents 或类似的东西,但响应是 none 类型,我不能从中调用任何东西?
我一直很高兴地从 api 中检索信息,然后使用该信息通过 sqlalchemy 填充数据库,但我不明白在创建播放列表时如何实际获得响应。
api 成功调用 info/db 人口的示例:
def get_artists(num=1):
'''takes num, retrieves num artists, returns list of dicts'''
if num <=50:
limit = num
else:
limit = 50
artist_list = []
after = 0
for offset in range(0, num, 50):
response = sp.current_user_followed_artists(limit=limit, after=after)
for artist in response['artists']['items']:
artist_name = artist['name']
artist_id = artist['id']
artist_dict={"artist name": artist_name,
"artist id": artist_id}
artist_list.append(artist_dict)
after = artist_id
return artist_list
def new_artist(artist_list):
'''takes list of artists, writes to db'''
session = Session()
artist_list = artist_list
for artist in artist_list:
new_artist = Artist(artist_name=artist['artist name'], artist_id=artist['artist id'])
session.merge(new_artist)
session.commit()
session.close()
def populate_artists(num=1000):
'''takes num_artists, calls get_artists, calls new_artist'''
artist_list = get_artists(num)
new_artist(artist_list)
return artist_list
artist_list = populate_artists(10)
你实际上 return
根本不是你的 make_plalist()
函数中的任何东西。
我找到了一种通过播放列表名称搜索播放列表 ID 的解决方法,但我确定必须有一种方法可以在我创建播放列表时简单地对 ID 进行 Spotify return?
def GetPlaylistID(playlist_name):
playlist_id = ''
playlists = sp.current_user_playlists()
for playlist in playlists['items']: # iterate through playlists I follow
if playlist['name'] == playlist_name: # filter for newly created playlist
playlist_id = playlist['id']
return playlist_id
def make_playlist(playlist_name='python_play'):
user = sp.me()['id']
sp.user_playlist_create(user=user,name=playlist_name)
playlist_id = GetPlaylistID(playlist_name)
return playlist_id
@StayPerfect 的意思是,您忘记了 return 第 3 行中 sp.user_playlist_create() 的响应:
def make_playlist(name='python_play'):
user = sp.me()['id']
return sp.user_playlist_create(user=user,name=name)
response = make_playlist()
pprint (response)