From ced8ecc96741e2a44cde47e8672017c367d3e70d Mon Sep 17 00:00:00 2001 From: Olivrv Date: Sun, 3 Oct 2021 15:02:54 +0200 Subject: [PATCH] You can now download more than 100 songs from spotify at the same time ! The settings have also changed: You'll be asked how man songs you want later in the process. --- main.py | 10 +++++++--- spotinfo.py | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 3e344a2..1dec28e 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,7 @@ from spotinfo import get_playlist_items -def main(suffix="audio", download_location="Downloads", quick_mode=False, limit=50): +def main(suffix="audio", download_location="Downloads", quick_mode=False, limit=100): print( """ __ __ _ _____ _ _ _____ _ @@ -21,7 +21,6 @@ def main(suffix="audio", download_location="Downloads", quick_mode=False, limit= suffix = input("New suffix (default:audio): ") download_location = input('Download path (default:Downloads): ') quick_mode = bool(input("Quick mode (boolean expected) (default:False): ")) - limit = int(input("Max number of songs in playlist (Spotify) (default:50): ")) choice1 = int(input("Would you like to download a single song (1) or multiple songs (2) ? \n>>> ")) if choice1 == 1: song = input('Please enter the name of the song. \n>>> ') @@ -64,10 +63,15 @@ def main(suffix="audio", download_location="Downloads", quick_mode=False, limit= try: inputSpotifyPlaylist = input("Please enter the link of the playlist.\n>>> ") playlist_link = inputSpotifyPlaylist[-22:] + nlimit = input("How many songs would you like to download? \n>>> ") + try: + limit = int(nlimit) + except ValueError: + limit = 50 songs = get_playlist_items(playlist_link, limit) print("Starting download...") for i in songs: - download(i) + download(i, suffix, download_location, quick_mode) print("Downloaded", i) print("Done.") except KeyError: diff --git a/spotinfo.py b/spotinfo.py index d4b1806..69657dc 100644 --- a/spotinfo.py +++ b/spotinfo.py @@ -26,13 +26,35 @@ } -def get_playlist_items(playlist_id: str, limit=50) -> list: +def get_hundred_songs(playlist_id: str, limit=100, offset=0) -> list: r = requests.get(BASE_URL + 'playlists/' + playlist_id + '/tracks', headers=headers, - params={'include_groups': 'track', 'limit': limit}) + params={'include_groups': 'track', 'limit': limit, 'offset': offset}) d = r.json() tracks = list() for track in d['items']: tracks.append(track['track']['name'] + ' by ' + track["track"]["album"]["artists"][0]["name"]) return tracks + + +def get_playlist_items(playlist_id: str, limit=50) -> list: + if limit <= 100: + r = requests.get(BASE_URL + 'playlists/' + playlist_id + '/tracks', + headers=headers, + params={'include_groups': 'track', 'limit': limit}) + + d = r.json() + tracks = list() + for track in d['items']: + tracks.append(track['track']['name'] + ' by ' + track["track"]["album"]["artists"][0]["name"]) + return tracks + else: + offset = 0 + tracks = list() + for i in range(int(limit/100)): + tracks += (get_hundred_songs(playlist_id, offset=offset)) + offset += 100 + tracks += (get_hundred_songs(playlist_id, limit=(limit % 100), offset=offset)) + return tracks +