From 8d48ab79658dc87da1214a71020b30586c724409 Mon Sep 17 00:00:00 2001 From: Aayush Patel Date: Mon, 24 Jan 2022 19:18:34 +0530 Subject: [PATCH] Feature to download multiple playlists (#231) Co-authored-by: Sathyajith Bhat --- GETTING_STARTED.md | 33 ++++++++++++++++++++++----------- README.md | 13 ++++++------- spotify_dl/spotify_dl.py | 40 ++++++++++++++++++++-------------------- 3 files changed, 48 insertions(+), 38 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 41e9d0b2..6c18374f 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -7,13 +7,13 @@ You need Python 3.6+. To download songs as MP3, you will need ffmpeg. - Linux users can get them by installing libav-tools by using apt-get `sudo apt-get install -y libav-tools`) or a package manager which comes with your distro - Windows users can download FFMPEG pre-built binaries from [here](http://ffmpeg.zeranoe.com/builds/). Extract the file using [7-zip](http://7-zip.org/) to a foldrer and [add the folder to your PATH environment variable](http://www.wikihow.com/Install-FFmpeg-on-Windows) -1. Install using pip +1. Install using pip pip3 install spotify_dl -2. Login to [Spotify developer console](https://developer.spotify.com/my-applications/#!/applications) and click on "Create an App". Fill in details for name and description +2. Login to [Spotify developer console](https://developer.spotify.com/my-applications/#!/applications) and click on "Create an App". Fill in details for name and description -3. Make a note of Client ID and Client Secret. These values need to be then set `SPOTIPY_CLIENT_ID`, `SPOTIPY_CLIENT_SECRET` environment variables respectively. +3. Make a note of Client ID and Client Secret. These values need to be then set `SPOTIPY_CLIENT_ID`, `SPOTIPY_CLIENT_SECRET` environment variables respectively. You can set environment variables as mentioned below: @@ -37,11 +37,20 @@ You need Python 3.6+. To download songs as MP3, you will need ffmpeg. For most users `spotify_dl -l spotify_playlist_link -o download_directory` should do where - `spotify_playlist_link` is a link to Spotify's playlist. You can get it from the 3-dot menu. + + ![image](images/spotify-playlist.png) + + spotify_dl also supports downloading multiple playlist, you can pass them like below: + + `spotify_dl -l spotify_playlist1 spotify_playlist2 -o download_directory` + + - `download_directory` is the location where the songs must be downloaded to. If you give a `.` then it will download to the current directory. - ![image](images/spotify-playlist.png) +5. To retrieve download songs as MP3, you will need to install ffmpeg. If you prefer to skip MP3 conversion, pass `-m` or `--skip_mp3` as a parameter when running the script + +- Linux users can get them by installing libav-tools by using apt-get (`sudo apt-get install -y libav-tools`) if you'rte using a Debian/Ubuntu or a package manager which comes with your distro +- Windows users can download FFMPEG pre-built binaries from [here](http://ffmpeg.zeranoe.com/builds/). Extract the file using [7-zip](http://7-zip.org/) to a foldrer and [add the folder to your PATH environment variable](http://www.wikihow.com/Install-FFmpeg-on-Windows) - If the Spotify playlist link is skipped then it will download songs from your "My Music" collection - - `download_directory` is the location where the songs must be downloaded to. If you give a `.` then it will download to the current directory. 6. If you prefer to skip MP3 conversion, pass `-m` or `--skip_mp3` as a parameter when running the script @@ -51,7 +60,8 @@ You need Python 3.6+. To download songs as MP3, you will need ffmpeg. Build the Docker image from the Dockerfile, run the following command in the spotify_dl root directory: `docker build -t spotify_dl .` Run the Docker image with your client ID and secret: -``` bash + +```bash docker run -d --rm \ -e SPOTIPY_CLIENT_ID=client_id \ -e SPOTIPY_CLIENT_SECRET=client_secret \ @@ -67,10 +77,11 @@ You can also run the bulk downloading script, make sure to configure it first: ` ### How do I set defaults? You can set defaults per user by creating a file at `~/.spotify_dl_settings`. Create a key with value for every argument you want a default for. Example: -``` json + +```json { - "output" : "/home/foo/spotify-dl-output" - , "verbose" : "true" - , "skip_mp3" : "t" + "output": "/home/foo/spotify-dl-output", + "verbose": "true", + "skip_mp3": "t" } ``` diff --git a/README.md b/README.md index d9130694..36f69448 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ## spotify_dl + Downloads songs from any Spotify playlist, album or track. [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com) @@ -12,7 +13,6 @@ Downloads songs from any Spotify playlist, album or track. [![Awesome Badges](https://img.shields.io/badge/badges-awesome-green.svg)](https://github.com/Naereen/badges) [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/E1E55G3EI) - ### Tell me more! I wanted an easy way to grab the songs present in my library so I can download it & use it offline. [spotify_to_mp3](https://github.com/frosas/spotify-to-mp3) worked well but it relied on grooveshark, which unfortunately is no more. So I wrote this script which mimics that library, but instead of downloading from grooveshark, it searches YouTube and downloads the song using [youtube-dl](https://rg3.github.io/youtube-dl/). @@ -23,9 +23,9 @@ Install using pip pip3 install spotify_dl -Run the program +Run the program - spotify_dl -l spotify_playlist_link -o download_directory + spotify_dl -l spotify_playlist_link/s -o download_directory For running in verbose mode, append `-V` @@ -37,14 +37,13 @@ For more details and other arguments, issue `-h` See [the getting started guide](https://github.com/SathyaBhat/spotify-dl/blob/master/GETTING_STARTED.md) for more details. - -### Contributing and Local development +### Contributing and Local development Pull requests and any contributions are always welcome. Please open an issue with your proposal before you start with something. #### Running tests -At the moment, there are barely any tests but PRs always welcome to improve this. Tests are setup and run with pytest, run +At the moment, there are barely any tests but PRs always welcome to improve this. Tests are setup and run with pytest, run make tests @@ -56,4 +55,4 @@ Take a look at [CONTRIBUTORS](/CONTRIBUTORS.md) for a list of all people who hav ### Issues, Feedback, Contact details -Feel free to raise any bugs/issues under Github issues. Pull requests are also more than welcome. +Feel free to raise any bugs/issues under Github issues. Pull requests are also more than welcome. diff --git a/spotify_dl/spotify_dl.py b/spotify_dl/spotify_dl.py index 319c6ed0..de8633ed 100755 --- a/spotify_dl/spotify_dl.py +++ b/spotify_dl/spotify_dl.py @@ -18,7 +18,7 @@ def spotify_dl(): """Main entry point of the script.""" parser = argparse.ArgumentParser(prog='spotify_dl') parser.add_argument('-l', '--url', action="store", - help="Spotify Playlist link URL", type=str, required=False) + help="Spotify Playlist link URL", type=str, nargs='+', required=True) parser.add_argument('-o', '--output', type=str, action='store', help='Specify download directory.', required=False) parser.add_argument('-d', '--download', action='store_true', @@ -71,27 +71,27 @@ def spotify_dl(): sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials()) log.debug('Arguments: {}'.format(args)) - if args.url: - valid_item = validate_spotify_url(args.url) + for url in args.url: + if url: + valid_item = validate_spotify_url(url) - if not valid_item: - sys.exit(1) + if not valid_item: + sys.exit(1) + + if args.output: + item_type, item_id = parse_spotify_url(url) + directory_name = get_item_name(sp, item_type, item_id) + save_path = Path(PurePath.joinpath(Path(args.output), Path(directory_name))) + save_path.mkdir(parents=True, exist_ok=True) + console.print(f"Saving songs to [bold green]{directory_name}[/bold green] directory") + + songs = fetch_tracks(sp, item_type, url) + if args.download is True: + file_name_f = default_filename + if args.keep_playlist_order: + file_name_f = playlist_num_filename - if args.output: - item_type, item_id = parse_spotify_url(args.url) - directory_name = get_item_name(sp, item_type, item_id) - save_path = Path(PurePath.joinpath(Path(args.output), Path(directory_name))) - save_path.mkdir(parents=True, exist_ok=True) - console.print(f"Saving songs to [bold green]{directory_name}[/bold green] directory") - - songs = fetch_tracks(sp, item_type, args.url) - if args.download is True: - file_name_f = default_filename - if args.keep_playlist_order: - file_name_f = playlist_num_filename - - download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, args.no_overwrites, - file_name_f) + download_songs(songs, save_path, args.format_str, args.skip_mp3, args.keep_playlist_order, args.no_overwrites, file_name_f) if __name__ == '__main__':