如何让Spotify当前播放的歌曲在Python(Windows)?

eimct9ow  于 2022-12-30  发布在  Python
关注(0)|答案(4)|浏览(147)

我想在16 X2 LCD上显示Spotify中当前播放的歌曲,我想把LCD和我的Arduino连接起来,然后做一个Python脚本,把Spotify当前播放的歌曲发送到Arduino上。
为了达到这个目的,我正在寻找一种方法来获得Spotify当前在Python中播放的歌曲。(我使用的是Windows 8。)我找到了一些类似dbus的方法,但它们要么是用于Linux,要么是用于Mac。
提前谢谢你!(抱歉我的英语语法不好。)

34gzjxbg

34gzjxbg1#

我遇到了同样的问题,所以我写了一个库来解决这个问题,这个库可以在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

artist()只返回第一个艺术家。2我开始使用另一个库 spotimeta.py 来解决这个问题。3然而,这个库还不能100%地工作。

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

如果出现错误,spotimeta. artist() 将只返回第一个艺术家(在 spotilib.artist() 中找到)
希望这对你有帮助(如果还需要的话)!

jum4pzuy

jum4pzuy2#

最简单的方法可能是将当前播放的曲目从Spotify客户端scrobble到last.fm帐户,然后使用python从那里获取。
Last.fm 允许你通过user.getRecentTracks的API获取scrobbed音轨,user.getRecentTracks提供nowplaying="true"属性,如果一首歌正在播放,它也提供一些其他有用的东西,你可能需要一个外部显示,如专辑封面的链接和last.fm歌曲的www.example.com页面。
下面是一个简单的例子,它将用户名和API键作为cmd行参数,并使用requests库获取当前正在为该用户播放的内容。

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客户端后曲目不再播放。

7rfyedvj

7rfyedvj3#

有不止一个pytify吗?这对我来说一直有效,直到今天:https://code.google.com/p/pytify/
Spotify已经更新,现在歌曲不再显示在windows标题中。
他们将把“特色”带回来:https://community.spotify.com/t5/ideas/v2/ideapage/blog-id/ideaexchange/article-id/73238/page/2#comments

hmae6n7t

hmae6n7t4#

当为True时:print(“欢迎加入项目,“+用户名['显示名'])

print("0 - Exit the console")

print("1 - Search for a Song")

user_input = int(input("Enter Your Choice: "))

if user_input == 1:
    search_song = input("Enter the song name: ")

    results = spotifyObject.search(search_song, 1, 0, "track")
    songs_dict = results['tracks']
    song_items = songs_dict['items']
    song = song_items[0]['external_urls']['spotify']
    webbrowser.open(song)
    print('Song has opened in your browser.')
elif user_input == 0:

    print("Good Bye, Have a great day!")
    break
else:
    print("Please enter valid user-input.")

相关问题