simplified the process of adding more search sites

This commit is contained in:
huangjx
2022-02-28 23:15:37 +08:00
parent dfb0d3fd9f
commit 618417023b
15 changed files with 173 additions and 41 deletions

View File

@@ -5,9 +5,6 @@ namespace OCA\NCDownloader\AppInfo;
use OCA\NCDownloader\Controller\Aria2Controller; use OCA\NCDownloader\Controller\Aria2Controller;
use OCA\NCDownloader\Controller\MainController; use OCA\NCDownloader\Controller\MainController;
use OCA\NCDownloader\Controller\YoutubeController; 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\Aria2;
use OCA\NCDownloader\Tools\Client; use OCA\NCDownloader\Tools\Client;
use OCA\NCDownloader\Tools\Helper; use OCA\NCDownloader\Tools\Helper;
@@ -91,20 +88,16 @@ class Application extends App
$container->registerService('crawler', function () { $container->registerService('crawler', function () {
return new Crawler(); return new Crawler();
}); });
$container->registerService(TPB::class, function (IContainer $container) { $sites = Helper::getSearchSites();
$crawler = $container->query('crawler'); foreach ($sites as $site) {
$client = $container->query('httpClient'); //fully qualified class name
return new TPB($crawler, $client); $className = $site['class'];
}); $container->registerService($className, function (IContainer $container) use ($className) {
$container->registerService(bitSearch::class, function (IContainer $container) { $crawler = $container->query('crawler');
$crawler = $container->query('crawler'); $client = $container->query('httpClient');
$client = $container->query('httpClient'); return $className::create($crawler, $client);
return new bitSearch($crawler, $client); });
}); }
$container->registerService(sliderkz::class, function (IContainer $container) {
$client = $container->query('httpClient');
return new sliderkz($client);
});
} }
private function getRealDownloadDir() private function getRealDownloadDir()

View File

@@ -72,6 +72,12 @@ class MainController extends Controller
$params['counter'] = $this->counters->getCounters(); $params['counter'] = $this->counters->getCounters();
$params['python_installed'] = Helper::pythonInstalled(); $params['python_installed'] = Helper::pythonInstalled();
$params['ffmpeg_installed'] = Helper::ffmpegInstalled(); $params['ffmpeg_installed'] = Helper::ffmpegInstalled();
$sites = [];
foreach (Helper::getSearchSites() as $site) {
$label = $site['class']::getLabel();
$sites[] = ['name' => $site['name'], 'label' => strtoupper($label)];
}
$params['search_sites'] = json_encode($sites);
$errors = []; $errors = [];
if ($aria2_installed) { if ($aria2_installed) {

View File

@@ -69,4 +69,8 @@ class TPB extends searchBase implements searchInterface
$this->rows = $this->parse(); $this->rows = $this->parse();
return $this; return $this;
} }
public static function getLabel(): string
{
return 'thepiratebay';
}
} }

View File

@@ -80,4 +80,8 @@ class bitSearch extends searchBase implements searchInterface
$this->rows = $this->parse(); $this->rows = $this->parse();
return $this; return $this;
} }
public static function getLabel(): string
{
return 'bitsearch';
}
} }

View File

@@ -9,6 +9,7 @@ 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
{ {
@@ -18,6 +19,16 @@ abstract class searchBase
return $this->tableTitles; return $this->tableTitles;
} }
public static function create($crawler,$client)
{
if (!self::$instance) {
self::$instance = new static($crawler,$client);
}
return self::$instance;
}
public function setTableTitles(array $titles) public function setTableTitles(array $titles)
{ {
$this->tableTitles = $titles; $this->tableTitles = $titles;

View File

@@ -10,10 +10,12 @@ class sliderkz extends searchBase implements searchInterface
public $baseUrl = "https://slider.kz/vk_auth.php"; public $baseUrl = "https://slider.kz/vk_auth.php";
protected $query = null; protected $query = null;
protected $tableTitles = []; protected $tableTitles = [];
public function __construct($client)
public function __construct($crawler,$client)
{ {
$this->client = $client; $this->client = $client;
} }
public function search(string $keyword): array public function search(string $keyword): array
{ {
$this->query = ['q' => trim($keyword)]; $this->query = ['q' => trim($keyword)];
@@ -69,4 +71,9 @@ class sliderkz extends searchBase implements searchInterface
return []; return [];
} }
public static function getLabel(): string
{
return 'music';
}
} }

59
lib/Tools/File.php Normal file
View File

@@ -0,0 +1,59 @@
<?php
namespace OCA\NCDownloader\Tools;
class File
{
private $dirName;
//$dir_name = iconv("utf-8", "gb2312", $dir_name);
public function __construct($dirname, $suffix = "php")
{
$this->dirName = $dirname;
$this->suffix = $suffix;
if (!is_dir($dirname)) {
throw new \Exception("directory ${dirname} doesn't exit");
}
}
public static function create($dir, $suffix)
{
return new static($dir, $suffix);
}
public function scandir($recursive = false)
{
if ($recursive) {
return $this->scandirRecursive();
}
$files = \glob($this->dirName . DIRECTORY_SEPARATOR . "*.{$this->suffix}");
$this->Files = $files;
return $files;
}
protected function scandirRecursive()
{
$directory = new \RecursiveDirectoryIterator($this->dirName);
$iterator = new \RecursiveIteratorIterator($directory);
$iterators = new \RegexIterator($iterator, '/.*\.' . $this->suffix . '$/', \RegexIterator::GET_MATCH);
$files = array();
foreach ($iterators as $info) {
if ($info) {
$files[] = reset($info);
}
}
$this->Files = $files;
return $files;
}
public function getBasename($file){
return basename($file,".".$this->suffix);
}
}

View File

@@ -2,6 +2,7 @@
namespace OCA\NCDownloader\Tools; namespace OCA\NCDownloader\Tools;
use OCA\NCDownloader\Search\Sites\searchInterface;
use OCA\NCDownloader\Tools\aria2Options; use OCA\NCDownloader\Tools\aria2Options;
use OC\Files\Filesystem; use OC\Files\Filesystem;
@@ -331,4 +332,37 @@ class Helper
return (bool) self::findBinaryPath('python'); return (bool) self::findBinaryPath('python');
} }
public static function findSearchSites($dir, $suffix = 'php'): array
{
$filetool = File::create($dir, $suffix);
$files = $filetool->scandir();
$sites = [];
foreach ($files as $file) {
$basename = $filetool->getBasename($file);
$namespace = 'OCA\\NCDownloader\\Search\\Sites\\';
$className = $namespace . $basename;
if (in_array(searchInterface::class, class_implements($className))) {
$sites[] = ['class' => $className, 'name' => $basename];
}
}
return $sites;
}
public static function getSearchSites(): array
{
$key = 'searchSites';
$memcache = \OC::$server->getMemCacheFactory()->createDistributed($key);
if ($memcache->hasKey($key)) {
$sites = $memcache->get($key);
} else {
try {
$sites = Helper::findSearchSites(__DIR__ . "/../Search/Sites/");
$memcache->set($key, $sites, 300);
} catch (\Exception $e) {
self::debug($e->getMessage());
}
}
return $sites;
}
} }

View File

@@ -33,6 +33,12 @@ const successCallback = (data, element) => {
export default { export default {
name: "mainApp", name: "mainApp",
inject: ["settings"],
provide() {
return {
search_sites: this.settings.search_sites,
};
},
data() { data() {
return { return {
display: { download: true, search: false }, display: { download: true, search: false },

View File

@@ -43,7 +43,12 @@
></uploadFile> ></uploadFile>
</div> </div>
</div> </div>
<searchInput v-else @search="search" @optionSelected="optionCallback"></searchInput> <searchInput
v-else
@search="search"
@optionSelected="optionCallback"
:selectOptions="searchOptions"
></searchInput>
</div> </div>
</form> </form>
</template> </template>
@@ -55,6 +60,7 @@ import uploadFile from "./uploadFile";
import { translate as t } from "@nextcloud/l10n"; import { translate as t } from "@nextcloud/l10n";
export default { export default {
inject: ["settings", "search_sites"],
data() { data() {
return { return {
checkedValue: false, checkedValue: false,
@@ -64,6 +70,7 @@ export default {
downloadType: "aria2", downloadType: "aria2",
placeholder: t("ncdownloader", "Paste your http/magnet link here"), placeholder: t("ncdownloader", "Paste your http/magnet link here"),
searchLabel: t("ncdownloader", "Search Torrents"), searchLabel: t("ncdownloader", "Search Torrents"),
searchOptions: this.search_sites ? this.search_sites : this.noOptions(),
}; };
}, },
components: { components: {
@@ -72,6 +79,7 @@ export default {
searchInput, searchInput,
uploadFile, uploadFile,
}, },
created() {},
computed: {}, computed: {},
methods: { methods: {
whichType(type, event) { whichType(type, event) {
@@ -105,10 +113,13 @@ export default {
optionCallback(option) { optionCallback(option) {
if (option.label.toLowerCase() == "music") { if (option.label.toLowerCase() == "music") {
this.searchLabel = t("ncdownloader", "Search Music"); this.searchLabel = t("ncdownloader", "Search Music");
}else{ } else {
this.searchLabel = t("ncdownloader", "Search Torrents"); this.searchLabel = t("ncdownloader", "Search Torrents");
} }
}, },
noOptions() {
return [{ name: "nooptions", label: "No Options" }];
},
}, },
mounted() {}, mounted() {},
name: "mainForm", name: "mainForm",

View File

@@ -29,11 +29,6 @@ export default {
return { return {
placeholder: t("ncdownloader", "Enter keyword to search"), placeholder: t("ncdownloader", "Enter keyword to search"),
selected: "TPB", selected: "TPB",
selectOptions: [
{ name: "TPB", label: "THEPIRATEBAY" },
{ name: "bitSearch", label: "BITSEARCH" },
{ name: "sliderkz", label: "MUSIC" },
],
}; };
}, },
components: { components: {
@@ -53,7 +48,9 @@ export default {
}, },
}, },
name: "searchInput", name: "searchInput",
props: [], props: {
selectOptions: Object,
},
}; };
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">

View File

@@ -21,22 +21,24 @@ window.addEventListener('DOMContentLoaded', function () {
updatePage.run(); updatePage.run();
buttonActions.run(); buttonActions.run();
let container = 'ncdownloader-form-wrapper'; let container = 'ncdownloader-form-wrapper';
const settingsID = "app-settings-content"; const dataContainerID = "app-settings-data";
let app = createApp(App); let app = createApp(App);
let bar = createApp(settingsBar); let bar = createApp(settingsBar);
let values; const dataContainer = document.getElementById(dataContainerID);
let values = {};
try { try {
const barEle = document.getElementById(settingsID); let settings = dataContainer.getAttribute("data-settings");
let settings = barEle.getAttribute("data-settings"); let searchSites = dataContainer.getAttribute("data-search-sites");
values = JSON.parse(settings); values['settings'] = JSON.parse(settings);
values['search_sites'] = JSON.parse(searchSites);
} catch (e) { } catch (e) {
values = {} values = {}
console.log(e); console.log(e);
} }
bar.provide('settings', values); bar.provide('settings', values['settings']);
bar.mount("#" + settingsID); bar.mount("#" + "app-settings-content");
app.provide('settings', values);
let vm = app.mount('#' + container); let vm = app.mount('#' + container);
helper.addVue(vm.$options.name, vm); helper.addVue(vm.$options.name, vm);

View File

@@ -69,12 +69,6 @@ export default {
components: { components: {
toggleButton, toggleButton,
}, },
provide() {
return {
settings,
};
},
mounted() {}, mounted() {},
}; };
</script> </script>

View File

@@ -1,4 +1,8 @@
<?php
extract($_);
?>
<div id="app-ncdownloader-wrapper"> <div id="app-ncdownloader-wrapper">
<?php print_unescaped($this->inc('Navigation'));?> <?php print_unescaped($this->inc('Navigation'));?>
<?php print_unescaped($this->inc('Content'));?> <?php print_unescaped($this->inc('Content'));?>
<div id="app-settings-data" data-search-sites=<?php print $search_sites;?> data-settings='<?php print($settings);?>' ></div>
</div> </div>

View File

@@ -107,6 +107,6 @@ extract($_);
<?php p($l->t('Settings'));?> <?php p($l->t('Settings'));?>
</button> </button>
</div> </div>
<div id="app-settings-content" data-settings='<?php print($settings);?>' ></div> <div id="app-settings-content" ></div>
</div> </div>
</div> </div>