commit 1d34cca7283dd6779b139214027b979c4d6da242 Author: kolbyd Date: Wed Jun 9 21:07:42 2021 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2033883 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.env +.cache +.idea/ \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..4c68f15 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Kolby Dunning + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/Main.py b/Main.py new file mode 100644 index 0000000..515dd71 --- /dev/null +++ b/Main.py @@ -0,0 +1,70 @@ +import os +import spotipy +from datetime import date +from dotenv import load_dotenv +from termcolor import colored + + +def chunks(l, n): + for i in range(0, len(l), n): + yield l[i:i+n] + + +# Check if the environment file exists +if os.path.exists('.env'): + load_dotenv() + pass +else: + with open('.env', 'x') as f: + f.write("# .env\n" + "SPOTIFY_CLIENT_ID=\n" + "SPOTIFY_CLIENT_SECRET=") + print(colored("Fill in the required fields in '.env'.", "yellow")) + exit(1) + +# Ensure we have our ID and secret from Spotify's API +if os.getenv("SPOTIFY_CLIENT_ID") is None or os.getenv("SPOTIFY_CLIENT_SECRET") is None: + print(colored("Ensure you have included your Spotify credentials. If you don't have them, please make an app at " + "developer.spotify.com", "red")) + exit(1) + +print("Connecting to Spotify. " + colored("Check your browser in case there is a login prompt.", "yellow")) + +spotify = spotipy.Spotify(auth_manager=spotipy.oauth2.SpotifyOAuth( + client_id=os.getenv("SPOTIFY_CLIENT_ID"), + client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"), + redirect_uri="http://localhost:8080/callback/", + scope="user-library-read playlist-modify-public playlist-modify-private") +) + +print("Connected to Spotify successfully. Starting the converter.") + +flag = True # Spotify doesn't want to return all liked songs. Set to false when done. +offset = 0 # Offset to spotify songs +tracks = [] + +while flag: + chunk = spotify.current_user_saved_tracks(50, offset) + + for track in chunk['items']: + tracks.append(track['track']['uri']) + + if chunk['next'] is None: + flag = False + + offset += 50 + +# Check if the user wants the playlist to be public +playlist_public = input(colored("Do you want the playlist to be public? (y/n)", "green") + + colored(" [n] ", "yellow")) == "y" + +new_playlist = spotify.user_playlist_create(spotify.current_user()['id'], + "Liked Songs - " + date.today().strftime("%m-%d-%Y"), + playlist_public, + description="Auto generated from this user's liked songs.") + +# Use chunks of 100 due to Spotify limitations +for chunk in chunks(tracks, 100): + spotify.playlist_add_items(new_playlist['id'], chunk) + +print("Complete! Check out your new playlist at " + new_playlist['external_urls']['spotify']) diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d9a6b2 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# Spotify Playlist Converter +### Developed by [Kolby Dunning](https://kolbyd.ca) (kolbyd) + +## Setup +### Spotify +If you do not have a Spotify account, ensure that you have that first. + +Go to [developer.spotify.com](https://developer.spotify.com/) and log in with your account. Create a new +app, and go into it. Click 'edit settings', add `http://localhost:8080/callback/` as your redirect URI. + +Copy the client ID and secret to add to the `.env` file. + +The file should be the same as the one shown below: +``` +# .env +SPOTIFY_CLIENT_ID={your client ID} +SPOTIFY_CLIENT_SECRET={your client secret} +``` + +If you run `Main.py` and you don't have the variables set or the file doesn't exist, you will told by the +CLI. + +### Python +This project was built in Python 3.8. If you don't currently have Python, please visit +[python.org/downloads](https://www.python.org/downloads/). + +1. Run `pip install -r requirements.txt` to install the dependencies. +1. Fill in the required fields in `.env` (if you haven't already) +1. Run `python Main.py` +1. Follow the steps from the CLI. + +## Contribution Guide +I always appreciate contributions to my work. I prefer pull requests over issues, but both work. + +### Pull Request +- Please document your code when required +- Explain the modifications you made in the description +- What did you solve? Why is this a good addition? + +### Issues +Even though it is labeled issues, feel free to add new feature requests as well. + +- Say what the issue/feature is. +- Why does this need to be fixed/why is it a good addition? +- How would you reproduce (if it's an issue)? + +## Help/Questions? +Please email me at [kolby[at]kolbyd.ca](mailto:kolby@kolbyd.ca). \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8d23a30 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +spotipy~=2.18.0 +python-dotenv~=0.14.0 +termcolor~=1.1.0 \ No newline at end of file