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

时间:2018-05-10 15:53:30

标签: python spotify spotipy

我正在使用Spotify的推荐系统,我在Python上使用spotipy。我不能使用函数ScalarFunction,因为Python说属性current_user_recently_played无效。

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

这是我的代码:

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)

1 个答案:

答案 0 :(得分:1)

Spotify API端点current_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>