使用Spotipy检测歌曲的结尾

时间:2020-11-03 21:35:51

标签: python spotify spotipy

我正在使用SpotipyLyricsGenius在终端上通过网络浏览器打开歌词。

我可以打开一首歌的URL,但是每次都必须运行脚本才能连续运行。使用Spotipy检测歌曲结束的方法有哪些?

import spotipy
import webbrowser
import lyricsgenius as lg

...

# Create our spotifyObject
spotifyObject = spotipy.Spotify(auth=token)

# Create out geniusObject
geniusObject = lg.Genius(access_token)

...

while True:

    currently_playing = spotifyObject.currently_playing()
    artist = currently_playing['item']['artists'][0]['name']
    title = currently_playing['item']['name']
    search_query = artist + " " + title

    # if (currently_playing has changed):
        song = geniusObject.search_songs(search_query)
        song_url = song['hits'][0]['result']['url']
        webbrowser.open(song_url)

    webbrowser.open(song_url)

我正在阅读thisthis之类的相关线程,并通读了文档,但是如果Spotipy可以解决这个问题,我找不到答案。任何建议,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我使用time.sleep(length)并将参数“ length”表示当前曲目的剩余持续时间。