add try/catch blocks inside all torrent engines #2

Merged
lamacchinadesiderante merged 1 commits from feature/add-try-catch-blocks-to-torrent-engines into master 2023-04-11 19:50:50 +00:00
6 changed files with 109 additions and 88 deletions

View File

@ -3,32 +3,37 @@
function get_1337x_results($response) function get_1337x_results($response)
{ {
global $config; try {
$xpath = get_xpath($response); global $config;
$results = array(); $xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tbody/tr") as $result) foreach($xpath->query("//table/tbody/tr") as $result)
{ {
$name = $xpath->evaluate(".//td[@class='coll-1 name']/a", $result)[1]->textContent; $name = $xpath->evaluate(".//td[@class='coll-1 name']/a", $result)[1]->textContent;
$magnet = "/engines/bittorrent/get_magnet_1337x.php?url=https://1337x.to" . $xpath->evaluate(".//td[@class='coll-1 name']/a/@href", $result)[1]->textContent; $magnet = "/engines/bittorrent/get_magnet_1337x.php?url=https://1337x.to" . $xpath->evaluate(".//td[@class='coll-1 name']/a/@href", $result)[1]->textContent;
$size_unformatted = explode(" ", $xpath->evaluate(".//td[contains(@class, 'coll-4 size')]", $result)[0]->textContent); $size_unformatted = explode(" ", $xpath->evaluate(".//td[contains(@class, 'coll-4 size')]", $result)[0]->textContent);
$size = $size_unformatted[0] . " " . preg_replace("/[0-9]+/", "", $size_unformatted[1]); $size = $size_unformatted[0] . " " . preg_replace("/[0-9]+/", "", $size_unformatted[1]);
$seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent; $seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent; $leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent;
array_push($results, array_push($results,
array ( array (
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => (int) $seeders, "seeders" => (int) $seeders,
"leechers" => (int) $leechers, "leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet), "magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size), "size" => htmlspecialchars($size),
"source" => "1337x.tw" "source" => "1337x.tw"
) )
); );
}
return $results;
} catch (Error $e) {
return array();
} }
return $results;
} }
?> ?>

View File

@ -1,25 +1,26 @@
<?php <?php
$nyaa_url = "https://nyaa.si/?q=$query"; $nyaa_url = "https://nyaa.si/?q=$query";
function get_nyaa_results($response) function get_nyaa_results($response)
{ {
try {
global $config; global $config;
$xpath = get_xpath($response); $xpath = get_xpath($response);
$results = array(); $results = array();
foreach($xpath->query("//tbody/tr") as $result) foreach ($xpath->query("//tbody/tr") as $result) {
{
$name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent; $name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent;
$centered = $xpath->evaluate(".//td[@class='text-center']", $result); $centered = $xpath->evaluate(".//td[@class='text-center']", $result);
$magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent; $magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0]; $magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers; $magnet = $magnet_without_tracker . $config->bittorent_trackers;
$size = $centered[1]->textContent; $size = $centered[1]->textContent;
$seeders = $centered[3]->textContent; $seeders = $centered[3]->textContent;
$leechers = $centered[4]->textContent; $leechers = $centered[4]->textContent;
array_push($results, array_push(
array ( $results,
array(
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => (int) $seeders, "seeders" => (int) $seeders,
"leechers" => (int) $leechers, "leechers" => (int) $leechers,
@ -31,5 +32,8 @@
} }
return $results; return $results;
} catch (Error $e) {
return array();
} }
}
?> ?>

View File

@ -1,26 +1,27 @@
<?php <?php
$rutor_url = "http://rutor.info/search/$query"; $rutor_url = "http://rutor.info/search/$query";
function get_rutor_results($response) function get_rutor_results($response)
{ {
try {
global $config; global $config;
$xpath = get_xpath($response); $xpath = get_xpath($response);
$results = array(); $results = array();
foreach($xpath->query("//table/tr[@class='gai' or @class='tum']") as $result) foreach ($xpath->query("//table/tr[@class='gai' or @class='tum']") as $result) {
{
$name = $xpath->evaluate(".//td/a", $result)[2]->textContent; $name = $xpath->evaluate(".//td/a", $result)[2]->textContent;
$magnet = $xpath->evaluate(".//td/a/@href", $result)[1]->textContent; $magnet = $xpath->evaluate(".//td/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0]; $magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers; $magnet = $magnet_without_tracker . $config->bittorent_trackers;
$size = $xpath->evaluate(".//td", $result)[3]->textContent; $size = $xpath->evaluate(".//td", $result)[3]->textContent;
$seeders = $xpath->evaluate(".//span", $result)[0]->textContent; $seeders = $xpath->evaluate(".//span", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//span", $result)[1]->textContent; $leechers = $xpath->evaluate(".//span", $result)[1]->textContent;
array_push($results, array_push(
array ( $results,
array(
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => (int) remove_special($seeders), "seeders" => (int) remove_special($seeders),
"leechers" => (int) remove_special($leechers), "leechers" => (int) remove_special($leechers),
@ -32,5 +33,9 @@
} }
return $results; return $results;
} catch (Error $e) {
return array();
} }
}
?> ?>

View File

@ -1,25 +1,26 @@
<?php <?php
$sukebei_url = "https://sukebei.nyaa.si/?q=$query"; $sukebei_url = "https://sukebei.nyaa.si/?q=$query";
function get_sukebei_results($response) function get_sukebei_results($response)
{ {
try {
global $config; global $config;
$xpath = get_xpath($response); $xpath = get_xpath($response);
$results = array(); $results = array();
foreach($xpath->query("//tbody/tr") as $result) foreach ($xpath->query("//tbody/tr") as $result) {
{
$name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent; $name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent;
$centered = $xpath->evaluate(".//td[@class='text-center']", $result); $centered = $xpath->evaluate(".//td[@class='text-center']", $result);
$magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent; $magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0]; $magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers; $magnet = $magnet_without_tracker . $config->bittorent_trackers;
$size = $centered[1]->textContent; $size = $centered[1]->textContent;
$seeders = $centered[3]->textContent; $seeders = $centered[3]->textContent;
$leechers = $centered[4]->textContent; $leechers = $centered[4]->textContent;
array_push($results, array_push(
array ( $results,
array(
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => (int) $seeders, "seeders" => (int) $seeders,
"leechers" => (int) $leechers, "leechers" => (int) $leechers,
@ -31,5 +32,8 @@
} }
return $results; return $results;
} catch (Error $e) {
return array();
} }
}
?> ?>

View File

@ -1,20 +1,19 @@
<?php <?php
$thepiratebay_url = "https://apibay.org/q.php?q=$query"; $thepiratebay_url = "https://apibay.org/q.php?q=$query";
function get_thepiratebay_results($response) function get_thepiratebay_results($response)
{ {
try {
global $config; global $config;
$results = array(); $results = array();
$json_response = json_decode($response, true); $json_response = json_decode($response, true);
if (empty($json_response)) if (empty($json_response)) {
{
return $results; return $results;
} }
foreach ($json_response as $response) foreach ($json_response as $response) {
{
$size = human_filesize($response["size"]); $size = human_filesize($response["size"]);
$hash = $response["info_hash"]; $hash = $response["info_hash"];
$name = $response["name"]; $name = $response["name"];
@ -26,8 +25,9 @@
if ($name == "No results returned") if ($name == "No results returned")
break; break;
array_push($results, array_push(
array ( $results,
array(
"size" => htmlspecialchars($size), "size" => htmlspecialchars($size),
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => (int) htmlspecialchars($seeders), "seeders" => (int) htmlspecialchars($seeders),
@ -39,6 +39,8 @@
} }
return $results; return $results;
} catch (Error $e) {
return array();
} }
}
?> ?>

View File

@ -1,31 +1,30 @@
<?php <?php
$yts_url = "https://yts.mx/api/v2/list_movies.json?query_term=$query"; $yts_url = "https://yts.mx/api/v2/list_movies.json?query_term=$query";
function get_yts_results($response) function get_yts_results($response)
{ {
try {
global $config; global $config;
$results = array(); $results = array();
$json_response = json_decode($response, true); $json_response = json_decode($response, true);
if ($json_response["status"] == "ok" && $json_response["data"]["movie_count"] != 0) if ($json_response["status"] == "ok" && $json_response["data"]["movie_count"] != 0) {
{ foreach ($json_response["data"]["movies"] as $movie) {
foreach ($json_response["data"]["movies"] as $movie) $name = $movie["title"];
{ $name_encoded = urlencode($name);
$name = $movie["title"];
$name_encoded = urlencode($name);
foreach ($movie["torrents"] as $torrent) foreach ($movie["torrents"] as $torrent) {
{
$hash = $torrent["hash"]; $hash = $torrent["hash"];
$seeders = $torrent["seeds"]; $seeders = $torrent["seeds"];
$leechers = $torrent["peers"]; $leechers = $torrent["peers"];
$size = $torrent["size"]; $size = $torrent["size"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config->bittorent_trackers"; $magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config->bittorent_trackers";
array_push($results, array_push(
array ( $results,
array(
"size" => htmlspecialchars($size), "size" => htmlspecialchars($size),
"name" => htmlspecialchars($name), "name" => htmlspecialchars($name),
"seeders" => htmlspecialchars($seeders), "seeders" => htmlspecialchars($seeders),
@ -35,11 +34,13 @@
) )
); );
} }
} }
} }
return $results; return $results;
} catch (Error $e) {
return array();
} }
}
?> ?>