From 963334dee5577e9a00da0e5909d3596c197c39ba Mon Sep 17 00:00:00 2001 From: huangjx Date: Sat, 26 Feb 2022 22:00:50 +0800 Subject: [PATCH] added experimental support for a music site --- appinfo/application.php | 12 ++++++- lib/Search/Sites/sliderkz.php | 65 ++++++++++++++++++++++++++++++++++ src/components/mainForm.vue | 24 +++++++++---- src/components/searchInput.vue | 15 ++++++-- 4 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 lib/Search/Sites/sliderkz.php diff --git a/appinfo/application.php b/appinfo/application.php index 0e0f2b6..567197c 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -6,6 +6,7 @@ use OCA\NCDownloader\Controller\Aria2Controller; use OCA\NCDownloader\Controller\MainController; use OCA\NCDownloader\Controller\YoutubeController; use OCA\NCDownloader\Search\Sites\bitSearch; +use OCA\NCDownloader\Search\Sites\sliderkz; use OCA\NCDownloader\Search\Sites\TPB; use OCA\NCDownloader\Tools\Aria2; use OCA\NCDownloader\Tools\Helper; @@ -85,7 +86,12 @@ class Application extends App return new Crawler(); }); $container->registerService('httpClient', function () { - return HttpClient::create(); + $agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'; + return HttpClient::create([ + 'headers' => [ + 'User-Agent' => $agent, + ], + ]); }); $container->registerService(TPB::class, function (IContainer $container) { $crawler = $container->query('crawler'); @@ -97,6 +103,10 @@ class Application extends App $client = $container->query('httpClient'); return new bitSearch($crawler, $client); }); + $container->registerService(sliderkz::class, function (IContainer $container) { + $client = $container->query('httpClient'); + return new sliderkz($client); + }); } private function getRealDownloadDir() diff --git a/lib/Search/Sites/sliderkz.php b/lib/Search/Sites/sliderkz.php new file mode 100644 index 0000000..4b9a76a --- /dev/null +++ b/lib/Search/Sites/sliderkz.php @@ -0,0 +1,65 @@ +client = $client; + } + public function search(string $keyword): array + { + $this->query = ['q' => trim($keyword)]; + $this->searchUrl = $this->baseUrl; + $this->getItems()->setTableTitles(["Title", "Duration", "Actions"])->addActionLinks(null); + return ["title" => $this->getTableTitles(), 'row' => $this->getRows()]; + } + + public function getItems() + { + $data = $this->getResponse(); + $this->rows = $this->transformResp($data); + return $this; + } + protected function getDownloadUrl(array $item): string + { + extract($item); + return sprintf("https://slider.kz/download/%s/%s/%s/%s.mp3?extra=null", $id, $duration, $url, urlencode($tit_art)); + } + + private function transformResp($data): array + { + $items = []; + if (count($data) < 1) { + return []; + } + foreach ($data as $item) { + $items[] = array("title" => $item["tit_art"], "data-link" => $this->getDownloadUrl($item), "duration" => Helper::formatInterval($item["duration"])); + } + unset($data); + return $items; + } + + public function getResponse(): array + { + + try { + $response = $this->client->request('GET', $this->searchUrl, ['query' => $this->query]); + $resp = $response->toArray(); + } catch (ExceptionInterface $e) { + return ["error" => $e->getMessage()]; + } + if (isset($resp['audios'])) { + return array_values($resp["audios"])[0]; + } + + return []; + } +} diff --git a/src/components/mainForm.vue b/src/components/mainForm.vue index 402f78d..386835b 100644 --- a/src/components/mainForm.vue +++ b/src/components/mainForm.vue @@ -17,7 +17,7 @@ class="search-torrents option-buttons" @click.prevent="whichType('search', $event)" > - Search Torrents + {{ searchLabel }}
@@ -36,10 +36,14 @@ >
- + - + @@ -48,6 +52,7 @@ import textInput from "./textInput"; import searchInput from "./searchInput.vue"; import actionButton from "./actionButton"; import uploadFile from "./uploadFile"; +import { translate as t } from "@nextcloud/l10n"; export default { data() { @@ -57,7 +62,8 @@ export default { inputType: "download", checkboxes: false, downloadType: "aria2", - placeholder: "Paste your http/magnet link here", + placeholder: t("ncdownloader", "Paste your http/magnet link here"), + searchLabel: t("ncdownloader", "Search Torrents"), }; }, components: { @@ -78,9 +84,8 @@ export default { this.downloadType = type; if (type === "aria2") { this.path = this.uris.aria2_url; - this.placeholder = "Paste your http/magnet link here"; } else if (type === "youtube-dl") { - this.placeholder = "Paste your video link here"; + this.placeholder = t("ncdownloader", "Paste your video link here"); this.path = this.uris.ytd_url; } else { this.path = this.uris.search_url; @@ -97,6 +102,13 @@ export default { uploadFile(event, vm) { this.$emit("uploadfile", event, vm); }, + optionCallback(option) { + if (option.label.toLowerCase() == "music") { + this.searchLabel = t("ncdownloader", "Search Music"); + }else{ + this.searchLabel = t("ncdownloader", "Search Torrents"); + } + }, }, mounted() {}, name: "mainForm", diff --git a/src/components/searchInput.vue b/src/components/searchInput.vue index a4aa3f0..5e65860 100644 --- a/src/components/searchInput.vue +++ b/src/components/searchInput.vue @@ -2,8 +2,8 @@
-