spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.05k stars 960 forks source link

How to get "category" tags for albums? #730

Closed josh-ashkinaze closed 3 years ago

josh-ashkinaze commented 3 years ago

Which object has "category" as an attribute? Here is a simple script to get all albums by the Talking Heads. Yet for each album object, I can't view the actual genre/categories of the album.

# Source
# https://github.com/plamere/spotipy/blob/master/examples/artist_albums.py

import spotipy
from spotipy.oauth2 import SpotifyOAuth
from spotipy.oauth2 import SpotifyClientCredentials
import logging

cid = "client_id"
cs = "client_secret"
uri = "http://localhost:8888/callback"

auth_manager = SpotifyClientCredentials(client_id=cid, client_secret=cs)
sp = spotipy.Spotify(auth_manager=auth_manager)

logger = logging.getLogger('artist_albums')
logging.basicConfig(level='INFO')

def get_artist(name):
    results = sp.search(q='artist:' + name, type='artist')
    items = results['artists']['items']
    if len(items) > 0:
        return items[0]
    else:
        return None

def show_artist_albums(artist):
    albums = []
    results = sp.artist_albums(artist['id'], album_type='album')
    albums.extend(results['items'])
    while results['next']:
        results = sp.next(results)
        albums.extend(results['items'])
    seen = set()  # to avoid dups
    albums.sort(key=lambda album: album['name'].lower())
    for album in albums:
        name = album['name']
        if name not in seen:
            logger.info('ALBUM: %s', name)
            seen.add(name)
    return albums

artist = get_artist("Talking Heads")
albums = show_artist_albums(artist)

# Get album object
sample_album = albums[0]
sample_album_dets = sp.albums([sample_album['id']])
stephanebruckert commented 3 years ago

Not all releases/artists have a genre unfortunately

Peter-Schorn commented 3 years ago

The album object does not have a genre field. The artist object does (but it may be empty). See the object index.

stephanebruckert commented 3 years ago

@Peter-Schorn albums also have a genres field, see here https://developer.spotify.com/console/get-album/?id=0sNOF9WDwhWunNAHPD3Baj (although it's empty here)

Peter-Schorn commented 3 years ago

I didn't realize that. The documentation for AlbumBase does not include a genre field. This must be a bug.

stephanebruckert commented 3 years ago

Hope these answers helped @josh-ashkinaze