与for loops
结果一起嵌套JSON
可能会让人头疼。
根据以下代码,我想提取属于特定track names
的{{1}}。
我创建了一系列空列表:
playlist
然后#for 'search' endpoint results
owner_ids = []
playlist_query_names = []
playlist_query_ids = []
#for 'playlist' endpoint results
playlist_names = []
track_names = []
track_ids = []
执行function
:
query
但是,必须访问另一个def query_playlists(query):
#query 'search' endpoint(desired playlists),
#with no track_ids to be found
query_results = sp.search(q=query, type='playlist')
query_items = query_results['playlists']['items']
for item in query_items:
playlist_query_names.append(item['name'])
playlist_query_ids.append(item['id'])
owner = item['owner']
owner_ids.append(owner['id'])
才能检索endpoint
:
track names
这是其他 #lookup 'user_playlists' endpoint (all playlists),
#where track ids are to be found
playlist_results = [sp.user_playlists(owner) for owner in owner_ids]
playlist_items = playlist_results[0]['items']
for item in playlist_items:
playlist_name = item['name']
playlist_names.append(playlist_name)
username = item['owner']['id']
endpoint
某种程度上,我无法将 results = sp.user_playlist(username, item['id'], fields="tracks,next")
#intersect both endpoints
if playlist_name in playlist_query_names:
tracks = results['tracks']
#find desired tracks
for item in (tracks['items']):
track = item['track']
track_id = item['track']['uri']
#add tracks
track_names.append(track['name'])
track_ids.append(track_id)
return track_ids, track_names
track names
缩小到通过results
playlists
search
可以请一些严谨的想法指出我的错误在哪里?
修改
来自endpoint
JSON
的 playlists
结果:
endpoint