2021-10-23 17:36:37 +02:00
|
|
|
from const import ITEMS, ARTISTS, NAME, ID
|
2021-11-19 18:45:04 +01:00
|
|
|
from termoutput import Printer
|
2021-10-23 17:36:37 +02:00
|
|
|
from track import download_track
|
2021-10-30 11:43:07 +02:00
|
|
|
from utils import fix_filename
|
2021-10-23 17:36:37 +02:00
|
|
|
from zspotify import ZSpotify
|
|
|
|
|
|
|
|
ALBUM_URL = 'https://api.spotify.com/v1/albums'
|
|
|
|
ARTIST_URL = 'https://api.spotify.com/v1/artists'
|
|
|
|
|
|
|
|
|
|
|
|
def get_album_tracks(album_id):
|
|
|
|
""" Returns album tracklist """
|
|
|
|
songs = []
|
|
|
|
offset = 0
|
|
|
|
limit = 50
|
|
|
|
|
|
|
|
while True:
|
2021-10-25 07:36:17 +02:00
|
|
|
resp = ZSpotify.invoke_url_with_params(f'{ALBUM_URL}/{album_id}/tracks', limit=limit, offset=offset)
|
2021-10-23 17:36:37 +02:00
|
|
|
offset += limit
|
|
|
|
songs.extend(resp[ITEMS])
|
|
|
|
if len(resp[ITEMS]) < limit:
|
|
|
|
break
|
|
|
|
|
|
|
|
return songs
|
|
|
|
|
|
|
|
|
|
|
|
def get_album_name(album_id):
|
|
|
|
""" Returns album name """
|
2021-11-25 10:25:25 +01:00
|
|
|
(raw, resp) = ZSpotify.invoke_url(f'{ALBUM_URL}/{album_id}')
|
2021-10-30 11:43:07 +02:00
|
|
|
return resp[ARTISTS][0][NAME], fix_filename(resp[NAME])
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_artist_albums(artist_id):
|
|
|
|
""" Returns artist's albums """
|
2021-11-25 10:25:25 +01:00
|
|
|
(raw, resp) = ZSpotify.invoke_url(f'{ARTIST_URL}/{artist_id}/albums?include_groups=album%2Csingle')
|
2021-10-23 17:36:37 +02:00
|
|
|
# Return a list each album's id
|
2021-10-24 18:18:18 +02:00
|
|
|
album_ids = [resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))]
|
|
|
|
# Recursive requests to get all albums including singles an EPs
|
|
|
|
while resp['next'] is not None:
|
2021-11-25 10:25:25 +01:00
|
|
|
(raw, resp) = ZSpotify.invoke_url(resp['next'])
|
2021-10-24 18:18:18 +02:00
|
|
|
album_ids.extend([resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))])
|
|
|
|
|
|
|
|
return album_ids
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def download_album(album):
|
|
|
|
""" Downloads songs from an album """
|
|
|
|
artist, album_name = get_album_name(album)
|
|
|
|
tracks = get_album_tracks(album)
|
2021-11-19 18:45:04 +01:00
|
|
|
for n, track in Printer.progress(enumerate(tracks, start=1), unit_scale=True, unit='Song', total=len(tracks)):
|
2021-11-19 17:54:07 +01:00
|
|
|
download_track('album', track[ID], extra_keys={'album_num': str(n).zfill(2), 'artist': artist, 'album': album_name, 'album_id': album}, disable_progressbar=True)
|
2021-10-23 17:36:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def download_artist_albums(artist):
|
|
|
|
""" Downloads albums of an artist """
|
|
|
|
albums = get_artist_albums(artist)
|
|
|
|
for album_id in albums:
|
|
|
|
download_album(album_id)
|