Skip to content

Instantly share code, notes, and snippets.

@mayerwin
Forked from darekkay/trakt-backup.php
Created February 11, 2019 02:41
Show Gist options
  • Save mayerwin/b5d3a7e6d5a44204b339c8d9e7b0a1fc to your computer and use it in GitHub Desktop.
Save mayerwin/b5d3a7e6d5a44204b339c8d9e7b0a1fc 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.txt");
loadAndZip("watchlist/shows/", "watchlist_shows.txt");
loadAndZip("watchlist/episodes/", "watchlist_episodes.txt");
loadAndZip("watchlist/seasons/", "watchlist_seasons.txt");
loadAndZip("ratings/movies/", "ratings_movies.txt");
loadAndZip("ratings/shows/", "ratings_shows.txt");
loadAndZip("ratings/episodes/", "ratings_episodes.txt");
loadAndZip("ratings/seasons/", "ratings_seasons.txt");
loadAndZip("collection/movies/", "library_collection_movies.txt");
loadAndZip("collection/shows/", "library_collection_shows.txt");
loadAndZip("watched/movies/", "watched_movies.txt");
loadAndZip("watched/shows/", "watched_shows.txt");
loadAndZip("history/movies/", "history_movies.txt");
loadAndZip("history/shows/", "history_shows.txt");
$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-v2launch.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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment