A simple web app for managing and adding songs to your Spotify playlists.
Spotify SongAdder allows users to authenticate with Spotify and add songs to their playlists with ease. The app allows you to process playlists by entering the playlist link, adding tracks, and viewing feedback on added, skipped, or missing tracks.
Here's the UI:
I created a test playlist for this demonstration.
And here's the .txt file that you need to enter the song
Here's how it looks after we add the songs.
The reason I started this project was because I was watching the man, the myth, the legend Sebastian Lague, and I found myself really enjoying the background music in his videos. I was always into editing, so I liked saving songs to use in the background and his video descriptions usually included a list of the songs he used. Since his videos were usually quite long, that meant there were a lot of songs and of course he would use some of the same songs in his other videos. That's why I decided to automate this process of copying a song title from his description and adding it to my Spotify playlist one by one. I did NOT want to spend hours torturing myself by doing all of that manual task, so instead, I tortured myself for hours making this app instead.
I was done in about a day or two, and I was fairly happy with the project because it solved a problem I had. But it actually turned out to solve one of my friend's problem too. He was getting song recommendations from everyone and he created this Spotify playlist where he would add one song (which later became 5) a day for a year. However, he wanted there to be no repeat of artists. So that's why my program currently has a "allow for repeated artists" button. Fun story I suppose.
This is also the project that made me learn the value of environmental variables. I'd like to think every developer at some point accidently pushed their API key to GitHub so to avoid that I created environmental variables and stored my secrets there.
-
Technologies Used:
Python– Core language for application logicFlask– Web framework for routing, sessions, and rendering templatesSpotipy– Spotify Web API wrapper for authentication and playlist operationsHTML– Rendered by Flask for forms and feedback pagesSpotify Web API– Handles track search, playlist modification, authenticationFlaskSessionCacheHandler– Manages Spotify OAuth tokens via Flask sessions
-
There's a cache handler to store the OAuth access token in the Flask session, so users don’t have to log in repeatedly. Checks whether the user is authenticated by validating the cached token stored in the Flask session.
-
If the token is not valid, the app generates an authorisation URL where the user can log in and grant permissions. If the user is authenticated, the app renders an HTML form where the user can enter a Spotify playlist URL. If an error message exists, it’s displayed in a red box.
-
Extracts the playlist URL from the form submission. Calls a helper function to extract the playlist ID from the URL. If the playlist ID is invalid, the user is redirected back to the homepage with an error message.
-
Opens the file list_of_songs.txt in read mode. This file is expected to contain a list of song names (one per line).
- Encodes the string into windows-1252 (a character encoding), and then decodes it back into utf-8. This step helps ensure compatibility with special characters or encoding issues that might arise when reading the file. For example if the song has a
'symbol it can cause problems.
- Encodes the string into windows-1252 (a character encoding), and then decodes it back into utf-8. This step helps ensure compatibility with special characters or encoding issues that might arise when reading the file. For example if the song has a
-
Uses the Spotipy search() method to search Spotify for tracks that match the query. The q=query parameter specifies the search term (the song name), type='track' restricts the search to tracks only, and limit=1 ensures only one result is returned (the most relevant one).
- Checks if any tracks were returned from the search (search_results['tracks']['items']). If the list is not empty, it means the track was found on Spotify. Then extracts every needed info like name and url, also checks if track is already in the playlist.
- Rather than a file, accept text book from website
- Remove "allow repeated artists" button
Follow these steps to set up the project on your local machine:
Make sure you have the following installed:
- Python 3.x (preferably Python 3.7+)
- Git (to clone the repository)
- Spotify Developer Account (for creating a Spotify API application and obtaining credentials)
Open your terminal and clone the project repository:
git clone https://github.com/Chetoh16/song-adderNavigate to the project directory and create a virtual environment:
cd spotify-songadder
python -m venv venvWindows:
venv\Scripts\activatemacOS/Linux:
source venv/bin/activateOnce the virtual environment is activated, install the required dependencies from requirements.txt:
pip install -r requirements.txtTo use the Spotify API, you need to create an app on the Spotify Developer Dashboard.
- Go to the Spotify Developer Dashboard and log in with your Spotify account.
- Create a new app.
- Note down the Client ID, Client Secret, and Redirect URI (you will need to set this URI in your app as well).
Create a .env file in the root directory of the project. This file will store sensitive information such as your Spotify API credentials. Add the following contents to the .env file:
client_id=your_client_id
client_secret=your_client_secretReplace your_client_id and your_client_secret with your actual credentials.
After setting up everything, run the Flask application:
python song_adder.pyThis should start the server locally at http://localhost:5000. Open this URL in your browser to use the app.
- Log In: When you visit the homepage, you'll be prompted to log in to your Spotify account.
- Add Songs: After logging in, you can paste your Spotify playlist URL to add songs to the playlist.
- Error Handling: If the playlist link is invalid, an error message will appear.
- Song Processing Feedback: Once songs are processed, you'll see feedback showing which songs were added, skipped, or couldn't be found.



