Created
September 11, 2020 16:22
-
-
Save kix2902/f2dfbd9e05881c29721197b6f65e12f4 to your computer and use it in GitHub Desktop.
Simple script to sync watched episodes and movies from Trakt.tv to any list of IMDb (it hasn't any API available so the calls are made using captured data from a real request in its website) (I know this is against its TOS but the script is for personal use, not commercial).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$traktApiClientId = "TRAKT-APP-CLIENTID"; // You can create an app in https://trakt.tv/oauth/applications | |
// This fields can be extracted from a real request to add a title to a list | |
$imdbListId = "list-id"; // list where you want to add watched titles, captured from url called with this format: "https://www.imdb.com/list/<<list-id>>/<<title-id>>/add" (list-id is the needed value) | |
$imdbCookie = "cookie"; // request header with name "cookie", it must be copied exactly as sent from the website | |
$imdbPost = "post-data"; // data sent in the body of the request | |
// TRAKT | |
$timestampFilename = "time.txt"; | |
$traktApiUrl = "https://api.trakt.tv"; | |
if (file_exists($timestampFilename)) { | |
$lastSync = file_get_contents($timestampFilename); | |
} else { | |
$lastSync = 0; | |
} | |
// CURL INIT | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_POST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
"Content-Type: application/json", | |
"trakt-api-version: 2", | |
"trakt-api-key: $traktApiClientId", | |
)); | |
// MOVIES | |
curl_setopt($ch, CURLOPT_URL, "$traktApiUrl/users/kix2902/watched/movies"); | |
$response = curl_exec($ch); | |
$data = json_decode($response); | |
foreach ($data as $item) { // MOVIE | |
$updated = strtotime($item->last_updated_at); | |
if ($lastSync > $updated) { | |
continue; | |
} | |
$imdbId = $item->movie->ids->imdb; | |
if (isset($imdbId)) { | |
$imdbItems[] = $imdbId; | |
} | |
} | |
gc_collect_cycles(); | |
// END_MOVIES | |
// SHOWS | |
curl_setopt($ch, CURLOPT_URL, "$traktApiUrl/users/kix2902/watched/shows"); | |
$response = curl_exec($ch); | |
$data = json_decode($response); | |
foreach ($data as $item) { // SHOW | |
$updated = strtotime($item->last_updated_at); | |
if ($lastSync > $updated) { | |
continue; | |
} | |
$showId = $item->show->ids->trakt; | |
if (isset($showId)) { | |
foreach ($item->seasons as $season) { // SEASON | |
$seasonId = $season->number; | |
foreach ($season->episodes as $episode) { // EPISODE | |
$updated = strtotime($episode->last_watched_at); | |
if ($lastSync > $updated) { | |
continue; | |
} | |
$episodeId = $episode->number; | |
curl_setopt($ch, CURLOPT_URL, "$traktApiUrl/shows/$showId/seasons/$seasonId/episodes/$episodeId"); | |
$epResponse = curl_exec($ch); | |
$epData = json_decode($epResponse); | |
$imdbId = $epData->ids->imdb; | |
if (isset($imdbId)) { | |
$imdbItems[] = $imdbId; | |
} | |
} | |
} | |
gc_collect_cycles(); | |
} | |
} | |
file_put_contents($timestampFilename, time()); | |
curl_close($ch); | |
// END_SHOWS | |
if (!isset($imdbItems)) { | |
return; | |
} | |
// IMDB | |
$error = false; | |
$ch = curl_init(); | |
foreach ($imdbItems as $item) { | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
"content-type: application/x-www-form-urlencoded", | |
"cookie: $imdbCookie", | |
)); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $imdbPost); | |
curl_setopt($ch, CURLOPT_URL, "https://www.imdb.com/list/$imdbListId/$item/add"); | |
$response = curl_exec($ch); | |
gc_collect_cycles(); | |
} | |
curl_close($ch); | |
// END_IMDB |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment