在尝试从Spotipy搜索结果中搜索URI时列出索引范围之外的范围
我使用Spotipy制作了该程序将Google表转换为Python的Spotify播放列表。
但是我在搜索结果中搜索URI时有一个错误(第55行),当这种情况发生是随机的时机时,有时会在歌曲25上发生在歌曲400上
。我尝试添加延迟等,但没有任何帮助。
错误:
Traceback (most recent call last):
File "C:\Users\###\OneDrive\Documenten\spotify playlist maker\maker\maker_v4.py", line 58, in <module>
uri_results.append(search_results[i]['tracks']['items'][0]['uri'])
IndexError: list index out of range
代码:
from re import S
import spotipy
from spotipy import SpotifyOAuth
import json
import gspread
import time
scope = 'playlist-modify-public'
username = '###'
#open google sheets credentials
sa = gspread.service_account()
#open google sheets client
sh = sa.open(input("Enter the name of the google sheet: "))
print("opened sheet:" + sh.title)
#open google sheets worksheet
wks = sh.worksheet(input("Enter the name of the worksheet: "))
print("opened worksheet:" + wks.title)
#open spotify credentials
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="###", client_secret="###", redirect_uri="http://127.0.0.1:8080/", scope=scope, username=username))
#create playlist
playlist_name = input("Enter playlist name: ")
playlist_description = input("Enter playlist description: ")
#fine playlist id
playlist = sp.user_playlist_create(user=username, name=playlist_name, public=True, description=playlist_description).get('id')
#get sheet data
sheet_songs = wks.get_all_values()
print(len(sheet_songs))
print("Playlist created")
print("Playlist name: " + playlist_name)
print("Playlist description: " + playlist_description)
print("Playlist ID: " + playlist)
time.sleep(2.5)
sheet_songs_uri = []
uri_results = []
search_results = []
for i in range(len(sheet_songs)):
sheet_song = sheet_songs[i-1]
print(i)
search_results.append(sp.search(q=sheet_song, type='track'))
print(len(search_results))
time.sleep(5)
for i in range(len(search_results)):
print(i)
#print(search_results[i])
uri_results.append(search_results[i]['tracks']['items'][0]['uri'])
# sheet_songs_uri.append(uri_results)
time.sleep(1)
sp.user_playlist_add_tracks(user=username, playlist_id=playlist, tracks=uri_results)
#print playlist info
print("Playlist created")
print("Playlist name: " + playlist_name)
print("Playlist description: " + playlist_description)
print("Playlist ID: " + playlist)
I made this program to convert a google sheets to a spotify playlist in python using spotipy.
But i have this error when trying to search for the uri in the search results (line 55), when this happens is random somtimes it happens at song 25 sometimes at song 400.
I have tried adding delays etc but nothing has helped.
error:
Traceback (most recent call last):
File "C:\Users\###\OneDrive\Documenten\spotify playlist maker\maker\maker_v4.py", line 58, in <module>
uri_results.append(search_results[i]['tracks']['items'][0]['uri'])
IndexError: list index out of range
code:
from re import S
import spotipy
from spotipy import SpotifyOAuth
import json
import gspread
import time
scope = 'playlist-modify-public'
username = '###'
#open google sheets credentials
sa = gspread.service_account()
#open google sheets client
sh = sa.open(input("Enter the name of the google sheet: "))
print("opened sheet:" + sh.title)
#open google sheets worksheet
wks = sh.worksheet(input("Enter the name of the worksheet: "))
print("opened worksheet:" + wks.title)
#open spotify credentials
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="###", client_secret="###", redirect_uri="http://127.0.0.1:8080/", scope=scope, username=username))
#create playlist
playlist_name = input("Enter playlist name: ")
playlist_description = input("Enter playlist description: ")
#fine playlist id
playlist = sp.user_playlist_create(user=username, name=playlist_name, public=True, description=playlist_description).get('id')
#get sheet data
sheet_songs = wks.get_all_values()
print(len(sheet_songs))
print("Playlist created")
print("Playlist name: " + playlist_name)
print("Playlist description: " + playlist_description)
print("Playlist ID: " + playlist)
time.sleep(2.5)
sheet_songs_uri = []
uri_results = []
search_results = []
for i in range(len(sheet_songs)):
sheet_song = sheet_songs[i-1]
print(i)
search_results.append(sp.search(q=sheet_song, type='track'))
print(len(search_results))
time.sleep(5)
for i in range(len(search_results)):
print(i)
#print(search_results[i])
uri_results.append(search_results[i]['tracks']['items'][0]['uri'])
# sheet_songs_uri.append(uri_results)
time.sleep(1)
sp.user_playlist_add_tracks(user=username, playlist_id=playlist, tracks=uri_results)
#print playlist info
print("Playlist created")
print("Playlist name: " + playlist_name)
print("Playlist description: " + playlist_description)
print("Playlist ID: " + playlist)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
错误是告诉您确切的问题是什么。
表示您在该索引上引用的价值不在那里...
我将所有这些都移至一个循环中。
The error is telling you exactly what the issue is.
Means the value you reference at that index isnt there...
I'd move this all into one loop.