Skip to main content
 首页 » 编程设计

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

2024年12月31日10me-sa

我在 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' 

请您参考如下方法:

Spotify API Endpoints 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>