Skip to content

Nitemice/spotify-backup-gas

Repository files navigation

Spotify Backup Script

Export Spotify data, using Google Apps Script.

This script can be used to automatically export a user's data, particularly playlists, from Spotify. They are stored in a specified Google Drive directory, where they can be easily downloaded or shared.

Usage

This script is designed to be run on-demand via the GAS interface, or periodically via GAS triggers. For more info on setting up GAS triggers, see this Google Apps Script guide.

The script includes a backupLibrary() function to export library data, such as saved tracks and followed artists, as well as backupPlaylists() to export all playlists, both of which can be run directly. To export all data at once, simply run the main() function.

Setup

There are four steps necessary to run this script.

  1. Register a Spotify API app
  2. Customize your config file
  3. Load the script into a new Google Apps Script project
  4. Authorise the script with the Spotify API

1. Register a Spotify API app

To access the Spotify API, each application needs to be registered with Spotify. This can be done from the 'Spotify Developer Dashboard' page in your Spotify settings.

To register an app, a name & description needs to be specified. Once registered, the client ID & client secret can be collected from the app page, for the config file.

For more information, see Spotify's Development Guide.

2. Customize your config file

config.js should contain a single JavaScript object, used to specify all necessary configuration information. Here's where you specify the user, the Spotify client ID & client secret for accessing the API, as well as the Google Drive directory to save the exported files to.

An example version is provided, named example.config.js, which can be renamed or copied to config.js before loading into the GAS project.

The basic structure can be seen below.

const config = {
    "clientId": "<Spotify client ID>",
    "clientSecret": "<Spotify client secret>",
    "outputFormat":  ["raw", "csv", "xspf"],
    "backupDir": "<Google Drive directory ID>",
    "removeMissingPlaylists": <true/false>
};
  • clientId & clientSecret: An identifier & key for accessing the Spotify API. They can be found on the page for the app you created in step 1.
  • outputFormat: An array indicating the desired output format(s). Valid values are:
    • raw - Raw, (mostly) unedited JSON file, direct from the API. This is useful if you want to capture some details that are not preserved by the other formats.
    • csv - A comma-separate text file, containing a minimal set of details for each category of backup.
    • xspf - An XML/XSPF playlist file, containing details about each track, as well as the overall playlist. Only relevant for playlist backup.
  • backupDir: The ID of the Google Drive directory, where exported data should be stored. This can be found by navigating to the folder, and grabbing the ID from the tail of the URL.
  • removeMissingPlaylists: This option will remove backed up playlist files if they do not match a current playlist, e.g. name changed, or deleted.

3. Load the script into a new Google Apps Script project

You can manually load the script into a new GAS project, by simply copying and pasting each of the files into the editor.

Or you can use a tool like clasp to upload it directly. For more information on using clasp, here is a guide I found useful.

4. Authorise the script with the Spotify API

For the script to be able to access your data, it first needs to be authorised. This should only need to be done once, unless the user or Spotify details in the config file change. This involves logging into Spotify as the desired user, via a URL generated by the script.

The script can be authorised by deploying it as a web app, and navigating to the deployed URL. For more information on how to do this, see this Google Apps Script guide.

On the page, you will find instructions on adding the web app's address as a redirect_uri to the Spotify app, and authorising the script with Spotify.

Once this is done, the script can be un-deployed (if desired), and all data retrieval functions should work. If the script is run and cannot detect usable credentials, it will throw an error.

Credits

Inspired by simonbru/spotify-backup.