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

View File

@@ -72,6 +72,12 @@ class MainController extends Controller
$params['counter'] = $this->counters->getCounters();
$params['python_installed'] = Helper::pythonInstalled();
$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 = [];
if ($aria2_installed) {

View File

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

View File

@@ -9,6 +9,7 @@ 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
{
@@ -18,6 +19,16 @@ abstract class searchBase
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)
{
$this->tableTitles = $titles;

View File

@@ -10,10 +10,12 @@ class sliderkz extends searchBase implements searchInterface
public $baseUrl = "https://slider.kz/vk_auth.php";
protected $query = null;
protected $tableTitles = [];
public function __construct($client)
public function __construct($crawler,$client)
{
$this->client = $client;
}
public function search(string $keyword): array
{
$this->query = ['q' => trim($keyword)];
@@ -69,4 +71,9 @@ class sliderkz extends searchBase implements searchInterface
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;
use OCA\NCDownloader\Search\Sites\searchInterface;
use OCA\NCDownloader\Tools\aria2Options;
use OC\Files\Filesystem;
@@ -331,4 +332,37 @@ class Helper
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 {
name: "mainApp",
inject: ["settings"],
provide() {
return {
search_sites: this.settings.search_sites,
};
},
data() {
return {
display: { download: true, search: false },

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,8 @@
<?php
extract($_);
?>
<div id="app-ncdownloader-wrapper">
<?php print_unescaped($this->inc('Navigation'));?>
<?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>

View File

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