some changes for search

This commit is contained in:
huangjx
2022-04-26 18:13:18 +08:00
parent c42be35566
commit 3ac43ec88a
8 changed files with 70 additions and 26 deletions

View File

@@ -1,7 +1,7 @@
<?php <?php
namespace OCA\NCDownloader\Controller; namespace OCA\NCDownloader\Controller;
use OCA\NCDownloader\Search\torrentSearch; use OCA\NCDownloader\Search\siteSearch;
use OCP\AppFramework\Controller; use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse; use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest; use OCP\IRequest;
@@ -20,7 +20,7 @@ class SearchController extends Controller
$this->appName = $appName; $this->appName = $appName;
$this->uid = $UserId; $this->uid = $UserId;
$this->urlGenerator = \OC::$server->getURLGenerator(); $this->urlGenerator = \OC::$server->getURLGenerator();
$this->search = new torrentSearch(); $this->search = new siteSearch();
} }
/** /**
* @NoAdminRequired * @NoAdminRequired

View File

@@ -2,6 +2,8 @@
namespace OCA\NCDownloader\Search\Sites; namespace OCA\NCDownloader\Search\Sites;
use OCA\NCDownloader\Tools\tableData;
//The Piratebay //The Piratebay
class TPB extends searchBase implements searchInterface class TPB extends searchBase implements searchInterface
{ {
@@ -14,15 +16,15 @@ class TPB extends searchBase implements searchInterface
$this->client = $client; $this->client = $client;
$this->crawler = $crawler; $this->crawler = $crawler;
} }
public function search(string $keyword): array public function search(string $keyword): tableData
{ {
$this->searchUrl = $this->baseUrl . trim($keyword); $this->searchUrl = $this->baseUrl . trim($keyword);
$this->crawler->add($this->getContent()); $this->crawler->add($this->getContent());
$this->getItems()->addActionLinks(null); $this->getItems()->addActionLinks();
if ($this->hasErrors()) { if ($this->hasErrors()) {
return ['error' => $this->getErrors()]; return tableData::create()->setEror($this->getErrors());
} }
return ['title' => $this->getTableTitles(), 'row' => $this->getRows()]; return tableData::create($this->getTableTitles(), $this->getRows());
} }
public function setContent($content) public function setContent($content)
{ {

View File

@@ -2,6 +2,8 @@
namespace OCA\NCDownloader\Search\Sites; namespace OCA\NCDownloader\Search\Sites;
use OCA\NCDownloader\Tools\tableData;
//bitsearch.to //bitsearch.to
class bitSearch extends searchBase implements searchInterface class bitSearch extends searchBase implements searchInterface
{ {
@@ -16,17 +18,17 @@ class bitSearch extends searchBase implements searchInterface
$this->client = $client; $this->client = $client;
$this->crawler = $crawler; $this->crawler = $crawler;
} }
public function search(string $keyword): array public function search(string $keyword): tableData
{ {
$this->query = ['q' => trim($keyword), 'sort' => 'seeders']; $this->query = ['q' => trim($keyword), 'sort' => 'seeders'];
$this->searchUrl = $this->baseUrl; $this->searchUrl = $this->baseUrl;
$content = $this->getContent(); $content = $this->getContent();
if ($this->hasErrors()) { if ($this->hasErrors()) {
return ['error' => $this->getErrors()]; return tableData::create()->setEror($this->getErrors());
} }
$this->crawler->add($content); $this->crawler->add($content);
$this->getItems()->addActionLinks(null); $this->getItems()->addActionLinks();
return ['title' => $this->getTableTitles(), 'row' => $this->getRows()]; return tableData::create($this->getTableTitles(), $this->getRows());
} }
public function setContent($content) public function setContent($content)
{ {

View File

@@ -9,7 +9,6 @@ abstract class searchBase
protected $rows = []; protected $rows = [];
protected $errors = []; protected $errors = [];
protected $actionLinks = [["name" => 'download', 'path' => '/index.php/apps/ncdownloader/new'], ['name' => 'clipboard']]; protected $actionLinks = [["name" => 'download', 'path' => '/index.php/apps/ncdownloader/new'], ['name' => 'clipboard']];
private static $instance = null;
public function getTableTitles(): array public function getTableTitles(): array
{ {
@@ -22,11 +21,8 @@ abstract class searchBase
public static function create($crawler, $client) public static function create($crawler, $client)
{ {
if (!self::$instance) { return new static($crawler, $client);
self::$instance = new static($crawler,$client);
}
return self::$instance;
} }
public function setTableTitles(array $titles) public function setTableTitles(array $titles)
@@ -35,7 +31,7 @@ abstract class searchBase
return $this; return $this;
} }
protected function addActionLinks(?array $links) protected function addActionLinks(array $links = null)
{ {
$links = $links ?? $this->actionLinks; $links = $links ?? $this->actionLinks;
foreach ($this->rows as $key => &$value) { foreach ($this->rows as $key => &$value) {

View File

@@ -1,10 +1,11 @@
<?php <?php
namespace OCA\NCDownloader\Search\Sites; namespace OCA\NCDownloader\Search\Sites;
use OCA\NCDownloader\Tools\tableData;
interface searchInterface interface searchInterface
{ {
public function search(string $keyword):array; public function search(string $keyword):tableData;
public function getRows():array; public function getRows():array;
public function getTableTitles():array; public function getTableTitles():array;
} }

View File

@@ -3,6 +3,7 @@
namespace OCA\NCDownloader\Search\Sites; namespace OCA\NCDownloader\Search\Sites;
use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\Helper;
use OCA\NCDownloader\Tools\tableData;
//slider.kz //slider.kz
class sliderkz extends searchBase implements searchInterface class sliderkz extends searchBase implements searchInterface
@@ -16,15 +17,15 @@ class sliderkz extends searchBase implements searchInterface
$this->client = $client; $this->client = $client;
} }
public function search(string $keyword): array public function search(string $keyword): tableData
{ {
$this->query = ['q' => trim($keyword)]; $this->query = ['q' => trim($keyword)];
$this->searchUrl = $this->baseUrl; $this->searchUrl = $this->baseUrl;
$this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks(null); $this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks();
if ($this->hasErrors()) { if ($this->hasErrors()) {
return ['error' => $this->getErrors()]; return tableData::create()->setEror($this->getErrors());
} }
return ["title" => $this->getTableTitles(), 'row' => $this->getRows()]; return tableData::create($this->getTableTitles(), $this->getRows());
} }
public function getItems() public function getItems()

View File

@@ -7,7 +7,7 @@ use OCP\AppFramework\QueryException;
use OCP\IServerContainer; use OCP\IServerContainer;
use Symfony\Component\HttpClient\Exception\ClientException; use Symfony\Component\HttpClient\Exception\ClientException;
class torrentSearch class siteSearch
{ {
public $container; public $container;
private $site = null; private $site = null;
@@ -17,7 +17,7 @@ class torrentSearch
$this->container = \OC::$server->query(IServerContainer::class); $this->container = \OC::$server->query(IServerContainer::class);
$this->site = __NAMESPACE__ . '\Sites\TPB'; $this->site = __NAMESPACE__ . '\Sites\TPB';
} }
public function go($keyword) public function go($keyword): array
{ {
try { try {
$siteInst = $this->container->query($this->site); $siteInst = $this->container->query($this->site);
@@ -26,8 +26,11 @@ class torrentSearch
} catch (ClientException $e) { } catch (ClientException $e) {
return ['message', $e->getMessage()]; return ['message', $e->getMessage()];
} }
$data = $siteInst->search($keyword); $result = $siteInst->search($keyword);
return $data; if ($result->hasError()) {
return ['error' => $result->getError()];
}
return $result->getData();
} }
public function setSite($site) public function setSite($site)

39
lib/Tools/tableData.php Normal file
View File

@@ -0,0 +1,39 @@
<?php
namespace OCA\NCDownloader\Tools;
class tableData
{
protected $row, $title = [];
private $error = null;
public function __construct(array $titles = [], $rows = [])
{
$this->title = $titles;
$this->row = $rows;
}
public static function create(array $titles = [], $rows = [])
{
return new static($titles, $rows);
}
public function setError(string $error)
{
$this->error = $error;
return $this;
}
public function getError(): string
{
return $this->error;
}
public function hasError(): bool
{
return isset($this->error);
}
public function getData(): array
{
return ["title" => $this->title, "row" => $this->row];
}
}