simplified the process of adding more search sites
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -69,4 +69,8 @@ class TPB extends searchBase implements searchInterface
|
||||
$this->rows = $this->parse();
|
||||
return $this;
|
||||
}
|
||||
public static function getLabel(): string
|
||||
{
|
||||
return 'thepiratebay';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,4 +80,8 @@ class bitSearch extends searchBase implements searchInterface
|
||||
$this->rows = $this->parse();
|
||||
return $this;
|
||||
}
|
||||
public static function getLabel(): string
|
||||
{
|
||||
return 'bitsearch';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
59
lib/Tools/File.php
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user