在尝试从Spotipy搜索结果中搜索URI时列出索引范围之外的范围

发布于 2025-01-27 13:44:25 字数 2377 浏览 4 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2025-02-03 13:44:25

错误是告诉您确切的问题是什么。

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'])
                                      ^         OR          ^                   

表示您在该索引上引用的价值不在那里...

我将所有这些都移至一个循环中。

sheet_songs_uri = []
uri_results = []
search_results = []

songs = len(sheet_songs)
for i in songs:
    sheet_song = sheet_songs[i-1]
    print(i)
    result = sp.search(q=sheet_song, type='track')
    # Logic here to check if result actually has a value.
    if result:
        uri_results.append(results['tracks']['items'][0]['uri'])
    else:
        print('Error getting result')

The error is telling you exactly what the issue is.

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'])
                                      ^         OR          ^                   

Means the value you reference at that index isnt there...

I'd move this all into one loop.

sheet_songs_uri = []
uri_results = []
search_results = []

songs = len(sheet_songs)
for i in songs:
    sheet_song = sheet_songs[i-1]
    print(i)
    result = sp.search(q=sheet_song, type='track')
    # Logic here to check if result actually has a value.
    if result:
        uri_results.append(results['tracks']['items'][0]['uri'])
    else:
        print('Error getting result')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文