From 237b4cca7e78e679c8dee6a902bec72ac5be2005 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Val=C3=A9rian=20Galliat?= Date: Sun, 8 Oct 2017 10:13:55 -0400 Subject: [PATCH] Add an option to grab an album (#142) --- README.md | 15 +++++++++++++++ core/misc.py | 2 ++ spotdl.py | 51 +++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 190ee2f..b500c91 100755 --- a/README.md +++ b/README.md @@ -88,6 +88,9 @@ optional arguments: -p PLAYLIST, --playlist PLAYLIST load songs from playlist URL into .txt (default: None) + -b ALBUM, --album ALBUM + load songs from album URL into .txt + (default: None) -u USERNAME, --username USERNAME load songs from user's playlist into .txt (default: None) @@ -165,6 +168,18 @@ For example - Then you can simply run `python3 spotdl.py --list=.txt` to download all the tracks. +#### Download albums + +- You can copy the Spotify URL of the album and pass it in `--album` option. + +For example + +- `python3 spotdl.py --album https://open.spotify.com/album/7CjakTZxwIF8oixONe6Bpb` + +- The script will load all the tracks from the album into `.txt` + +- Then you can simply run `python3 spotdl.py --list=.txt` to download all the tracks. + #### Download playlists by username - You can also load songs using Spotify username if you don't have the playlist URL. (Open profile in Spotify, click on the three little dots below name, "Share", "Copy to clipboard", paste last numbers into command-line: `https://open.spotify.com/user/0123456790`) diff --git a/core/misc.py b/core/misc.py index 3a6db49..4eb9e30 100755 --- a/core/misc.py +++ b/core/misc.py @@ -41,6 +41,8 @@ def get_arguments(): '-l', '--list', help='download songs from a file') group.add_argument( '-p', '--playlist', help='load songs from playlist URL into .txt') + group.add_argument( + '-b', '--album', help='load songs from album URL into .txt') group.add_argument( '-u', '--username', help="load songs from user's playlist into .txt") diff --git a/spotdl.py b/spotdl.py index fbcf6f6..79bc17a 100755 --- a/spotdl.py +++ b/spotdl.py @@ -194,20 +194,17 @@ def feed_playlist(username): print('') playlist = misc.input_link(links) print('') - write_tracks(playlist) + write_playlist(playlist) -def write_tracks(playlist): - results = spotify.user_playlist( - playlist['owner']['id'], playlist['id'], fields='tracks,next') - text_file = u'{0}.txt'.format(slugify(playlist['name'], ok='-_()[]{}')) - print(u'Feeding {0} tracks to {1}'.format(playlist['tracks']['total'], text_file)) - - tracks = results['tracks'] +def write_tracks(text_file, tracks): with open(text_file, 'a') as file_out: while True: for item in tracks['items']: - track = item['track'] + if 'track' in item: + track = item['track'] + else: + track = item try: file_out.write(track['external_urls']['spotify'] + '\n') except KeyError: @@ -221,6 +218,24 @@ def write_tracks(playlist): break +def write_playlist(playlist): + results = spotify.user_playlist( + playlist['owner']['id'], playlist['id'], fields='tracks,next') + text_file = u'{0}.txt'.format(slugify(playlist['name'], ok='-_()[]{}')) + print(u'Feeding {0} tracks to {1}'.format(playlist['tracks']['total'], text_file)) + + tracks = results['tracks'] + write_tracks(text_file, tracks) + + +def write_album(album): + tracks = spotify.album_tracks(album['id']) + text_file = u'{0}.txt'.format(slugify(album['name'], ok='-_()[]{}')) + print(u'Feeding {0} tracks to {1}'.format(tracks['total'], text_file)) + + write_tracks(text_file, tracks) + + def download_song(file_name, content): """Download the audio file from YouTube.""" if args.input_ext == '.webm': @@ -339,7 +354,21 @@ def grab_playlist(playlist): else: break - write_tracks(playlist) + write_playlist(playlist) + + +def grab_album(album): + if '/' in album: + if album.endswith('/'): + playlist = playlist[:-1] + splits = album.split('/') + else: + splits = album.split(':') + + album_id = splits[-1] + album = spotify.album(album_id) + + write_album(album) def grab_single(raw_song, number=None): @@ -410,6 +439,8 @@ if __name__ == '__main__': grab_list(text_file=args.list) elif args.playlist: grab_playlist(playlist=args.playlist) + elif args.album: + grab_album(album=args.album) elif args.username: feed_playlist(username=args.username) else: