added one more bt site;added the option for choosing a site to search torrents

This commit is contained in:
huangjx
2021-09-17 14:32:22 +08:00
parent 7427f395f5
commit de2afbb0ef
10 changed files with 200 additions and 47 deletions

View File

@@ -134,11 +134,11 @@ class Aria2Controller extends Controller
if (isset($resp['error'])) {
return new JSONResponse($resp);
}
$data = $this->prepareResp($resp);
$data = $this->transformResp($resp);
$data['counter'] = $counter;
return new JSONResponse($data);
}
private function prepareResp($resp)
private function transformResp($resp)
{
$data = [];
@@ -191,7 +191,9 @@ class Aria2Controller extends Controller
$left = Helper::formatInterval($remaining);
$numSeeders = $value['numSeeders'] ?? 0;
$extraInfo = "Seeders: $numSeeders";
$upload = $value['uploadLength'] ?? 0;
$upload = Helper::formatBytes($upload);
$extraInfo = "Seeders: $numSeeders|Up:$upload";
// $numPeers = isset($peers['result']) ? count($peers['result']) : 0;
$value['progress'] = array(sprintf("%s(%.2f%%)", $completed, $percentage), $extraInfo);
$timestamp = $timestamp ?? 0;

View File

@@ -19,12 +19,15 @@ class SearchController extends Controller
$this->appName = $appName;
$this->uid = $UserId;
$this->urlGenerator = \OC::$server->getURLGenerator();
$this->search = new torrentSearch();
}
public function execute()
{
$keyword = trim($this->request->getParam('form_input_text'));
$data = torrentSearch::go($keyword);
$site = trim($this->request->getParam('select-value-search'));
$this->search->setSite($site);
$data = $this->search->go($keyword);
$resp['title'] = ['title', 'seeders', 'info', 'actions'];
$resp['row'] = $data;
return new JSONResponse($resp);

View File

@@ -3,7 +3,7 @@
namespace OCA\NCDownloader\Search\Sites;
//The Piratebay
class TPB
class TPB implements searchBase
{
//html content
private $content = null;

View File

@@ -0,0 +1,65 @@
<?php
namespace OCA\NCDownloader\Search\Sites;
//bitsearch.to
class bitSearch implements searchBase
{
//html content
private $content = null;
public $baseUrl = "https://bitsearch.to/search";
private $query = null;
public function __construct($crawler, $client)
{
$this->client = $client;
$this->crawler = $crawler;
}
public function search($keyword)
{
$this->query = ['q' => trim($keyword), 'sort' => 'seeders'];
$this->searchUrl = $this->baseUrl;
//$this->setContent(file_get_contents(__DIR__ . "/BitSearch.html"));
$this->crawler->add($this->getContent());
return $this->parse();
}
public function setContent($content)
{
$this->content = $content;
}
public function getContent()
{
if ($this->content) {
return $this->content;
}
$response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]);
return $response->getContent();
}
public function parse()
{
$data = $this->crawler->filter(".w3-col.s12.mt-4 .search-result")->each(function ($node, $i) {
if ($node->getNode(0)) {
try {
$title = $node->filter(".info h5.title")->text();
$infoNode = $node->filter(".info .stats div");
$count = $infoNode->count();
$info = [];
for ($i = 0; $i < $count; $i++) {
$name = strtolower($infoNode->filter("img")->eq($i)->attr("alt"));
$info[$name] = trim($infoNode->eq($i)->text());
}
$seeders = $info['seeder'];
$info = sprintf("%s on %s", $info['size'], $info['date']);
$magnetLink = $node->filter(".links.center-flex a:nth-child(2)")->attr("href");
return ['title' => $title, 'data-link' => $magnetLink, 'seeders' => $seeders, 'info' => $info];
} catch (\Exception $e) {
//echo $e->getMessage();
}
}
});
return $data;
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace OCA\NCDownloader\Search\Sites;
interface searchBase
{
public function search($keyword);
public function parse();
}

View File

@@ -3,25 +3,35 @@
namespace OCA\NCDownloader\Search;
require __DIR__ . "/../../vendor/autoload.php";
use OCA\NCDownloader\Search\Sites\TPB;
use OCA\NCDownloader\Tools\Helper;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpClient\HttpClient;
use OCP\IServerContainer;
class torrentSearch
{
public static function go($keyword)
public $container;
private $site = null;
public function __construct()
{
$client = HttpClient::create();
$crawler = new Crawler();
$tpb = new TPB($crawler, $client);
$data = $tpb->search($keyword);
self::addAction($data);
$this->container = \OC::$server->query(IServerContainer::class);
$this->site = __NAMESPACE__ . '\Sites\TPB';
}
public function go($keyword)
{
$siteInst = $this->container->query($this->site);
$data = $siteInst->search($keyword);
$this->addAction($data);
return $data;
}
private static function addAction(&$data)
public function setSite($site)
{
if (strpos($site, '\\') !== false) {
$this->site = $site;
} else {
$this->site = __NAMESPACE__ . '\Sites\\' . $site;
}
}
private function addAction(&$data)
{
foreach ($data as $key => &$value) {
if (!$value) {