Discover Festival Gems with a Python Script
Do you often find yourself at festivals, overwhelmed by the sheer number of bands and unsure of which ones to see? Especially at smaller festivals with a plethora of lesser-known yet precious artists, it can be challenging to decide where to invest your time. I recently faced this dilemma at a festival with my girlfriend. My solution was to create a Python script that takes a list of artists as input and outputs a Spotify playlist of their most popular tracks. This makes it easy to get an idea of what each artist has to offer and helps in making an informed decision on which performances to attend.
Here’s how you can set up and use this script.
Step 1: Install Dependencies
First, you need to install the necessary Python libraries. This script uses the spotipy
library to interact with the Spotify API. Install it using pip:
pip install spotipy
Step 2: Set Up a Spotify Developer Account
To use the Spotify API, you need to create a Spotify Developer account and set up a new application to get the necessary credentials.
- Go to the Spotify Developer Dashboard and log in with your Spotify account.
- Click on “Create an App” and fill in the required details.
- Once your app is created, you will see the
Client ID
andClient Secret
. You will need these for your script. - Set the Redirect URI for your app. This can be any valid URI; for development purposes, you can use
http://localhost:8888/callback
.
Step 3: Configure Your Script
Create a config.json
file in the same directory as your script with the following content:
{
"client_id": "your_spotify_client_id",
"client_secret": "your_spotify_client_secret",
"redirect_uri": "your_spotify_redirect_uri"
}
Replace your_spotify_client_id
, your_spotify_client_secret
, and your_spotify_redirect_uri
with the values you obtained from your Spotify Developer Dashboard.
Step 4: Prepare Your List of Artists
Create a text file named artists.txt
in an input
folder in the same directory as your script. List each artist’s name on a new line. For example:
Artist One
Artist Two
Artist Three
Step 5: Run the Script
Download the script from the following GitLab repository: Spotify Playlist Generator Script
Run the script from your terminal:
python playlistgen.py
This script will read the list of artists from artists.txt
, fetch their top tracks from Spotify, and create a playlist in your Spotify account named “Top Tracks by Artists”.
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json
import os
import sys
def load_artists(file_path):
with open(file_path, 'r') as file:
return [line.strip() for line in file]
def load_config(config_path):
with open(config_path, 'r') as file:
return json.load(file)
def main(artists_file_path):
script_dir = os.path.dirname(os.path.abspath(__file__))
input_folder = os.path.join(script_dir, 'input')
# Load artists from file
artists_file_path = artists_file_path or os.path.join(input_folder, 'artists.txt')
artists = load_artists(artists_file_path)
# Load Spotify credentials from config.json
config_path = os.path.join(script_dir, 'config.json')
config = load_config(config_path)
# Initialize the Spotify API client
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=config['client_id'],
client_secret=config['client_secret'],
redirect_uri=config['redirect_uri'],
scope='playlist-modify-public'))
# Create a new playlist
playlist = sp.user_playlist_create(sp.current_user()['id'], 'Top Tracks by Artists')
# Iterate through the list of artists and add their top track to the playlist
for artist_name in artists:
results = sp.search(q=f'artist:"{artist_name}"', type='artist')
if results['artists']['items']:
artist = results['artists']['items'][0]
top_tracks = sp.artist_top_tracks(artist['id'])
if top_tracks['tracks']:
top_track = top_tracks['tracks'][0]
sp.playlist_add_items(playlist['id'], [top_track['uri']])
print(f'Playlist "{playlist["name"]}" created with the top tracks of the listed artists.')
if __name__ == "__main__":
artists_file_path = sys.argv[1] if len(sys.argv) > 1 else None
main(artists_file_path)
Now, you have a customized Spotify playlist featuring the top tracks of artists performing at your festival, making it easier to decide which bands to see live. Enjoy the music and have a great time at your festival! Source