A project to get CBC Music (CBC Radio 2) onto a dynamically generated Spotify playlist using an AWS Lambda scheduled task. Everyday a playlist for each of CBC Music's programs will be created.
# Clone and navigate to the repo
$ git clone https://github.com/jeffjyang/cbc-radio-2-spotify.git
$ cd cbc-radio-2-spotify
# Create your Python virtual environment
$ python3 -m venv env
# Start the virtual environment
$ source env/bin/activate
# Install dependencies
$ pip install -r requirements.txt
To shutdown the virtual environment, run
$ deactivate
- Create a Spotify developer account https://developer.spotify.com/dashboard/
- On the dashboard click "CREATE A CLIENT ID", and follow the prompts
- Into
config.py
, paste yourClient ID
andClient Secret
, and your Spotify username. - Go to your Spotify account and create 10 empty playlists
- For each of your playlists paste the playlist id into
config.py
- Navigate to each of your playlists in the Spotify web player, your playlist id will be in the url (i.e. https://open.spotify.com/user/[username]/playist/[playlist_id])
- See Discussion for the reasoning behind this
- Run
$ python3 spotify_token_cli_tool.py
to generate a Spotify access token that will be used by the script
- Login to your AWS Management Console as root
- Create an IAM user with
AWSLambdaExecute
permissions - Create an access key for the IAM user and paste the credentials into
config.py
- Create an S3 bucket and give read and write permissions to your IAM user. Follow the guide in https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html to get the canonical user ID for your IAM user
- Paste the name of your bucket into
config.py
- Create a Python 3.6 runtime AWS Lambda function
- Set the timeout of the Lambda function to a few minutes
- This script tends to take roughly 1 to 2 minutes to execute
- Add an CloudWatch Events trigger with the following scheduled rule:
cron(0 7 * * ? *)
- Note this is assuming PDT timezone
To run the script on your local machine, run $ python3 lambda_cli_tool.py
To deploy to Lambda, run $ bash deploy.sh
Spotipy, the Python client library for Spotify's API, does not support getting the playlist_id of a Spotify playlist. This is an issue since Spotipy requires a playlist's playlist_id in order to populate it with tracks.
As well, Spotify's API does not support the deletion of playlists. Since the number of programs on CBC Music may differ each day, it is inevitable that we will end up with unused playlists on days where CBC Music has fewer programs.
By creating a list of Spotify playlists, we can provide Spotipy a number of playlist_id's to use when we want to add tracks to a playlist. Now, instead of requiring Spotipy to create playlists itself, it can simply rename a playlist from "Inactive" to the title of one of CBC Music's programs when it wants to add tracks to it. Likewise, if Spotipy wants to "delete" a playlist, it can simply remove all tracks from the playlist and rename it to "Inactive".
The choice of creating 10 playlists is to ensure that we will not run out of playlists to use on days where CBC Music has a relatively large number of programs. This tends to be about 8 programs per day.