Skip to content

Commit

Permalink
simplified the process of adding more search sites
Browse files Browse the repository at this point in the history
  • Loading branch information
shiningw committed Feb 28, 2022
1 parent dfb0d3f commit 6184170
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 41 deletions.
27 changes: 10 additions & 17 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
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;
Expand Down Expand Up @@ -91,20 +88,16 @@ public function __construct(array $urlParams = array())
$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()
Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ private function buildParams(): array
$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) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Search/Sites/TPB.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public function getItems()
$this->rows = $this->parse();
return $this;
}
public static function getLabel(): string
{
return 'thepiratebay';
}
}
4 changes: 4 additions & 0 deletions lib/Search/Sites/bitSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ public function getItems()
$this->rows = $this->parse();
return $this;
}
public static function getLabel(): string
{
return 'bitsearch';
}
}
11 changes: 11 additions & 0 deletions lib/Search/Sites/searchBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -18,6 +19,16 @@ public function getTableTitles(): array
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;
Expand Down
9 changes: 8 additions & 1 deletion lib/Search/Sites/sliderkz.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Expand Down Expand Up @@ -69,4 +71,9 @@ public function getResponse(): array

return [];
}

public static function getLabel(): string
{
return 'music';
}
}
59 changes: 59 additions & 0 deletions lib/Tools/File.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
34 changes: 34 additions & 0 deletions lib/Tools/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\NCDownloader\Tools;

use OCA\NCDownloader\Search\Sites\searchInterface;
use OCA\NCDownloader\Tools\aria2Options;
use OC\Files\Filesystem;

Expand Down Expand Up @@ -331,4 +332,37 @@ public static function pythonInstalled()
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;
}

}
6 changes: 6 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
15 changes: 13 additions & 2 deletions src/components/mainForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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,
Expand All @@ -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: {
Expand All @@ -72,6 +79,7 @@ export default {
searchInput,
uploadFile,
},
created() {},
computed: {},
methods: {
whichType(type, event) {
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 3 additions & 6 deletions src/components/searchInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -53,7 +48,9 @@ export default {
},
},
name: "searchInput",
props: [],
props: {
selectOptions: Object,
},
};
</script>
<style scoped lang="scss">
Expand Down
18 changes: 10 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 0 additions & 6 deletions src/settingsBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ export default {
components: {
toggleButton,
},
provide() {
return {
settings,
};
},
mounted() {},
};
</script>
Expand Down
4 changes: 4 additions & 0 deletions templates/Index.php
Original file line number Diff line number Diff line change
@@ -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>
2 changes: 1 addition & 1 deletion templates/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,6 @@ class="settings-button"
<?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>

0 comments on commit 6184170

Please sign in to comment.