use engine request for torrent search

This commit is contained in:
davidovski 2023-08-22 13:25:44 +01:00
parent 0bd726d897
commit 4941a6aa58
9 changed files with 268 additions and 308 deletions

View File

@ -1,34 +1,39 @@
<?php <?php
$_1337x_url = "https://1337x.to/search/$query/1/"; class _1337xRequest extends EngineRequest {
public function get_request_url() {
function get_1337x_results($response) $query = urlencode($this->query);
{ return "https://1337x.to/search/$query/1/";
global $config;
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tbody/tr") as $result)
{
$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;
$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]);
$seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "1337x.to"
)
);
} }
return $results; public function get_results() {
$response = curl_multi_getcontent($this->ch);
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tbody/tr") as $result)
{
$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;
$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]);
$seeders = $xpath->evaluate(".//td[@class='coll-2 seeds']", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//td[@class='coll-3 leeches']", $result)[0]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "1337x.to"
)
);
}
return $results;
}
} }
?> ?>

View File

@ -4,68 +4,32 @@
$this->query = $query; $this->query = $query;
$this->page = $page; $this->page = $page;
// TODO make these engine requests
require "engines/bittorrent/thepiratebay.php"; require "engines/bittorrent/thepiratebay.php";
require "engines/bittorrent/rutor.php"; require "engines/bittorrent/rutor.php";
require "engines/bittorrent/nyaa.php";
require "engines/bittorrent/yts.php"; require "engines/bittorrent/yts.php";
require "engines/bittorrent/torrentgalaxy.php"; require "engines/bittorrent/torrentgalaxy.php";
require "engines/bittorrent/1337x.php"; require "engines/bittorrent/1337x.php";
require "engines/bittorrent/sukebei.php"; require "engines/bittorrent/sukebei.php";
$query = urlencode($query); $this->requests = array(
new PirateBayRequest($query, $page, $mh, $config),
$torrent_urls = array( new _1337xRequest($query, $page, $mh, $config),
$thepiratebay_url, new NyaaRequest($query, $page, $mh, $config),
$rutor_url, new RutorRequest($query, $page, $mh, $config),
$nyaa_url, new SukebeiRequest($query, $page, $mh, $config),
$yts_url, new TorrentGalaxyRequest($query, $page, $mh, $config),
$torrentgalaxy_url, new YTSRequest($query, $page, $mh, $config),
$_1337x_url,
$sukebei_url
); );
$this->chs = array();
foreach ($torrent_urls as $url)
{
$ch = curl_init($url);
curl_setopt_array($ch, $config->curl_settings);
array_push($this->chs, $ch);
curl_multi_add_handle($mh, $ch);
}
} }
public function get_results() { public function get_results() {
$query = urlencode($this->query); $query = urlencode($this->query);
$results = array(); $results = array();
for ($i=0; count($this->chs)>$i; $i++) foreach ($this->requests as $request) {
{ error_log($request->get_request_url());
$response = curl_multi_getcontent($this->chs[$i]); error_log( curl_getinfo($request->ch)['http_code'] );
if ($request->successful()) {
switch ($i) $results = array_merge($results, $request->get_results());
{
case 0:
$results = array_merge($results, get_thepiratebay_results($response));
break;
case 1:
$results = array_merge($results, get_rutor_results($response));
break;
case 2:
$results = array_merge($results, get_nyaa_results($response));
break;
case 3:
$results = array_merge($results, get_yts_results($response));
break;
case 4:
//$results = array_merge($results, get_torrentgalaxy_results($response));
break;
case 5:
//$results = array_merge($results, get_1337x_results($response));
break;
case 6:
$results = array_merge($results, get_sukebei_results($response));
break;
} }
} }
@ -78,30 +42,29 @@
public static function print_results($results) { public static function print_results($results) {
echo "<div class=\"text-result-container\">"; echo "<div class=\"text-result-container\">";
if (!empty($results)) if (empty($results)) {
{
foreach($results as $result)
{
$source = $result["source"];
$name = $result["name"];
$magnet = $result["magnet"];
$seeders = $result["seeders"];
$leechers = $result["leechers"];
$size = $result["size"];
echo "<div class=\"text-result-wrapper\">";
echo "<a href=\"$magnet\">";
echo "$source";
echo "<h2>$name</h2>";
echo "</a>";
echo "<span>SE: <span class=\"seeders\">$seeders</span> - ";
echo "LE: <span class=\"leechers\">$leechers</span> - ";
echo "$size</span>";
echo "</div>";
}
}
else
echo "<p>There are no results. Please try different keywords!</p>"; echo "<p>There are no results. Please try different keywords!</p>";
return;
}
foreach($results as $result) {
$source = $result["source"];
$name = $result["name"];
$magnet = $result["magnet"];
$seeders = $result["seeders"];
$leechers = $result["leechers"];
$size = $result["size"];
echo "<div class=\"text-result-wrapper\">";
echo "<a href=\"$magnet\">";
echo "$source";
echo "<h2>$name</h2>";
echo "</a>";
echo "<span>SE: <span class=\"seeders\">$seeders</span> - ";
echo "LE: <span class=\"leechers\">$leechers</span> - ";
echo "$size</span>";
echo "</div>";
}
echo "</div>"; echo "</div>";
} }

View File

@ -1,35 +1,50 @@
<?php <?php
$nyaa_url = "https://nyaa.si/?q=$query"; class NyaaRequest extends EngineRequest {
public $SOURCE = "nyaa.si";
function get_nyaa_results($response) public function get_request_url() {
{ return "https://$this->SOURCE/?q=" . urlencode($this->query);
global $config;
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//tbody/tr") as $result)
{
$name = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result)[0]->textContent;
$centered = $xpath->evaluate(".//td[@class='text-center']", $result);
$magnet = $xpath->evaluate(".//a[2]/@href", $centered[0])[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers;
$size = $centered[1]->textContent;
$seeders = $centered[3]->textContent;
$leechers = $centered[4]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "nyaa.si"
)
);
} }
return $results; public function get_results() {
$response = curl_multi_getcontent($this->ch);
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//tbody/tr") as $result)
{
$name_node = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result);
if ($name_node->length > 0) {
$name = $name_node[0]->textContent;
} else {
$name = "";
}
$centered = $xpath->evaluate(".//td[@class='text-center']", $result);
$magnet_node = $xpath->evaluate(".//a[2]/@href", $centered[0]);
if ($magnet_node->length > 0) {
$magnet = $magnet_node[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
} else {
$magnet = "";
}
$size = $centered[1]->textContent;
$seeders = $centered[3]->textContent;
$leechers = $centered[4]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => $this->SOURCE
)
);
}
return $results;
}
} }
?> ?>

View File

@ -1,36 +1,39 @@
<?php <?php
$rutor_url = "http://rutor.info/search/$query"; class RutorRequest extends EngineRequest {
public function get_request_url() {
function get_rutor_results($response) return "http://rutor.info/search/" . urlencode($this->query);
{
global $config;
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tr[@class='gai' or @class='tum']") as $result)
{
$name = $xpath->evaluate(".//td/a", $result)[2]->textContent;
$magnet = $xpath->evaluate(".//td/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers;
$td = $xpath->evaluate(".//td", $result);
$size = $td[count($td) == 5 ? 3 : 2]->textContent;
$seeders = $xpath->evaluate(".//span", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//span", $result)[1]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) filter_var($seeders, FILTER_SANITIZE_NUMBER_INT),
"leechers" => (int) filter_var($leechers, FILTER_SANITIZE_NUMBER_INT),
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "rutor.info"
)
);
} }
return $results; public function get_results() {
$response = curl_multi_getcontent($this->ch);
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//table/tr[@class='gai' or @class='tum']") as $result)
{
$name = $xpath->evaluate(".//td/a", $result)[2]->textContent;
$magnet = $xpath->evaluate(".//td/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
$td = $xpath->evaluate(".//td", $result);
$size = $td[count($td) == 5 ? 3 : 2]->textContent;
$seeders = $xpath->evaluate(".//span", $result)[0]->textContent;
$leechers = $xpath->evaluate(".//span", $result)[1]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) filter_var($seeders, FILTER_SANITIZE_NUMBER_INT),
"leechers" => (int) filter_var($leechers, FILTER_SANITIZE_NUMBER_INT),
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "rutor.info"
)
);
}
return $results;
}
} }
?> ?>

View File

@ -1,44 +1,6 @@
<?php <?php
$sukebei_url = "https://sukebei.nyaa.si/?q=$query"; include "engines/bittorrent/nyaa.php";
class SukebeiRequest extends NyaaRequest {
function get_sukebei_results($response) public $SOURCE = "sukebei.nyaa.si";
{
global $config;
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//tbody/tr") as $result)
{
$name_node = $xpath->evaluate(".//td[@colspan='2']//a[not(contains(@class, 'comments'))]/@title", $result);
if ($name_node->length > 0) {
$name = $name_node[0]->textContent;
} else {
$name = "";
}
$centered = $xpath->evaluate(".//td[@class='text-center']", $result);
$magnet_node = $xpath->evaluate(".//a[2]/@href", $centered[0]);
if ($magnet_node->length > 0) {
$magnet = $magnet_node[0]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers;
} else {
$magnet = "";
}
$size = $centered[1]->textContent;
$seeders = $centered[3]->textContent;
$leechers = $centered[4]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "sukebei.nyaa.si"
)
);
}
return $results;
} }
?> ?>

View File

@ -1,44 +1,46 @@
<?php <?php
class PirateBayRequest extends EngineRequest {
public function get_request_url() {
return "https://apibay.org/q.php?q=" . urlencode($this->query);
}
$thepiratebay_url = "https://apibay.org/q.php?q=$query"; public function get_results() {
$response = curl_multi_getcontent($this->ch);
$results = array();
$json_response = json_decode($response, true);
function get_thepiratebay_results($response) if (empty($json_response))
{ {
global $config; return $results;
$results = array(); }
$json_response = json_decode($response, true);
foreach ($json_response as $response)
{
$size = human_filesize($response["size"]);
$hash = $response["info_hash"];
$name = $response["name"];
$seeders = (int) $response["seeders"];
$leechers = (int) $response["leechers"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name" . $config->bittorent_trackers;
if ($name == "No results returned")
break;
array_push($results,
array (
"size" => htmlspecialchars($size),
"name" => htmlspecialchars($name),
"seeders" => (int) htmlspecialchars($seeders),
"leechers" => (int) htmlspecialchars($leechers),
"magnet" => htmlspecialchars($magnet),
"source" => "thepiratebay.org"
)
);
}
if (empty($json_response))
{
return $results; return $results;
} }
foreach ($json_response as $response)
{
$size = human_filesize($response["size"]);
$hash = $response["info_hash"];
$name = $response["name"];
$seeders = (int) $response["seeders"];
$leechers = (int) $response["leechers"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name" . $config->bittorent_trackers;
if ($name == "No results returned")
break;
array_push($results,
array (
"size" => htmlspecialchars($size),
"name" => htmlspecialchars($name),
"seeders" => (int) htmlspecialchars($seeders),
"leechers" => (int) htmlspecialchars($leechers),
"magnet" => htmlspecialchars($magnet),
"source" => "thepiratebay.org"
)
);
}
return $results;
} }
?> ?>

View File

@ -1,34 +1,38 @@
<?php <?php
$torrentgalaxy_url = "https://torrentgalaxy.to/torrents.php?search=$query#results"; class TorrentGalaxyRequest extends EngineRequest {
public function get_request_url() {
function get_torrentgalaxy_results($response) $query = urlencode($this->query);
{ return "https://torrentgalaxy.to/torrents.php?search=$query#results";
global $config;
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//div[@class='tgxtablerow txlight']") as $result)
{
$name = $xpath->evaluate(".//div[contains(@class, 'clickable-row')]", $result)[0]->textContent;
$magnet = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $config->bittorent_trackers;
$size = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span", $result)[0]->textContent;
$seeders = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[1]->textContent;
$leechers = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[2]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "torrentgalaxy.to"
)
);
} }
return $results; public function get_results() {
} $response = curl_multi_getcontent($this->ch);
$xpath = get_xpath($response);
$results = array();
foreach($xpath->query("//div[@class='tgxtablerow txlight']") as $result)
{
$name = $xpath->evaluate(".//div[contains(@class, 'clickable-row')]", $result)[0]->textContent;
$magnet = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/a/@href", $result)[1]->textContent;
$magnet_without_tracker = explode("&tr=", $magnet)[0];
$magnet = $magnet_without_tracker . $this->config->bittorent_trackers;
$size = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span", $result)[0]->textContent;
$seeders = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[1]->textContent;
$leechers = $xpath->evaluate(".//div[@class='tgxtablecell collapsehide rounded txlight']/span/font", $result)[2]->textContent;
array_push($results,
array (
"name" => htmlspecialchars($name),
"seeders" => (int) $seeders,
"leechers" => (int) $leechers,
"magnet" => htmlspecialchars($magnet),
"size" => htmlspecialchars($size),
"source" => "torrentgalaxy.to"
)
);
}
return $results;
}
}
?> ?>

View File

@ -1,45 +1,47 @@
<?php <?php
$yts_url = "https://yts.mx/api/v2/list_movies.json?query_term=$query"; class YTSRequest extends EngineRequest {
public function get_request_url() {
function get_yts_results($response) return "https://yts.mx/api/v2/list_movies.json?query_term=" . urlencode($this->query);
{
global $config;
$results = array();
$json_response = json_decode($response, true);
if ($json_response["status"] == "ok" && $json_response["data"]["movie_count"] != 0)
{
foreach ($json_response["data"]["movies"] as $movie)
{
$name = $movie["title"];
$name_encoded = urlencode($name);
foreach ($movie["torrents"] as $torrent)
{
$hash = $torrent["hash"];
$seeders = $torrent["seeds"];
$leechers = $torrent["peers"];
$size = $torrent["size"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config->bittorent_trackers";
array_push($results,
array (
"size" => htmlspecialchars($size),
"name" => htmlspecialchars($name),
"seeders" => htmlspecialchars($seeders),
"leechers" => htmlspecialchars($leechers),
"magnet" => htmlspecialchars($magnet),
"source" => "yts.mx"
)
);
}
}
} }
return $results; public function get_results() {
$response = curl_multi_getcontent($this->ch);
global $config;
$results = array();
$json_response = json_decode($response, true);
if ($json_response["status"] == "ok" && $json_response["data"]["movie_count"] != 0)
{
foreach ($json_response["data"]["movies"] as $movie)
{
$name = $movie["title"];
$name_encoded = urlencode($name);
foreach ($movie["torrents"] as $torrent)
{
$hash = $torrent["hash"];
$seeders = $torrent["seeds"];
$leechers = $torrent["peers"];
$size = $torrent["size"];
$magnet = "magnet:?xt=urn:btih:$hash&dn=$name_encoded$config->bittorent_trackers";
array_push($results,
array (
"size" => htmlspecialchars($size),
"name" => htmlspecialchars($name),
"seeders" => htmlspecialchars($seeders),
"leechers" => htmlspecialchars($leechers),
"magnet" => htmlspecialchars($magnet),
"source" => "yts.mx"
)
);
}
}
}
return $results;
}
} }
?> ?>

View File

@ -17,6 +17,10 @@
return ""; return "";
} }
public function successful() {
return curl_getinfo($this->ch)['http_code'] == '200';
}
abstract function get_results(); abstract function get_results();
static public function print_results($results){} static public function print_results($results){}
} }