我尝试在Python中使用此功能,以便从spotify获取链接:
import os
import urllib.parse
import urllib.request
import json
from tkinter import *
from tkinter import filedialog
def makeRequestToSpotify(songname):
encodedname = urllib.parse.quote_plus(songname)
url = "https://api.spotify.com/v1/search?query=" + encodedname+"&type=track" "
content = urllib.request.urlopen(url)
data = json.loads(content.readall().decode('utf-8'))
songLink = data["tracks"]["items"][0]["externalurls"]["spotify"]
print(songLink)
def getMusicFilesFromFolder(afolder):
extensions = [".mp3",".wma",".aac",".m4a"]
filenames = []
for x in os.walk(afolder):
for file in x[2]:
file_name, filext = os.path.splitext(file)
for ext in extensions:
if filext == ext:
filenames.append(file_name)
break
for file in filenames:
makeRequestToSpotify(file)
print(filenames)
def buttonClick():
file_path = filedialog.askdirectory()
getMusicFilesFromFolder(file_path)
#User Interface
root = Tk()
root.title("Music Repository with Spotify")
root.geometry("600x600")
app = Frame(root)
app.grid()
chooseFileButton = Button(app, command=buttonClick, text = "Choose a Folder")
chooseFileButton.grid()
root.mainloop()
这给了我一个index out of range
- 错误。
我希望从此程序中获取该文件夹中每首歌曲的链接