Jessica Martini
Jessica Martini

Reputation: 263

How to retrieve a listening history object by spotipy?

I'm working at a recommendation system for Spotify and I'm using spotipy on Python. I can't use the function current_user_recently_played, because Python says that the attribute current_user_recently_played isn't valid.

I don't know how to solve this problem, I absolutely need of this information to continue with my work.

This is my code:

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="[email protected]",
        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)

and the response is:

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

Upvotes: 2

Views: 2023

Answers (1)

Rach Sharp
Rach Sharp

Reputation: 2444

The Spotify API Endpoints current_user_recently_added exists in the source code on Github, but I don't seem to have it in my local installation. I think the version on the Python package index is out of date, last change to the source code was 8 months ago and last change to the PyPI version was over a year ago.

I've gotten the code example to work by patching the Spotify client object to add the method myself, but this way of doing it is not the best way generally as it adds custom behaviour to a particular instance rather than the general 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))

Other ways of getting it to work in a more correct way are:

  • installing it from the source on Github, instead of through Pip
  • poking Plamere to request he update the version on PyPI
  • subclass the Spotify client class and add the missing methods to the subclass (probably the quickest and simplest)

Here's a partial snippet of the way I've subclassed it in my own project:

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>

Upvotes: 2

Related Questions