Add an option to grab an album (#142)

This commit is contained in:
Valérian Galliat
2017-10-08 10:13:55 -04:00
committed by Ritiek Malhotra
parent 98d21bbad9
commit 237b4cca7e
3 changed files with 58 additions and 10 deletions

View File

@@ -88,6 +88,9 @@ optional arguments:
-p PLAYLIST, --playlist PLAYLIST
load songs from playlist URL into <playlist_name>.txt
(default: None)
-b ALBUM, --album ALBUM
load songs from album URL into <album_name>.txt
(default: None)
-u USERNAME, --username USERNAME
load songs from user's playlist into
<playlist_name>.txt (default: None)
@@ -165,6 +168,18 @@ For example
- Then you can simply run `python3 spotdl.py --list=<playlist_name>.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 `<album_name>.txt`
- Then you can simply run `python3 spotdl.py --list=<album_name>.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`)

View File

@@ -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 <playlist_name>.txt')
group.add_argument(
'-b', '--album', help='load songs from album URL into <album_name>.txt')
group.add_argument(
'-u', '--username',
help="load songs from user's playlist into <playlist_name>.txt")

View File

@@ -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: