Skip to content

Instantly share code, notes, and snippets.

@insign
Created November 7, 2015 05:08
Show Gist options
  • Save insign/84c0b270e43fcd807194 to your computer and use it in GitHub Desktop.
Save insign/84c0b270e43fcd807194 to your computer and use it in GitHub Desktop.
Sync IMDb watchlist to trakt.tv watchlist. Usage: php -f watchlist.php trakt_apikey trakt_username trakt_passwordhash imdb_login imdb_password imdb_uid (IMDb login is email adress or nickname, it's NOT the ur12345678 bit, that's the imdb_uid)
<?php
define('TRAKT_APIKEY', $argv[1]);
define('TRAKT_USERNAME', $argv[2]);
define('TRAKT_PASSWORD', $argv[3]);
define('IMDB_EMAIL', $argv[4]);
define('IMDB_PASSWORD', $argv[5]);
define('IMDB_UID', $argv[6]);
function curl_post($urlpre, $data, $urlpost = '')
{
$data->username = TRAKT_USERNAME;
$data->password = TRAKT_PASSWORD;
$data = json_encode($data);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $urlpre . TRAKT_APIKEY . $urlpost,
CURLOPT_POSTFIELDS => $data,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 0
)
);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
function curl_get($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 0
)
);
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
function csv_to_array($input, $delimiter=';')
{
$header = null;
$data = array();
$csvData = str_getcsv($input, "\n");
foreach($csvData as $csvLine){
if(is_null($header)) $header = explode($delimiter, $csvLine);
else{
$items = explode($delimiter, $csvLine);
for($n = 0, $m = count($header); $n < $m; $n++){
$prepareData[$header[$n]] = $items[$n];
}
$data[] = $prepareData;
}
}
return $data;
}
$lecookie="./lecookiep";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_COOKIEJAR, $lecookie);
curl_setopt ($ch, CURLOPT_COOKIEFILE, $lecookie);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
curl_setopt($ch, CURLOPT_URL, 'https://secure.imdb.com/register-imdb/login');
$result = curl_exec($ch);
preg_match('/<input type="hidden" name="([^"]*)" value="([^"]*)" \/>/', $result, $match);
$postdata = $match[1] . "=" . $match[2] . "&login=" . urlencode(IMDB_EMAIL) . "&password=" . urlencode(IMDB_PASSWORD);
curl_setopt($ch, CURLOPT_URL, 'https://secure.imdb.com/register-imdb/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_REFERER, 'https://secure.imdb.com/register-imdb/login');
$resulttemp = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://www.imdb.com/list/watchlist');
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'http://www.imdb.com/list/export?list_id=watchlist&author_id=' . urlencode(IMDB_UID));
$result2 = curl_exec($ch);
curl_close($ch);
$watchlist_imdb_t = csv_to_array($result2, ',');
$watchlist_trakt_t = json_decode(curl_post('http://api.trakt.tv/user/watchlist/movies.json/', new stdClass(), '/' . TRAKT_USERNAME));
$watchlist_trakt = array();
$watchlist_imdb = array();
foreach ($watchlist_trakt_t as $currenta)
{
$watchlist_trakt[] = $currenta->imdb_id;
}
foreach ($watchlist_imdb_t as $currentb)
{
$watchlist_imdb[] = str_replace('"', '', $currentb['"const"']);
}
$to_remove = array_diff($watchlist_trakt, $watchlist_imdb);
$to_add = array_diff($watchlist_imdb, $watchlist_trakt);
$to_remove2 = array();
$to_add2 = array();
foreach ($to_remove as $currentx)
{
$temp = new stdClass();
$temp->imdb_id = $currentx;
$to_remove2[] = $temp;
unset($temp);
}
foreach ($to_add as $currenty)
{
$temp = new stdClass();
$temp->imdb_id = $currenty;
$to_add2[] = $temp;
unset($temp);
}
if(count($to_remove2) > 0)
{
if(count($to_remove2) < 10)
{
$datar = new stdClass();
$datar->movies = $to_remove2;
echo 'Removing ' . count($to_remove2) . ' movies from trakt-watchlist:' . "\n" . print_r($to_remove2) . "\n" . curl_post('http://api.trakt.tv/movie/unwatchlist/', $datar) . "\n\n";
}
else
{
echo 'Would remove ' . count($to_remove2) . ' movies from trakt watchlist, seems too much, skipping.';
}
}
if(count($to_add2) > 0)
{
if(count($to_add2) < 30)
{
$dataa = new stdClass();
$dataa->movies = $to_add2;
echo 'Adding ' . count($to_add2) . ' movies to trakt-watchlist:' . "\n" . print_r($to_add2) . "\n" . curl_post('http://api.trakt.tv/movie/watchlist/', $dataa) . "\n\n";
}
else
{
echo 'Would add ' . count($to_add2) . ' movies from IMDb watchlist to trakt, seems too much, skipping.';
}
}
?>
@binvius
Copy link

binvius commented Jun 22, 2018

Hey dude, is this still working since IMDB locked everything down?

Cheers pal!

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