python - 如何继续查看 Spotify 上当前正在播放的歌曲以查看它是否发生了变化
问题描述
我只是想弄清楚如何继续检查是否有使用 Spotipy 在 Spotify 上播放的歌曲然后打印它,当歌曲改变时它应该也打印出它改变的内容。这应该一直持续到程序关闭。这就是我到目前为止所拥有的。
import spotipy
import spotipy.util as util
class SongAsAppName:
def __init__(self):
self.new_song = ''
scope = 'user-read-currently-playing'
self.token = util.prompt_for_user_token(username, scope,
CLIENT_ID, CLIENT_SECRET,
redirect_uri='http://localhost:8888/callback')
self.spotify = spotipy.Spotify(auth=self.token)
self.current_track = self.spotify.current_user_playing_track()
def set_song_info(self, new_song):
if self.song != new_song:
self.get_song_info()
self.current_track = new_song
self.print_song_info()
def get_song_info(self):
song_title = self.current_track['item']['name']
artist_name = self.current_track['item']['artists']
for entry in artist_name:
artist_name = entry['name']
full_title = (song_title + ' - ' + artist_name)
return full_title
def print_song_info(self):
self.song = self.get_song_info()
while True:
if self.new_song != self.song:
print('Playing song: ' + self.song)
self.new_song = self.song
test = SongAsAppName()
test.print_song_info()
我认为这与用歌曲覆盖 new_song 并卡在那里有关。例如它打印出:
Playing song: Natural Disasters - Enon
并且当下一首歌曲播放时,不会打印出该歌曲的名称。大脑在工作了一整天并做这个 lil 副项目后只是油炸了,所以任何帮助都非常感谢!
--------------------------------------------------------------------------
在这里尝试不同的东西,但总体思路相同。下一首歌曲播放时,歌曲似乎没有更新。
import spotipy
import spotipy.util as util
import sched
import time
new_song = ''
s = sched.scheduler(time.time, time.sleep)
scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope,
CLIENT_ID, CLIENT_SECRET,
redirect_uri='http://localhost:8888/callback')
spotify = spotipy.Spotify(auth=token)
current_track = spotify.current_user_playing_track()
def get_song_info():
while True:
song_title = current_track['item']['name']
artist_name = current_track['item']['artists']
for entry in artist_name:
artist_name = entry['name']
full_title = (song_title + ' - ' + artist_name)
s.enter(10, 1, get_song_info)
return full_title
def print_song_info(new_song):
while True:
new_song = new_song
song = get_song_info()
if new_song != song:
print('Playing song: ' + song)
print(new_song)
new_song = song
print(new_song)
SongAsAppName.s.enter(10, 1, print_song_info, (new_song,))
s.enter(10, 1, print_song_info(new_song))
print_song_info()
解决方案
您发布的编辑代码有一些问题。我认为您正在尝试new_song
在全局范围内使用print_song_info
,而这些工作方式并非如此。您已重新定义print_song_info
以接受参数,因此您的最后一行无法运行。我不确定你为什么要while True
加入,get_song_info
因为它总是在第一次迭代后返回。print_song_info
永远不会到达最后一行。
sched
我以示例为例剥离了代码。这用于time()
生成一个每秒仅更改一次的字符串,但我认为这与您在不使用线程或异步的情况下的预期行为相似。
import sched
import time
s = sched.scheduler(time.time, time.sleep)
current_song = ''
def get_song_info():
global current_song
seconds = int(time.time())
current_song = "This string changes once per second " + str(seconds)
s.enter(0.5, 1, get_song_info)
def print_song_info(song):
if current_song != song:
print(current_song)
s.enter(0.1, 1, print_song_info, (current_song,))
get_song_info()
print_song_info('')
s.run()
推荐阅读
- c - 解析不同字符串的相等异或值以进行字谜检测
- python - django 和 asyncio 的事件循环错误
- c - 使用 c_api.h 执行使用 tensorflow.contrib.resampler 的冻结张量流图
- c# - 条件置换后如何将数组恢复为原始形式
- azure - 在 HDInsight 中添加 Azure Government 存储帐户
- python - 使用谷歌日历api删除与会者时如何触发UI刷新
- javascript - 使用外部链接将纹理应用于 WebGL 中的 3D 模型
- wordpress - 使用 trello 和 wordpress 时自动发送电子邮件
- python - 如何在tensorflow中保存文本分类模型?
- templates - 如何访问 Microweber 上页面的 html?