/trakt-backup-gas

Export Trakt user data, using Google Apps Script

Primary LanguageJavaScript

Trakt Backup Script

Export Trakt user data, using Google Apps Script.

This script can be used to automatically export user data and stats from Trakt. This includes movie & TV watchlists, comments, ratings, recommendations, custom lists, and watch history. 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 backupCore() function to export most data types, as well as backupLists() to export all custom lists, both of which can be run directly. To export all user data at once, simply run the main() function.

Setup

There are four steps necessary to run this script.

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

1. Register a Trakt API app

To access the Trakt API, each application needs to be registered with Trakt. This can be done from the 'Your API Apps' page in your Trakt settings.

To register an app, two values will need to be specified (everything else is optional):

  • Name: Trakt GAS Script, or whatever you want
  • Redirect uri: urn:ietf:wg:oauth:2.0:oob as the script uses "Device authentication"

Once registered, Trakt will display the details needed for the config file.

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 Trakt 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 = {
    "username": "<Trakt username>",
    "clientId": "<Trakt client ID>",
    "clientSecret": "<Trakt client secret>",
    "backupDir": "<Google Drive directory ID>",
    "removeMissingLists": <true/false>
};
  • username: Name of the Trakt user whose data you want to export.
  • clientId & clientSecret: An identifier & key for accessing the Trakt API. They can be found on the page for the app you created in step 1.
  • 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.
  • removeMissingLists: This option will remove backed up list files if they do not match a current list.

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 it 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 Trakt API

For the script to be able to access user data, it first needs to be authorised. This should only need to be done once, unless the user or Trakt details in the config file change. This involves inputting a unique code, generated by the script, into a Trakt page while logged in as the desired user.

The script can be authorised by running any of the data-retrieval functions, including main(). Alternatively, if you don't want to retrieve any data, retrieveAuth() can be run instead. If the authorisation needs to be reset, such as if some config has changed, use resetAuth() to delete the existing credentials and re-authorise the script.

In all cases, if the script cannot detect usable credentials, it will print a new unique code to the log, along with the URL of the page where it must be entered. The URL is usually https://trakt.tv/activate/ although this may be subject to change.

The user has about 5 minutes to enter the code before it expires, although Google App Script also has a runtime timeout which will usually expire first.

Once the code has been entered, the script should automatically detect that it is now authorised, and will be able to run without intervention.

Credits

Inspired by mstarzinger/trakt-backup.