Skip to content

Instantly share code, notes, and snippets.

@darekkay
Last active March 18, 2025 03:55
Show Gist options
  • Save darekkay/ff1c5aadf31588f11078 to your computer and use it in GitHub Desktop.
Save darekkay/ff1c5aadf31588f11078 to your computer and use it in GitHub Desktop.
Trakt.tv backup script
<?php
/*
Backup script for trakt.tv (API v2).
Live demo: https://darekkay.com/blog/trakt-tv-backup/
*/
// create a Trakt app to get a client API key: http://docs.trakt.apiary.io/#introduction/create-an-app
$apikey = "CLIENT_API_KEY";
$username = "YOUR_USERNAME";
$zip = new ZipArchive();
$zip_filename = "trakt_backup_" . date("Y-m-d") . ".zip";
$zip_filepath = "/tmp/trakt_backup_" . date("Y-m-d") . ".zip";
if ($zip->open($zip_filepath, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open <$zip_filepath>\n");
}
loadAndZip("watchlist/movies/", "watchlist_movies.json");
loadAndZip("watchlist/shows/", "watchlist_shows.json");
loadAndZip("watchlist/episodes/", "watchlist_episodes.json");
loadAndZip("watchlist/seasons/", "watchlist_seasons.json");
loadAndZip("ratings/movies/", "ratings_movies.json");
loadAndZip("ratings/shows/", "ratings_shows.json");
loadAndZip("ratings/episodes/", "ratings_episodes.json");
loadAndZip("ratings/seasons/", "ratings_seasons.json");
loadAndZip("collection/movies/", "library_collection_movies.json");
loadAndZip("collection/shows/", "library_collection_shows.json");
loadAndZip("watched/movies/", "watched_movies.json");
loadAndZip("watched/shows/", "watched_shows.json");
loadAndZip("history/movies/", "history_movies.json");
loadAndZip("history/shows/", "history_shows.json");
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$zip_filename");
header("Pragma: no-cache");
header("Expires: 0");
readfile($zip_filepath);
exit();
function loadAndZip($path, $filename)
{
global $zip, $apikey, $username;
$url = "https://api.trakt.tv/users/" . $username . '/' . $path ;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"trakt-api-key: " . $apikey,
"trakt-api-version: 2"),
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
));
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
exit("<h3>Wrong username!</h3>");
}
curl_close($ch);
$zip->addFromString($filename, $result);
}
@Mageas
Copy link

Mageas commented Jan 7, 2025

Thanks so much ;)

@muhd-kifayath
Copy link

The history only extracts past 10 days data. Is there any way to get the whole data?

@rezpower
Copy link

I am trying to get my trakt data by using the web page as I have no idea how to use this script
https://darekkay.com/blog/trakt-tv-backup/
Unfortunately it creates a zip file with 0 byte json files! Can someone please help me with this?

@micmalti
Copy link

@rezpower that's strange. I just tried the web page route and it worked, as per usual. It was only when I tried to run the script locally that it created the zip file with 0 byte json files.

@rezpower
Copy link

@micmalti Thanks a lot for replying. By confirming that it worked for you I started trying again to see why it's not working for me and I finally figured out that it was because of my username. My username on Trakt was my e-mail address with a dot in the middle. ([email protected]). I changed it from the email format and only kept myname.s , but trakt changes the URL from . to -
So to fix the problem I entered myname-s in the website and I was able to get it work.
Mentioned all this in case someone else faces the same problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment