Ejemplo n.º 1
0
def get_multiple_albums(spotify_ids):
    """Get Spotify catalog information for multiple albums identified by their
    Spotify IDs.

    Returns:
        List of album objects from Spotify. More info about this type of objects
        is available at https://developer.spotify.com/web-api/object-model/#album-object.
    """
    namespace = "spotify_albums"
    albums = cache.get_many(spotify_ids, namespace)

    # Checking which albums weren't in cache
    for album_id, data in albums.items():
        if data is not None and album_id in spotify_ids:
            spotify_ids.remove(album_id)

    if len(spotify_ids) > 0:
        resp = requests.get("%s/albums?ids=%s" %
                            (BASE_URL, ','.join(spotify_ids))).json()['albums']

        received_albums = {}
        for album in resp:
            if album is not None:
                received_albums[album['id']] = album

        cache.set_many(received_albums,
                       namespace=namespace,
                       time=DEFAULT_CACHE_EXPIRATION)

        albums.update(received_albums)

    return albums
Ejemplo n.º 2
0
    def test_many(self):
        # With namespace
        mapping = {
            "test1": "Hello",
            "test2": "there",
        }
        self.assertTrue(cache.set_many(mapping, namespace="testing-1"))
        self.assertEqual(cache.get_many(list(mapping.keys()), namespace="testing-1"), mapping)

        # With another namespace
        test = cache.get_many(list(mapping.keys()), namespace="testing-2")
        for key, val in test.items():
            self.assertIn(key, mapping)
            self.assertIsNone(val)

        # Without a namespace
        mapping = {
            "test1": "What's",
            "test2": "good",
        }
        self.assertTrue(cache.set_many(mapping))
        self.assertEqual(cache.get_many(list(mapping.keys())), mapping)
Ejemplo n.º 3
0
    def test_many(self):
        # With namespace
        mapping = {
            "test1": "Hello",
            "test2": "there",
        }
        self.assertTrue(cache.set_many(mapping, namespace="testing-1"))
        self.assertEqual(cache.get_many(list(mapping.keys()), namespace="testing-1"), mapping)

        # With another namespace
        test = cache.get_many(list(mapping.keys()), namespace="testing-2")
        for key, val in test.items():
            self.assertIn(key, mapping)
            self.assertIsNone(val)

        # Without a namespace
        mapping = {
            "test1": "What's",
            "test2": "good",
        }
        self.assertTrue(cache.set_many(mapping))
        self.assertEqual(cache.get_many(list(mapping.keys())), mapping)
Ejemplo n.º 4
0
    def test_get_multiple_albums(self):
        spotify_ids = ['0Y7qkJVZ06tS2GUCDptzyW']
        if cache.get_many(spotify_ids, 'spotify_albums'):
            cache.delete_many(spotify_ids, 'spotify_albums')

        spotify._get = lambda query: FakeSpotifyResponse.fromSpotifyIds(
            spotify_ids)
        albums = spotify.get_multiple_albums(spotify_ids)
        self.assertDictEqual(albums[spotify_ids[0]], {
            'id': spotify_ids[0],
            'data': spotify._BASE_URL + spotify_ids[0],
        })

        albums = cache.get_many(spotify_ids, 'spotify_albums')
        self.assertListEqual(list(albums.keys()), spotify_ids)

        # test if cached result is returned properly
        albums = spotify.get_multiple_albums(spotify_ids)
        self.assertDictEqual(albums[spotify_ids[0]], {
            'id': spotify_ids[0],
            'data': spotify._BASE_URL + spotify_ids[0],
        })
Ejemplo n.º 5
0
def get_multiple_albums(spotify_ids: List[str]) -> List[dict]:
    """Get information about multiple albums.

    Args:
        spotify_ids: List of Spotify IDs of albums.

    Returns:
        List of album objects from Spotify. More info about this type of objects
        is available at https://developer.spotify.com/web-api/object-model/#album-object.
    """
    if not spotify_ids:
        return []
    spotify_ids = list(spotify_ids)
    cache_namespace = "spotify_albums"
    albums = cache.get_many(spotify_ids, cache_namespace)

    # Checking which albums weren't in cache
    for album_id, data in albums.items():
        if data is not None and album_id in spotify_ids:
            spotify_ids.remove(album_id)

    if spotify_ids:
        resp = _get(f"albums?ids={','.join(spotify_ids)}")
        if "albums" not in resp:
            logging.error("Album data is missing from a Spotify response",
                          extra={
                              "spotify_ids": spotify_ids,
                              "response": resp,
                          })
            raise SpotifyUnexpectedResponseException("Album data is missing")

        received_albums = {}
        for album in resp["albums"]:
            if album is not None:
                received_albums[album['id']] = album
        cache.set_many(received_albums,
                       namespace=cache_namespace,
                       time=_DEFAULT_CACHE_EXPIRATION)
        albums.update(received_albums)

    return albums