Ejemplo n.º 1
0
    def _compile_saved_albums(self):
        """Requests the Spotify user's saved albums and saves them.

        Requests the Spotify user's saved albums and stores it locally.
        """

        saved_albums = []

        limit = 50
        query_dict = {'limit': limit}
        url = '/v1/me/albums'

        # Get first page of items
        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)
        json = results.json()
        for i in json['items']:
            saved_albums.append(i['album'])

        # Get the rest of the pages
        while (json.get('next')):
            results = spotify.make_authorized_request(self.session,
                                                      json.get('next'),
                                                      full_url=True)
            json = results.json()
            for i in json['items']:
                saved_albums.append(i['album'])

        self._saved_albums = saved_albums
Ejemplo n.º 2
0
    def _compile_followed_artists(self):
        """Requests the Spotify user's followed artist data and saves it.

        Requests the Spotify user's followed artist data and stores it
        locally.
        """

        followed_artists = []

        limit = 50
        query_dict = {'limit': limit, 'type': 'artist'}
        url = '/v1/me/following'

        # Get the first page of items
        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)
        json = results.json()
        followed_artists.extend(json['artists']['items'])

        # Get the rest of the pages
        while (json.get('next')):
            results = spotify.make_authorized_request(self.session,
                                                      json.get('next'),
                                                      full_url=True)
            json = results.json()
            followed_artists.extend(json['items'])

        self._followed_artists = followed_artists
Ejemplo n.º 3
0
    def _compile_playlists(self):
        """Requests the Spotify user's simple playlist data and saves it.

        Requests the Spotify user's simple playlist data and stores it
        locally.
        """

        playlists = []

        limit = 50
        query_dict = {'limit': limit}
        url = '/v1/me/playlists'

        # Request first page of items
        page = spotify.make_authorized_request(self.session,
                                               url,
                                               query_dict=query_dict)
        json = page.json()
        playlists.extend(json['items'])

        # Keep requesting pages until they run out
        while (json.get('next')):
            page = spotify.make_authorized_request(self.session,
                                                   json.get('next'),
                                                   full_url=True)
            json = page.json()
            playlists.extend(json['items'])

        self._playlists = playlists
Ejemplo n.º 4
0
    def _compile_audio_features(self):
        """Requests the user's extended music taste data and saves it.

        Requests the Spotify user's extended music taste data and
        stores it locally.
        """

        music_taste = self.music_taste()

        ids = [t['id'] for t in music_taste]

        limit = 100

        # Split the song ids into sections (Spotify only lets you
        # request so many at a time)
        id_sections = split_into_subsections(ids, limit)
        for section in id_sections:
            url = '/v1/audio-features'
            query_dict = {'ids': spotify.create_id_querystr(section)}
            results = spotify.make_authorized_request(self.session,
                                                      url,
                                                      query_dict=query_dict)

            tracks = results.json()['audio_features']

            music_taste = combine_track_json(music_taste, tracks)

        self._music_taste = music_taste
Ejemplo n.º 5
0
    def test_make_authorized_request(self):
        """
        make_authorized_request() should successfully make the given
        request if a user is logged in.
        """
        url = '/v1/me'
        results = spotify.make_authorized_request(self.session, url)

        self.assertEqual(results.status_code, 200)
Ejemplo n.º 6
0
    def _compile_personal_data(self):
        """Requests the Spotify user's personal data and saves it.

        Requests the Spotify user's personal data and stores it
        locally.
        """

        url = '/v1/me'
        results = spotify.make_authorized_request(self.session, url)

        self._personal_data = results.json()
Ejemplo n.º 7
0
    def test_make_authorized_request_bad_url_no_raise(self):
        """
        Calling make_authorized_request() with raise_on_error=False and
        giving it a bad url should return the results of the bad
        request without raising an error.
        """
        query_dict = {'slksdf': 3451}
        url = '/v1/me/top/tracks'
        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict,
                                                  raise_on_error=False)

        self.assertNotEqual(results.status_code, 200)
Ejemplo n.º 8
0
    def test_make_authorized_request_no_auth_access_token(self):
        """
        make_authorized_request() should successfully make the given
        authorized request if a user is logged in, even if no
        auth_access_token is set.
        """
        # Remove the existing auth access token if there is one from the
        # other tests
        spotify._clear_auth_access_token(self.session)

        url = '/v1/me'
        results = spotify.make_authorized_request(self.session, url)

        self.assertEqual(results.status_code, 200)
Ejemplo n.º 9
0
    def _compile_recently_played(self):
        """Requests the user's recently played tracks and saves them.

        Requests the Spotify user's recently played tracks and stores
        them locally.
        """

        limit = 50
        query_dict = {'limit': limit}
        url = '/v1/me/player/recently-played'
        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)

        self._recently_played = [t['track'] for t in results.json()['items']]
Ejemplo n.º 10
0
    def test_make_authorized_request_with_full_url(self):
        """
        Calling make_authorized_request() with full_url=True should
        ignore the query string and just use whatever url was passed in
        through the url argument.
        """
        query_dict = {'slkww': 1430}
        url = 'https://api.spotify.com/v1/me'

        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict,
                                                  full_url=True)

        self.assertEqual(results.status_code, 200)
Ejemplo n.º 11
0
    def test_make_authorized_request_query_string(self):
        """
        make_authorized_request() should append the given dictionary
        onto the end of the request URL as a query string.
        """
        query_dict = {
            'limit': 10,
            'time_range': 'short_term',
        }
        url = '/v1/me/top/artists'

        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)

        self.assertEqual(results.status_code, 200)
        self.assertEqual(len(results.json().get('items')), 10)
Ejemplo n.º 12
0
    def _compile_playlist_details(self):
        """Requests the user's extended playlist data and saves it.

        Requests the Spotify user's extended playlist data and
        stores it locally.
        """

        playlists = self.playlists()

        for p in playlists:
            url = '/v1/playlists/' + p['id']
            query_dict = {'fields': 'followers'}
            results = spotify.make_authorized_request(self.session,
                                                      url,
                                                      query_dict=query_dict)
            p['followers'] = results.json()['followers']

        self._playlists = playlists
Ejemplo n.º 13
0
    def _compile_top_artists(self, time_range):
        """Requests the user's top artists over a period and saves them.

        Requests the Spotify user's top artists over a time range and
        stores them locally.

        Parameters
        ----------
        time_range : str
            The time range over which to collect the top data. Can be
            one of 'short_term', 'medium_term', or 'long_term'.
        """

        limit = 50
        query_dict = {'limit': limit, 'time_range': time_range}
        url = '/v1/me/top/artists'

        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)
        self._top_artists[time_range] = results.json()['items']
Ejemplo n.º 14
0
    def get_playlist_with_tracks(self, playlist_id):
        """Returns the complete playlist data for one playlist as JSON.
        
        Returns the complete data about one of the Spotify user's
        playlists, including the tracks in the playlist. If the data
        does not exist locally, requests it from the Spotify API. The
        playlist id is the Spotify ID (see the Spotify API
        documentation) of that playlist, which must identify a playlist
        that the user owns. If it does not, this function returns None.

        Parameters
        ----------
        playlist_id : str
            The Spotify ID of the playlist to return data about.

        Returns
        -------
        dict 
            The complete data about the playlist.
        """

        # First get basic data about all the user's playlists, so we
        # can ensure that the given playlist id is one of the user's
        # playlists and not another user's
        playlists = self.playlists()

        playlist = None
        index = None
        for i, p in enumerate(playlists):
            if p['id'] == playlist_id:
                playlist = p
                index = i
                break

        # If no playlist found, fail
        if not playlist:
            return None

        # If playlist isn't one of the user's, fail
        if playlist['tracks'].get('items') is not None:
            return playlist

        # Request the full playlist info
        url = '/v1/playlists/' + playlist_id
        query_dict = {'fields': 'tracks'}
        results = spotify.make_authorized_request(self.session,
                                                  url,
                                                  query_dict=query_dict)

        # The simple playlist data has a 'tracks' section that contains
        # the number of tracks and nothing more. Delete that and then
        # merge the dicts

        json = results.json()
        del playlist['tracks']
        json.update(playlist)
        playlists[
            index] = json  # playlist is just a reference, so edit the list

        self._playlists = playlists
        return playlists[index]