【发布时间】:2021-10-16 09:23:28
【问题描述】:
运行时会吐出错误,无法将令牌写入缓存位置:.cache,但它仍然设法抓取正在播放的任何内容。每次它发出请求时,它都会重新打开一个身份验证页面。它也不会保存到任何文件中。使用主 python 环境在 pycharm 中一切运行良好。
from spotipy.oauth2 import SpotifyOAuth
import requests
import time
#file locations
file_time_playing = "time_playing.txt"
file_time_playing_ms = "time_playing_ms.txt"
file_time_total = "time_total.txt"
file_time_total_ms = "time_total_ms.txt"
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id = ".......",
client_secret = ".......",
redirect_uri = "http://localhost:8080",
scope = "streaming"))
song = ""
def track():
print("======================")
global song
global currently_playing
# print(sp.currently_playing())
try:
currently_playing = sp.currently_playing()
#print(currently_playing)
if song == str(currently_playing["item"]["name"]):
print(song)
play_time()
return
else:
song = str(currently_playing["item"]["name"])
print(song)
with open("song.txt", "w", encoding="utf-8") as f:
f.write(song)
except:
pass
starttime = time.time()
while True:
track()
time.sleep(0.5 - ((time.time() - starttime) % 0.5))```
【问题讨论】: