spotipy/examples/artist_albums.py
Niko 2bc8d35fc6
Formatted strings (#1178)
* use newer string formatters (https://pyformat.info)

* fix typo

* went through the files again, additions from #1165 are also included

* replace .format() with f strings
2025-01-14 07:56:47 +01:00

53 lines
1.3 KiB
Python

import argparse
import logging
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
logger = logging.getLogger('examples.artist_albums')
logging.basicConfig(level='INFO')
sp = spotipy.Spotify(client_credentials_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='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()