2

我正在为 Spotify 推荐系统工作,我在 Python 上使用了 spotipy。我不能使用 function current_user_recently_played,因为 Python 说该属性current_user_recently_played无效。

我不知道如何解决这个问题,我绝对需要这些信息来继续我的工作。

这是我的代码:

import spotipy
import spotipy.util as util
import json


def current_user_recently_played(self, limit=50):
    return self._get('me/player/recently-played', limit=limit)


token = util.prompt_for_user_token(
        username="212887@studenti.unimore.it",
        scope="user-read-recently-played user-read-private user-top-read user-read-currently-playing",
        client_id="xxxxxxxxxxxxxxxxxxxxxx",
        client_secret="xxxxxxxxxxxxxxxxxxxxxx",
        redirect_uri="https://www.google.it/")

spotify = spotipy.Spotify(auth=token)



canzonirecenti= spotify.current_user_recently_played(limit=50) 
out_file = open("canzonirecenti.json","w") 
out_file.write(json.dumps(canzonirecenti, sort_keys=True, indent=2)) 
out_file.close()

print json.dumps(canzonirecenti, sort_keys=True, indent=2)

回应是:

AttributeError: 'Spotify' object has no attribute 'current_user_recently_played'
4

1 回答 1

2

Spotify API Endpointscurrent_user_recently_added存在于 Github 的源代码中,但我的本地安装中似乎没有。我认为 Python 包索引上的版本已经过时,最后一次更改源代码是 8 个月前,最后一次更改 PyPI 版本是一年多前。

我已经通过修补 Spotify 客户端对象来自己添加方法来使代码示例工作,但是这种方法通常不是最好的方法,因为它向特定实例而不是一般类添加了自定义行为。

import spotipy
import spotipy.util as util
import json

import types


def current_user_recently_played(self, limit=50):
    return self._get('me/player/recently-played', limit=limit)


token = util.prompt_for_user_token(
        username="xxxxxxxxxxxxxx",
        scope="user-read-recently-played user-read-private user-top-read user-read-currently-playing",
        client_id="xxxxxxxxxxxxxxxxxxxxxx",
        client_secret="xxxxxxxxxxxxxxxxxxxxxxxx",
        redirect_uri="https://www.google.it/")

spotify = spotipy.Spotify(auth=token)
spotify.current_user_recently_played = types.MethodType(current_user_recently_played, spotify)

canzonirecenti = spotify.current_user_recently_played(limit=50)
out_file = open("canzonirecenti.json","w")
out_file.write(json.dumps(canzonirecenti, sort_keys=True, indent=2))
out_file.close()

print(json.dumps(canzonirecenti, sort_keys=True, indent=2))

使其以更正确的方式工作的其他方法是:

  • 从 Github 上的源代码安装它,而不是通过 Pip
  • 戳 Plamere 请求他更新 PyPI 上的版本
  • 子类化 Spotify 客户端类并将缺少的方法添加到子类(可能是最快和最简单的)

这是我在自己的项目中对其进行子类化的方式的部分片段:

class SpotifyConnection(spotipy.Spotify):
    """Modified version of the spotify.Spotipy class

  Main changes are:
    -implementing additional API endpoints (currently_playing, recently_played)
    -updating the main internal call method to update the session and retry once on error,
     due to an issue experienced when performing actions which require an extended time
     connected.
  """

    def __init__(self, client_credentials_manager, auth=None, requests_session=True, proxies=None,
                 requests_timeout=None):
        super().__init__(auth, requests_session, client_credentials_manager, proxies, requests_timeout)

    def currently_playing(self):
        """Gets whatever the authenticated user is currently listening to"""
        return self._get("me/player/currently-playing")

    def recently_played(self, limit=50):
        """Gets the last 50 songs the user has played

    This doesn't include whatever the user is currently listening to, and no more than the
    last 50 songs are available.
    """
        return self._get("me/player/recently-played", limit=limit)

        <...more stuff>
于 2018-05-14T12:09:59.637 回答