如何通过 spotipy 检索收听历史对象?

How to retrieve a listening history object by spotipy?

我在 Spotify 的推荐系统工作,我在 Python 上使用 spotipy。我无法使用函数 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'

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

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

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 上的版本
  • subclass Spotify 客户端 class 并将缺少的方法添加到 subclass(可能是最快和最简单的)

这是我在自己的项目中子class编辑它的方式的部分片段:

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>