import argparse import logging import spotipy from spotipy.oauth2 import SpotifyClientCredentials logger = logging.getLogger('examples.artist_albums') logging.basicConfig(level='INFO') sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials()) def get_args(): parser = argparse.ArgumentParser(description='Gets albums from artist') parser.add_argument('-a', '--artist', required=True, help='Name of Artist') return parser.parse_args() def get_artist(name): results = sp.search(q=f'artist:{name}', type='artist') items = results['artists']['items'] return items[0] if items else 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(f'ALBUM: {name}') seen.add(name) def main(): args = get_args() artist = get_artist(args.artist) if artist: show_artist_albums(artist) else: logger.error(f"Can't find artist: {artist}") if __name__ == '__main__': main()