From 2b42f0b3a122440c0310c6b8202b90df58ce35d9 Mon Sep 17 00:00:00 2001 From: AlfredoSequeida Date: Wed, 10 Oct 2018 23:53:10 -0700 Subject: [PATCH] added the ability to get all artist's songs as suggested by #366 (#389) * added the ability to get all artist's songs as suggested by #366 * added log to featch_all_albums_from_artist function and removed the use of a uri * updated the functionality to get all albums with the ability to get singles * updated main function with new write_all_albums_from_artist function to get all albums from an artist * fixed typos * updated test case for getting all albums from artist * fixed typos --- spotdl/handle.py | 5 ++++ spotdl/spotdl.py | 3 +++ spotdl/spotify_tools.py | 55 ++++++++++++++++++++++++++++++++++++++++- test/test_list.py | 12 +++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/spotdl/handle.py b/spotdl/handle.py index 9a1408f..0d62a54 100644 --- a/spotdl/handle.py +++ b/spotdl/handle.py @@ -110,6 +110,11 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True): group.add_argument( "-b", "--album", help="load tracks from album URL into .txt" ) + group.add_argument( + "-ab", + "--all-albums", + help="load all tracks from artist URL into .txt" + ) group.add_argument( "-u", "--username", diff --git a/spotdl/spotdl.py b/spotdl/spotdl.py index 7b33f9c..1560032 100755 --- a/spotdl/spotdl.py +++ b/spotdl/spotdl.py @@ -214,6 +214,7 @@ def main(): log.debug("Platform: {}".format(platform.platform())) log.debug(pprint.pformat(const.args.__dict__)) + try: if const.args.song: download_single(raw_song=const.args.song) @@ -227,6 +228,8 @@ def main(): spotify_tools.write_playlist(playlist_url=const.args.playlist) elif const.args.album: spotify_tools.write_album(album_url=const.args.album) + elif const.args.all_albums: + spotify_tools.write_all_albums_from_artist(artist_url=const.args.all_albums) elif const.args.username: spotify_tools.write_user_playlist(username=const.args.username) diff --git a/spotdl/spotify_tools.py b/spotdl/spotify_tools.py index 75fdbc2..ece1bff 100644 --- a/spotdl/spotify_tools.py +++ b/spotdl/spotify_tools.py @@ -10,7 +10,6 @@ from titlecase import titlecase import pprint import sys - def generate_token(): """ Generate the token. Please respect these credentials :) """ credentials = oauth2.SpotifyClientCredentials( @@ -150,6 +149,60 @@ def fetch_album(album): album = spotify.album(album_id) return album +def fetch_album_from_artist(artist_url, album_type='album'): + """ + This funcction returns all the albums from a give artist_url using the US + market + :param artist_url - spotify artist url + :param album_type - the type of album to fetch (ex: single) the default is + a standard album + :param return - the album from the artist + """ + + #fetching artist's albums limitting the results to the US to avoid duplicate + #albums from multiple markets + results = spotify.artist_albums(artist_url, album_type=album_type, country='US') + + albums = results['items'] + + #indexing all pages of results + while results['next']: + results = spotify.next(results) + albums.extend(results['items']) + + return albums + + +def write_all_albums_from_artist(artist_url, text_file=None): + """ + This function gets all albums from an artist and writes it to a file in the + current working directory called [ARTIST].txt, where [ARTIST] is the artist + of the album + :param artist_url - spotify artist url + :param text_file - file to write albums to + """ + + album_base_url = 'https://open.spotify.com/album/' + + #fetching all default albums + albums = fetch_album_from_artist(artist_url) + + #if no file if given, the default save file is in the current working + #directory with the name of the artist + if text_file is None: + text_file = albums[0]['artists'][0]['name']+'.txt' + + for album in albums: + #logging album name + log.info('Fetching album: ' + album['name']) + write_album(album_base_url + album['id'], text_file=text_file) + + #fetching all single albums + singles = fetch_album_from_artist(artist_url, album_type='single') + + for single in singles: + log.info('Fetching single: ' + single['name']) + write_album(album_base_url + single['id'], text_file=text_file) def write_album(album_url, text_file=None): album = fetch_album(album_url) diff --git a/test/test_list.py b/test/test_list.py index a44f86f..96eec41 100644 --- a/test/test_list.py +++ b/test/test_list.py @@ -7,6 +7,7 @@ from spotdl import spotdl PLAYLIST_URL = "https://open.spotify.com/user/alex/playlist/0iWOVoumWlkXIrrBTSJmN8" ALBUM_URL = "https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg" +ARTIST_URL = "https://open.spotify.com/artist/4dpARuHxo51G3z768sgnrY" def test_user_playlists(tmpdir, monkeypatch): @@ -37,6 +38,17 @@ def test_album(tmpdir): tracks = len(f.readlines()) assert tracks == expect_tracks +def test_all_albums(tmpdir): + #current number of tracks on spotify since as of 10/10/2018 + #in US market only + expect_tracks = 49 + global text_file + text_file = os.path.join(str(tmpdir), 'test_ab.txt') + spotify_tools.write_all_albums_from_artist(ARTIST_URL, text_file) + with open(text_file, 'r') as f: + tracks = len(f.readlines()) + assert tracks == expect_tracks + def test_trim(): with open(text_file, "r") as track_file: