如何在 Python (Windows) 中获取 Spotify 当前正在播放的歌曲?

How to get Spotify current playing song in Python (Windows)?

我想在 16x2 LCD 上显示 Spotify 中当前正在播放的歌曲。 我正在考虑将 LCD 与我的 Arduino 连接,然后制作一个 Python 脚本,将 Spotify 的当前播放歌曲发送到 Arduino。

进入正题,我正在寻找一种在 Python 中获取 Spotify 当前播放歌曲的方法。 (我正在使用 Windows 8。)我找到了一些类似 dbus 的方法,但它们要么用于 Linux 要么用于 Mac.

提前致谢! (抱歉英语语法不好。)

pytify不止一个吗? 直到今天这对我有用: https://code.google.com/p/pytify/

Spotify 已更新,现在 windows 标题中不再显示歌曲。

他们会把 "feature" 带回来: https://community.spotify.com/t5/ideas/v2/ideapage/blog-id/ideaexchange/article-id/73238/page/2#comments

最简单的方法可能是将当前正在播放的曲目从 Spotify 客户端复制到 last.fm 帐户,然后使用 python 从那里获取它。

Last.fm 允许您通过 api 和 user.getRecentTracks 获得涂鸦曲目,如果正在播放歌曲,它会提供 nowplaying="true" 属性。它还提供了其他一些您可能需要的外部显示有用的东西,例如专辑封面的 link 和歌曲的 last.fm 页面。

这是一个简单的示例,它使用用户名和 api 键作为 cmd 行参数,并使用请求库获取该用户当前正在播放的内容。

from time import sleep
import requests
import json
from pprint import pprint
import sys    

def get_playing():
    base_url = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='
    user = sys.argv[1]
    key = sys.argv[2]
    r = requests.get(base_url+user+'&api_key='+key+'&format=json')
    data = json.loads(r.text)
    latest_track = data['recenttracks']['track'][0]
    try:
        if latest_track['@attr']['nowplaying'] == 'true':
            artist = latest_track['artist']['#text']
            song = latest_track['name']
            album = latest_track['album']['#text']
            print "\nNow Playing: {0} - {1}, from the album {2}.\n".format(artist, song, album)
    except KeyError:
        print '\nNothing playing...\n'    

def main():
    if len(sys.argv)<3:
        print "\nError: Please provide a username and api key in the format of: 'scrobble.py username api_key\n"
    else:
        get_playing()    

if __name__ == '__main__':
    main()

在快速测试中,似乎确实需要一分钟左右的时间才能意识到在暂停或退出 Spotify 客户端后曲目不再播放。

我遇到了同样的问题,所以我写了一个库来解决这个问题。该库位于 github: https://github.com/XanderMJ/spotilib。请记住,这仍在进行中。

只需复制文件并将其放在您的 Python/Lib 目录中。

import spotilib
spotilib.artist() #returns the artist of the current playing song
spotilib.song() #returns the song title of the current playing song

spotilib.artist() return只是第一位艺术家。我开始研究另一个库 spotimeta.py 来解决这个问题。但是,这还没有达到 100%。

import spotimeta
spotimeta.artists() #returns a list of all the collaborating artists of the track

如果发生错误,spotimeta.artists() 将 return 仅第一个艺术家(通过 spotilib.artist 找到())

希望这对您有所帮助(如果仍然需要)!