Skip to content

Instantly share code, notes, and snippets.

@AndyScherzinger
Forked from darekkay/trakt-backup.php
Last active July 2, 2019 19:26
Show Gist options
  • Save AndyScherzinger/754e1ba6268258bdc758 to your computer and use it in GitHub Desktop.
Save AndyScherzinger/754e1ba6268258bdc758 to your computer and use it in GitHub Desktop.
<?php
/*
Backup script for trakt.tv.
Live demo: http://eclectide.com/blog/2014/08/12/trakt-tv-backup/
*/
// You can find your personal api key under:
// https://trakt.tv/api-docs/authentication
$apikey = "YOUR_API_KEY";
$username = "YOUR_USERNAME";
$zip = new ZipArchive();
$zip_filename = "trakt_backup_" . date("Y-m-d") . ".zip";
$zip_filepath = "./trakt_backup_" . date("Y-m-d") . ".zip";
if ($zip->open($zip_filepath, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open <$zip_filepath>\n");
}
loadAndZip("watchlist/movies.json/", "watchlist_movies.txt");
loadAndZip("watchlist/shows.json/", "watchlist_shows.txt");
loadAndZip("watchlist/episodes.json/", "watchlist_episodes.txt");
loadAndZip("ratings/movies.json/", "ratings_movies.txt");
loadAndZip("ratings/shows.json/", "ratings_shows.txt");
loadAndZip("ratings/episodes.json/", "ratings_episodes.txt");
loadAndZip("library/movies/collection.json/", "library_collection_movies.txt");
loadAndZip("library/shows/collection.json/", "library_collection_shows.txt");
loadAndZip("library/movies/watched.json/", "library_watched_movies.txt");
loadAndZip("library/shows/watched.json/", "library_watched_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 = "http://api.trakt.tv/user/" . $path . $apikey . "/" . $username;
$json = file_get_contents($url);
if (strpos($http_response_header[0], "401")) {
exit("Wrong apikey!");
}
if (strpos($http_response_header[0], "404")) {
exit("Wrong username!");
}
$zip->addFromString($filename, $json);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment