Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RARBG torrents bookmark feature, like it was on KickAss Torrents KAT.cr . You just need to add the movies to your own watchlist on the IMDb website and you will be automagically notified of new uploads at RARBG torrents.
<?php
error_reporting (E_ALL);
ini_set ("display_errors", true);
$BASE = strtok (basename ($_SERVER["SCRIPT_NAME"], ".php"), "-");
libxml_use_internal_errors (false);
function clear_dates ($str) {
return preg_replace ("/(\<updated\>)[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}\:[0-9]{2}\:[0-9]{2}\+[0-9]{2}\:[0-9]{2}(\<\/updated\>)/m", "\\1\\2", $str);
}
$suffixes = array ("k","M","G","T","P","E","Z","Y");
if (is_readable ("feedproxy.conf")) $conf = parse_ini_file ("feedproxy.conf", true);
if (empty ($_SERVER["argv"][1]) || !preg_match ("/^(?:https?\:\/\/rss\.imdb\.com\/)?(user\/ur[0-9]+\/watchlist|list\/ls[0-9]+)\/?/", $_SERVER["argv"][1], $reg)) die ("FATAL specified imdb list is invalid: {$_SERVER["argv"][1]}\n");
$list = $reg[1];
$url = "http://rss.imdb.com/$list";
set_time_limit (32);
if (!($curl = curl_init ($url)) || curl_errno ($curl)) die ("FATAL curl init #" . curl_errno ($curl) . " " . curl_error ($curl) . "\n");
curl_setopt_array ($curl, array (
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "feedproxy (+http://sourceforge.net/projects/feedproxy/)",
CURLOPT_FAILONERROR => true,
CURLOPT_CONNECTTIMEOUT => 8,
CURLOPT_TIMEOUT => 16,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false,
));
if (!($curlret = curl_exec ($curl)) || curl_errno ($curl)) error_log (date ("Y-m-d H:i:s") . "\tERROR curl exec #" . curl_errno ($curl) . " " . curl_error ($curl) . " @ $url\n", 3, "$BASE.log");
elseif (!($xml = simplexml_load_string ($curlret)) || empty ($xml->channel)) error_log (date ("Y-m-d H:i:s") . "\tERROR xml load @ $url\n", 3, "$BASE.log");
else {
if (!empty ($xml->channel->item)) foreach ($xml->channel->item as $item) {
if (empty ($item->link) || !preg_match ("/^(?:https?\:)?\/\/(?:www\.)?imdb\.com\/title\/tt([0-9]+)\/?/", (string) $item->link, $reg)) error_log (date ("Y-m-d H:i:s") . "\tERROR xml is missing or has invalid item/link @ $url\n", 3, "$BASE.log");
else $imdbs[] = $reg[1];
}
}
if (!empty ($imdbs)) {
if (file_exists ("$BASE.cache") && filesize ("$BASE.cache") && ($cache = json_decode (file_get_contents ("$BASE.cache"), true)) && $cache === false) die ("FATAL cannot load cache json error: #" . json_last_error () . "\n");
$sent = 0;
shuffle ($imdbs);
for ($key = 0; $key < count ($imdbs); $key++) {
if (empty ($token)) {
$url = "http://torrentapi.org/pubapi_v2.php?app_id=hu.moli.feedrarbg&get_token=get_token";
curl_setopt ($curl, CURLOPT_URL, $url);
set_time_limit (32);
if ($sent++) sleep (8);
$curlret = curl_exec ($curl);
$code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
$redir = curl_getinfo ($curl, CURLINFO_REDIRECT_URL);
if (!$curlret || curl_errno ($curl)) {
error_log (date ("Y-m-d H:i:s") . "\tERROR curl exec #" . curl_errno ($curl) . " " . curl_error ($curl) . ($code != 200 ? " http$code" . ($redir ? " ( $redir )" : "") : "") . " @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
exit (1);
}
if (!($json = json_decode ($curlret, true)) || empty ($json["token"])) {
error_log (date ("Y-m-d H:i:s") . "\tERROR api cannot get token @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
exit (1);
}
$token = $json["token"];
}
$imdb = $imdbs[$key];
$url = "http://torrentapi.org/pubapi_v2.php?app_id=hu.moli.feedrarbg&token=$token&mode=search&search_imdb=tt$imdb&ranked=0&sort=last&limit=25&format=json_extended";
curl_setopt ($curl, CURLOPT_URL, $url);
set_time_limit (32);
if ($sent++) sleep (8);
$curlret = curl_exec ($curl);
$code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
$redir = curl_getinfo ($curl, CURLINFO_REDIRECT_URL);
if (!$curlret || curl_errno ($curl)) error_log (date ("Y-m-d H:i:s") . "\tERROR curl exec #" . curl_errno ($curl) . " " . curl_error ($curl) . ($code != 200 ? " http$code" . ($redir ? " ( $redir )" : "") : "") . " @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
elseif (!($json = json_decode ($curlret, true)) || (empty ($json["error_code"]) && empty ($json["torrent_results"]))) error_log (date ("Y-m-d H:i:s") . "\tERROR api returned invalid json @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
elseif (!empty ($json["error_code"]) && $json["error_code"] == 2) {
$token = null;
$key--;
} elseif (!empty ($json["error_code"]) && in_array ($json["error_code"], array (10, 20))) true;
elseif (!empty ($json["error_code"]) || empty ($json["torrent_results"])) error_log (date ("Y-m-d H:i:s") . "\tERROR api cannot get search results @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
else {
foreach ($json["torrent_results"] as $torr) {
$entry = array ("id" => null, "imdb" => $imdb, "updated" => null, "size" => null);
if (!empty ($torr["download"])) {
if (preg_match ("/[\?\&]xt\=urn\:btih\:([0-9a-f]{40})(?:\&|\b)/i", $torr["download"], $reg)) $entry["id"] = strtolower ($reg[1]);
$entry["magnet"] = $torr["download"];
}
if (!empty ($torr["pubdate"]) && ($stamp = strtotime ($torr["pubdate"]))) $entry["updated"] = $stamp;
if (!empty ($torr["size"])) $entry["size"] = $torr["size"];
$entry["title"] = (!empty ($torr["title"]) ? $torr["title"] : "???");
if (empty ($entry["id"])) error_log (date ("Y-m-d H:i:s") . "\tERROR api cannot get torrent uniqueid in search results @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", var_export ($torr, true)) . "\n", 3, "$BASE.log");
else {
foreach ($entry as $field => $value) {
if (empty ($value)) error_log (date ("Y-m-d H:i:s") . "\tERROR api missing field $field in search results @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", var_export ($torr, true)) . "\n", 3, "$BASE.log");
}
if (!empty ($entry["title"]) && preg_match ("/[^a-z0-9]1080p[^a-z0-9]/i", $entry["title"])) continue;
if (!empty ($entry["title"]) && preg_match("/[^a-z0-9]xvid[^a-z0-9]/i", $entry["title"])) continue;
if (!empty ($entry["title"]) && preg_match("/[^a-z0-9]mp3[^a-z0-9]/i", $entry["title"])) continue;
if (!empty ($cache[$entry["id"]]["rarbg"])) $entry["rarbg"] = $cache[$entry["id"]]["rarbg"];
elseif (!empty ($torr["info_page"])) {
$url = $torr["info_page"];
curl_setopt ($curl, CURLOPT_URL, $url);
set_time_limit (32);
if ($sent++) sleep (8);
$curlret = curl_exec ($curl);
$code = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
$redir = curl_getinfo ($curl, CURLINFO_REDIRECT_URL);
if (curl_errno ($curl) || $code != 302 || !preg_match ("/^https?\:\/\/(?:www\.)?rarbg\.to\/torrent\/([^\/]+)/", $redir, $reg) || $curlret) {
if (curl_errno ($curl)) error_log (date ("Y-m-d H:i:s") . "\tERROR curl exec #" . curl_errno ($curl) . " " . curl_error ($curl) . " http$code" . ($redir ? "( $redir )" : "") . " @ $url debug: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) . "\n", 3, "$BASE.log");
error_log (date ("Y-m-d H:i:s") . "\tERROR api cannot get torrent link @ $url debug: http$code" . ($redir ? "( $redir )" : "") . ($curlret ? " body: " . preg_replace ("/[\r\n\t\s ]+/", " ", $curlret) : "") . "\n", 3, "$BASE.log");
} else {
$entry["rarbg"] = $reg[1];
$cache[$entry["id"]]["rarbg"] = $reg[1];
if (!file_put_contents ("$BASE.cache", preg_replace ("/\":\{\"([0-9a-f])/", "\":{\n\"\\1", str_replace ("},\"", "},\n\"", preg_replace ("/\{/", "{\n", json_encode ($cache), 1))), LOCK_EX)) die ("FATAL cannot write cache\n");
}
}
$entries[$entry["id"]] = $entry;
}
}
}
}
}
if (!empty ($entries)) {
ksort ($entries);
$filename = "$BASE-" . sha1 ($list) . ".atom";
$feedpath = (!empty ($conf["base"]["url"]) ? $conf["base"]["url"] : "") . $filename;
$basepath = (!empty ($conf["base"]["url"]) ? $conf["base"]["url"] : "") . "$BASE.atom";
$now = date ("c");
$feedcont = <<<HEREDOC
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
<id>$feedpath</id>
<updated>$now</updated>
<title type="text">IMDb+RARBG</title>
<link rel="related" href="http://www.imdb.com/$list" type="text/html" title="IMDb list" hreflang="en"/>
<link rel="related" href="https://rarbg.to/torrents.php" type="text/html" title="RARBG torrents" hreflang="en"/>
<link rel="self" href="$feedpath" type="application/atom+xml" title="IMDb+RARBG" hreflang="en"/>
<author>
<name>moli</name>
<uri>http://moli.hu</uri>
</author>
HEREDOC;
foreach ($entries as $entry) {
if (!empty ($entry["size"])) {
$sizelen = floor ((strlen ($entry["size"]) - 1) / 3);
$suffix = (!empty ($suffixes[$sizelen - 1]) ? $suffixes[$sizelen - 1] : null);
$entry["size"] = sprintf ("%.1f", $entry["size"] / pow (1000, $sizelen)) . " " . (!empty ($suffix) ? $suffix : "") . "B";
}
$feedcont .= " <entry>\n";
$feedcont .= " <id>$basepath?{$entry["id"]}</id>\n";
$feedcont .= " <updated>" . date ("c", (!empty ($entry["updated"]) ? $entry["updated"] : 1)) . "</updated>\n";
if (!empty ($entry["rarbg"])) $feedcont .= " <link rel=\"alternate\" href=\"https://rarbg.to/torrent/" . htmlspecialchars ($entry["rarbg"], ENT_QUOTES, "UTF-8") . "\" type=\"text/html\" hreflang=\"en\"/>\n";
$feedcont .= " <link rel=\"related\" href=\"http://www.imdb.com/title/tt{$entry["imdb"]}/\" type=\"text/html\" hreflang=\"en\"/>\n";
$feedcont .= " <title type=\"text\">" . (!empty ($entry["title"]) ? htmlspecialchars ($entry["title"], ENT_QUOTES, "UTF-8") : "???") . "</title>\n";
$feedcont .= " <content type=\"html\">" . htmlspecialchars ((!empty ($entry["size"]) ? "{$entry["size"]} " : "") . "<a href=\"" . htmlspecialchars ($entry["magnet"], ENT_QUOTES, "UTF-8") . "\">magnet</a> <a href=\"http://www.imdb.com/title/tt{$entry["imdb"]}\">imdb</a>" . (!empty ($entry["rarbg"]) ? " <a href=\"https://rarbg.to/torrent/" . htmlspecialchars ($entry["rarbg"], ENT_QUOTES, "UTF-8") . "\">rarbg</a>" : ""), ENT_QUOTES, "UTF-8") . "</content>\n";
$feedcont .= " </entry>\n";
}
$feedcont .= <<<HEREDOC
</feed>
HEREDOC;
if (file_exists ($filename) && filesize ($filename) && ($oldcont = file_get_contents ($filename)) && $oldcont === false) die ("FATAL cannot read old feed\n");
if (empty ($oldcont) || clear_dates ($feedcont) != clear_dates ($oldcont)) {
if (!file_put_contents ($filename, $feedcont)) die ("FATAL cannot write new feed\n");
}
}
?>
Owner
phpmoli commented Jul 25, 2016

You need to make your IMDb list public and call the script using the list name as parameter like: php feedrarbg.php user/ur9028759/watchlist (i dont know whose it is, it is just a google feeling lucky result) or php feedrarbg.php list/ls000031161 for created lists.

Hi!
I don't really understand what your script do but can i make custom RSS feed with TV series i want?
Example: "Preacher" "Stranger Things"

Owner
phpmoli commented Jul 28, 2016 edited

Hello @misakitchi,
Sorry for the late reply, but github doesnt seem to send notification about comments on your gists, so i noted your comment by chance.

Yes, you can make a custom rss feed with the movies or tv series you want, in fact all your watchlisted imdb movies and tv shows will automatically appear. Just add "preacher" to your watchlist and it works like it would make a search on rarbg, example: https://rarbg.to/torrents.php?page=1&order=data&by=DESC&imdb=tt5016504 and give you the torrents there in rss.

While this script is intended for advanced audience, who knows how to set up a private webserver at home (e.g raspberry pi) or who has an access to a server in a hosting company, but you can in fact run this script on even your home windows desktop if your rss feed reader client supports local (file://) feeds (but i guess not many do) than you can run the script without a webserver, just with command line php, it should be working that way.

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