Skip to content

Instantly share code, notes, and snippets.

@darekkay
Last active April 18, 2024 01:08
Show Gist options
  • Star 79 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • 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);
}
Copy link

ghost commented Jul 24, 2021

Thank you so much for this! I had a quick question though. Is it possible to list all watch dates? I noticed that if you watch something multiple times it only has the last watched at field.

@darekkay
Copy link
Author

Hey @PAQuestions, sorry, I cannot answer this question. This script downloads only what Trakt API exposes.

@stefan2904
Copy link

@holybladder
Copy link

Hi Darek, thank you so much for this script! It's now started downloading empty zip files as of about a week ago; can't see a verbose option so no information as to what might have gone wrong, any thoughts?

@joserbatista
Copy link

Hi Darek, thank you so much for this script! It's now started downloading empty zip files as of about a week ago; can't see a verbose option so no information as to what might have gone wrong, any thoughts?

Trakt API URL changed to https://api.trakt.tv/

@darekkay
Copy link
Author

darekkay commented Feb 8, 2024

Thanks @holybladder for reporting the issue and @joserbatista for the fix! I've updated the gist and also the live demo on my blog.

@holybladder
Copy link

Thank you @darekkay for the fix! Working nicely again now.

@micmalti
Copy link

micmalti commented Mar 6, 2024

Thank you @darekkay for writing this gist. May I ask why you're choosing to save the JSON output in .txt format instead of .json?

@darekkay
Copy link
Author

darekkay commented Mar 6, 2024

@micmalti No reason, updated to .json!

@holybladder
Copy link

holybladder commented Apr 8, 2024

EDIT: Sorry, never mind, error on my end!

Hi @darekkay, this issue seems to have surfaced again?

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