From 3ac43ec88a1a7cd86951018af4b76ae4bcaf97c2 Mon Sep 17 00:00:00 2001 From: huangjx Date: Tue, 26 Apr 2022 18:13:18 +0800 Subject: [PATCH] some changes for search --- lib/Controller/SearchController.php | 4 +- lib/Search/Sites/TPB.php | 10 +++-- lib/Search/Sites/bitSearch.php | 10 +++-- lib/Search/Sites/searchBase.php | 10 ++--- lib/Search/Sites/searchInterface.php | 3 +- lib/Search/Sites/sliderkz.php | 9 +++-- .../{torrentSearch.php => siteSearch.php} | 11 ++++-- lib/Tools/tableData.php | 39 +++++++++++++++++++ 8 files changed, 70 insertions(+), 26 deletions(-) rename lib/Search/{torrentSearch.php => siteSearch.php} (80%) create mode 100644 lib/Tools/tableData.php diff --git a/lib/Controller/SearchController.php b/lib/Controller/SearchController.php index a92b1e9..502a28f 100644 --- a/lib/Controller/SearchController.php +++ b/lib/Controller/SearchController.php @@ -1,7 +1,7 @@ appName = $appName; $this->uid = $UserId; $this->urlGenerator = \OC::$server->getURLGenerator(); - $this->search = new torrentSearch(); + $this->search = new siteSearch(); } /** * @NoAdminRequired diff --git a/lib/Search/Sites/TPB.php b/lib/Search/Sites/TPB.php index 15b265b..f4c7bf4 100644 --- a/lib/Search/Sites/TPB.php +++ b/lib/Search/Sites/TPB.php @@ -2,6 +2,8 @@ namespace OCA\NCDownloader\Search\Sites; +use OCA\NCDownloader\Tools\tableData; + //The Piratebay class TPB extends searchBase implements searchInterface { @@ -14,15 +16,15 @@ class TPB extends searchBase implements searchInterface $this->client = $client; $this->crawler = $crawler; } - public function search(string $keyword): array + public function search(string $keyword): tableData { $this->searchUrl = $this->baseUrl . trim($keyword); $this->crawler->add($this->getContent()); - $this->getItems()->addActionLinks(null); + $this->getItems()->addActionLinks(); 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) { diff --git a/lib/Search/Sites/bitSearch.php b/lib/Search/Sites/bitSearch.php index 8e18002..61fc5a7 100644 --- a/lib/Search/Sites/bitSearch.php +++ b/lib/Search/Sites/bitSearch.php @@ -2,6 +2,8 @@ namespace OCA\NCDownloader\Search\Sites; +use OCA\NCDownloader\Tools\tableData; + //bitsearch.to class bitSearch extends searchBase implements searchInterface { @@ -16,17 +18,17 @@ class bitSearch extends searchBase implements searchInterface $this->client = $client; $this->crawler = $crawler; } - public function search(string $keyword): array + public function search(string $keyword): tableData { $this->query = ['q' => trim($keyword), 'sort' => 'seeders']; $this->searchUrl = $this->baseUrl; $content = $this->getContent(); if ($this->hasErrors()) { - return ['error' => $this->getErrors()]; + return tableData::create()->setEror($this->getErrors()); } $this->crawler->add($content); - $this->getItems()->addActionLinks(null); - return ['title' => $this->getTableTitles(), 'row' => $this->getRows()]; + $this->getItems()->addActionLinks(); + return tableData::create($this->getTableTitles(), $this->getRows()); } public function setContent($content) { diff --git a/lib/Search/Sites/searchBase.php b/lib/Search/Sites/searchBase.php index b9c8874..4f8ae39 100644 --- a/lib/Search/Sites/searchBase.php +++ b/lib/Search/Sites/searchBase.php @@ -9,7 +9,6 @@ abstract class searchBase protected $rows = []; protected $errors = []; protected $actionLinks = [["name" => 'download', 'path' => '/index.php/apps/ncdownloader/new'], ['name' => 'clipboard']]; - private static $instance = null; public function getTableTitles(): array { @@ -19,14 +18,11 @@ abstract class searchBase return $this->tableTitles; } - public static function create($crawler,$client) + public static function create($crawler, $client) { - if (!self::$instance) { - self::$instance = new static($crawler,$client); - } + return new static($crawler, $client); - return self::$instance; } public function setTableTitles(array $titles) @@ -35,7 +31,7 @@ abstract class searchBase return $this; } - protected function addActionLinks(?array $links) + protected function addActionLinks(array $links = null) { $links = $links ?? $this->actionLinks; foreach ($this->rows as $key => &$value) { diff --git a/lib/Search/Sites/searchInterface.php b/lib/Search/Sites/searchInterface.php index 6258628..d699883 100644 --- a/lib/Search/Sites/searchInterface.php +++ b/lib/Search/Sites/searchInterface.php @@ -1,10 +1,11 @@ client = $client; } - public function search(string $keyword): array + public function search(string $keyword): tableData { $this->query = ['q' => trim($keyword)]; $this->searchUrl = $this->baseUrl; - $this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks(null); + $this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks(); 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() diff --git a/lib/Search/torrentSearch.php b/lib/Search/siteSearch.php similarity index 80% rename from lib/Search/torrentSearch.php rename to lib/Search/siteSearch.php index fc8cb6f..3d329ef 100644 --- a/lib/Search/torrentSearch.php +++ b/lib/Search/siteSearch.php @@ -7,7 +7,7 @@ use OCP\AppFramework\QueryException; use OCP\IServerContainer; use Symfony\Component\HttpClient\Exception\ClientException; -class torrentSearch +class siteSearch { public $container; private $site = null; @@ -17,7 +17,7 @@ class torrentSearch $this->container = \OC::$server->query(IServerContainer::class); $this->site = __NAMESPACE__ . '\Sites\TPB'; } - public function go($keyword) + public function go($keyword): array { try { $siteInst = $this->container->query($this->site); @@ -26,8 +26,11 @@ class torrentSearch } catch (ClientException $e) { return ['message', $e->getMessage()]; } - $data = $siteInst->search($keyword); - return $data; + $result = $siteInst->search($keyword); + if ($result->hasError()) { + return ['error' => $result->getError()]; + } + return $result->getData(); } public function setSite($site) diff --git a/lib/Tools/tableData.php b/lib/Tools/tableData.php new file mode 100644 index 0000000..dad8656 --- /dev/null +++ b/lib/Tools/tableData.php @@ -0,0 +1,39 @@ +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]; + } +}