Skip to content

Latest commit

 

History

History
126 lines (99 loc) · 8.1 KB

README.md

File metadata and controls

126 lines (99 loc) · 8.1 KB

Spotify Save New Music Friday

Based on the original (spotify-save-discover-weekly) by (@RegsonDR)

Caution

Introducing some changes to our Web API (Spotify for Developers).

As of November 27, 2024, “important security changes” 🙄🤥 introduced by Spotify effectively rendered this project and any other project using non-public API access (still under development) unusable.

Listing item 8 “Web API endpoint integrationprevents access to Spotify's editorial playlists (NMF and NMF Dance are owned by Spotify, managed directly by the streaming service). There was no advance notice, let alone any thought of amateur projects that moved nothing for the purpose of service fruition.

Therefore, until I have a way, time, and desire to better understand how it works and if there is a way around this obstacle, I have disabled GitHub actions and updated the 2024 JSON file with the last playlist saved (manually) at the end of November.

It was nice, convenient and, I hope, useful for someone other than me.

❤️

Append songs Backup New Music Friday

This script automatically saves your "New Music Friday" playlist which is generated by Spotify and refreshed every Friday. The songs from the temporary playlist are saved into a permanent playlist (a cumulative one or brand new playlist every Friday), using the Spotify API (Authorization Code Flow).

The automation is powered by GitHub Actions and executes automatically on Fridays as defined in the append.yaml (cumulative playlist, append songs every Friday) and/or nmf.yaml (brand new playlist every Friday).

Initial Set Up (approx: 10 minutes)

You should not need to make any commits back to the repo. The files in /setup will help obtain the authorization information for setting up the environment variables in GitHub secrets in order to allow main.py and/or nmf.py to execute properly. You need to fork this repo in order to have your own instance of GitHub Actions.

(1) Create a Fork

Start off with simple fork by clicking on the "Fork" button. Once you've done that, you can use your favorite git client to clone your repo or use the command line:

# Clone your fork to your local machine
$ git clone https://github.com/gioxx/spotify-save-new-music-friday.git

(2) Libraries

Use the package manager pip to install all of the required libraries. You could use this with a virtual environment if required.

$ pip install -r requirements.txt

(3) Spotify API Credentials

  1. Open the .sample.env file from the /setup folder on your local machine.
  2. Sign into your Spotify API Dashboard and create a new application. You can use any uri for the redirect uri, this is the base uri you will be redirected to after authorizing the app to access your account. If you see a "INVALID_CLIENT: Invalid redirect URI", then edit settings of your app from the Spotify dashboard and add your uri as a redirect uri.
  3. Fill out the env file with the same Client ID, Secret and Redirect URI details used in step 2 and save this file as .env. Do not post these details anywhere publically.

Example:

CLIENT_ID=thisisanid
CLIENT_SECRET=thisisasecret
REDIRECT_URI=http://localhost:8888/callback
  1. Execute authorization.py, the script will have you authenticate to Spotify, ask for the necessary permissions, and return the refresh token.

Example:

$python authorization.py
Step 1: Get the authorization code...
Server waiting for the authorization code...
127.0.0.1 - - [03/Dec/2024 14:10:17] "GET /callback?code=XXXXXXXX HTTP/1.1" 200 -
Step 2: Exchange the authorization code for a refresh token...
Access Token: XXXX-YYYY-ZZZZ-LLLLLL-s
Refresh Token: UUUUUUUU-XXXXXXX

Operation completed!
Save your REFRESH_TOKEN in the .env file:
REFRESH_TOKEN=POHFDJAHDIGYUVEQ-jhbjhabfdujsfbu23y7u231

(4) GitHub Actions

  1. Go to the settings of your forked repo and click on Secrets.
  2. You will need to create the following secrets:
  • CLIENT_ID - Use the same Client ID from your .env.
  • CLIENT_SECRET - Use the same Client Secret from your .env
  • REFRESH_TOKEN - Use the refresh token generated in the (3) Spotify API Credentials instructions above.
  • NEW_MUSIC_FRIDAY_ID - This is the ID of your New Music Friday playlist, which can obtained using the method described below.
  • SAVE_TO_ID - This is the ID of your permanent playlist, which can be obtained using the method described below. You will need to create a new playlist or use an existing playlist if there is somewhere you would like to already save the songs into.
  • USER_ID - This is the ID of your user (is the username you have choosen and used to register on Spotify).

image


Obtaining Spotify Playlist IDs

  1. Right click on a playlist > "Share" > Copy the Spotify URI (spotify:playlist:c11M5VLWLMh66yW4gsl51S).
  2. The ID is of this playlist is c11M5VLWLMh66yW4gsl51S, use this for the environment variable.

If the URL is different, for example https://open.spotify.com/playlist/37i9dQZF1DWVKDF4ycOESi?si=7f0b77dde6fc4382, the ID is the first part before the ?si=, then 37i9dQZF1DWVKDF4ycOESi (in the case of the example).


Manual Execution via GitHub Actions

  1. Go to Actions in your forked repo.
  2. Click on "Append songs"
  3. Click on "Run workflow" which will bring up a drop down menu.
  4. Click on "Run Workflow" again, this will initiate the script. Within the next few minutes, the script should execute and your songs should be in your new playlist in Spotify.

Any execution errors can be found from within the actions tab of your forked repo.

image

If you want to create a brand new playlist, backup of actual New Music Friday, you have to choose "Backup New Music Friday" instead "Append songs".

Local Execution

Alternatively, you can store the REFRESH_TOKEN, NEW_MUSIC_FRIDAY_ID & SAVE_TO_ID back into your .env file and execute main.py or nmf.py on your machine when required, maybe manually or using a task scheduler. Make sure to have the .env and main.py (or nmf.py) files in the same directory for this.

$python main.py

Next Steps:

  • RSS feed (contains weekly playlist backups) JSON now available!
  • Summary webpage (with all weekly "New Music Friday" direct links)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT

Credits

spotify/web-api#519 (comment)
https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
https://towardsdatascience.com/how-to-download-an-image-using-python-38a75cfa21c
https://stackoverflow.com/a/16129667
https://stackoverflow.com/a/179608
https://stackoverflow.com/a/34843232