diff --git a/src/Api/Client.php b/src/Api/Client.php
index 00060b1..f789dd4 100644
--- a/src/Api/Client.php
+++ b/src/Api/Client.php
@@ -50,12 +50,16 @@ class Client
$url = '/torrents/search/'.urlencode($query);
$query = array();
- foreach (array('offset', 'limit') as $p) {
+ foreach (array('offset', 'limit', 'cat') as $p) {
if (!empty($options[$p])) {
$query[$p] = $options[$p];
}
}
+ if (!empty($options['terms'])) {
+ $query['terms'] = $options['terms'];
+ }
+
$url.= '?'.http_build_query($query);
return $this->get(true, $url);
diff --git a/src/Console/Command/CategoriesTreeCommand.php b/src/Console/Command/CategoriesTreeCommand.php
index 1a1727f..68fdbf9 100644
--- a/src/Console/Command/CategoriesTreeCommand.php
+++ b/src/Console/Command/CategoriesTreeCommand.php
@@ -45,17 +45,23 @@ class CategoriesTreeCommand extends Command
}
foreach ($response->getData() as $category) {
- if (isset($category['name'])) {
- $output->writeln(sprintf('`- %s', $category['name']));
+ if (!isset($category['name'])) {
+ $category['name'] = 'Category name not defined';
}
+ $output->writeln(sprintf('%3d %s', isset($category['id']) ? $category['id'] : null, $category['name']));
+
if (!empty($category['cats'])) {
$isFirst = true;
- foreach ($category['cats'] as $subCategory) {
- if (isset($subCategory['name'])) {
- $output->writeln(sprintf(' |- %s', $subCategory['name']));
+ foreach ($category['cats'] as $subCategoryId => $subCategory) {
+ $char = '|';
+ if ($isFirst) {
+ $isFirst = false;
+ $char = '`';
}
+
+ $output->writeln(sprintf(' %s- %4d %s', $char, $subCategoryId, $subCategory['name']));
}
}
diff --git a/src/Console/Command/TermsTreeCommand.php b/src/Console/Command/TermsTreeCommand.php
deleted file mode 100644
index d9a3ff5..0000000
--- a/src/Console/Command/TermsTreeCommand.php
+++ /dev/null
@@ -1,76 +0,0 @@
-setName('terms:tree')
- ->setDescription('Show terms')
- ->setHelp("The %command.name% show the terms");
- }
-
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $client = new Client();
- $configLoader = new ConfigLoader();
-
- if (!isset($configLoader->getConfig()['auth']['token'])) {
- $output->writeln('You must login.');
-
- return;
- }
-
- $client->setAuthorization($configLoader->getConfig()['auth']['token']);
-
- try {
- $categoriesReponse = $client->getCategoriesTree();
- $termsResponse = $client->getTermsTree();
-
- foreach (array($categoriesReponse, $termsResponse) as $response) {
- if ($response->hasError()) {
- $output->writeln(sprintf(
- '%s (%d)',
- $response->getErrorMessage(),
- $response->getErrorCode()
- ));
-
- return;
- }
- }
-
- foreach ($termsResponse->getData() as $categoryId => $termTypes) {
- foreach ($categoriesReponse->getData() as $id => $category) {
- if ($categoryId === $id) {
- if (isset($category['name'])) {
- $output->writeln(sprintf('`- %s', $category['name']));
- } else {
- $output->writeln(sprintf('`- %s', 'Not defined'));
- }
- }
-
- foreach ($termTypes as $termType) {
- if (isset($termType['type'])) {
- $output->writeln(sprintf(' +- %s', $termType['type']));
- }
-
- foreach ($termType['terms'] as $term) {
- $output->writeln(sprintf(' | %s', $term));
- }
- }
- $output->writeln('');
- }
- }
- } catch (ClientException $e) {
- $output->writeln(sprintf('An error occured. %s', $e->getMessage()));
- }
- }
-}
diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php
index d964b5c..90321a7 100644
--- a/src/Console/Command/TorrentsSearchCommand.php
+++ b/src/Console/Command/TorrentsSearchCommand.php
@@ -9,6 +9,7 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
+use Api\ClientResponse;
class TorrentsSearchCommand extends Command
{
@@ -18,11 +19,10 @@ class TorrentsSearchCommand extends Command
->setName('torrents:search')
->setDescription('Search torrents')
->addArgument('query', InputArgument::REQUIRED, 'Query')
- ->addOption('offset', null, InputOption::VALUE_OPTIONAL, 'Search offset')
- ->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Search limit')
- ->addOption('category', null, InputOption::VALUE_OPTIONAL, 'Category')
- ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Type')
- ->addOption('term', null, InputOption::VALUE_OPTIONAL, 'Term')
+ ->addOption('offset', 'o', InputOption::VALUE_OPTIONAL, 'Search offset')
+ ->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Search limit')
+ ->addOption('category', 'c', InputOption::VALUE_OPTIONAL, 'Category')
+ ->addOption('terms', 't', InputOption::VALUE_OPTIONAL, 'Terms')
->setHelp("The %command.name% search torrents");
}
@@ -45,35 +45,71 @@ class TorrentsSearchCommand extends Command
array(
'offset' => (int) $input->getOption('offset'),
'limit' => (int) $input->getOption('limit'),
- 'category' => $input->getOption('category'),
- 'type' => $input->getOption('type'),
- 'term' => $input->getOption('term'),
+ 'cat' => (int) $input->getOption('category'),
+ 'terms' => $this->convertTerms($input->getOption('terms'), $client->getTermsTree()->getData()),
)
);
- if ($response->hasError()) {
- $output->writeln(sprintf(
- '%s (%d)',
- $response->getErrorMessage(),
- $response->getErrorCode()
- ));
-
- return;
- }
-
- $output->writeln(' ID NAME');
-
- foreach ($response->getData()['torrents'] as $torrent) {
- $output->writeln(sprintf(
- '[%4d%4d] %9d %s',
- $torrent['seeders'],
- $torrent['leechers'],
- $torrent['id'],
- $torrent['name']
- ));
- }
+ return $this->showResults($response, $output);
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. %s', $e->getMessage()));
}
}
+
+ public function convertTerms($value, array $termTypesTree)
+ {
+ $value = trim($value);
+
+ $terms = array_map(
+ function ($v) {
+ return (int) trim($v);
+ },
+ explode(',', $value)
+ );
+
+ $finalTerms = array();
+
+ foreach ($termTypesTree as $termTypes) {
+ foreach ($termTypes as $termTypeId => $termType) {
+ foreach ($terms as $term) {
+ if (isset($termType['terms'][$term])) {
+ if (!isset($finalTerms[$termTypeId])) {
+ $finalTerms[$termTypeId] = array();
+ }
+
+ if (!in_array($term, $finalTerms[$termTypeId])) {
+ $finalTerms[$termTypeId][] = $term;
+ }
+ }
+ }
+ }
+ }
+
+ return $finalTerms;
+ }
+
+ protected function showResults(ClientResponse $response, OutputInterface $output)
+ {
+ if ($response->hasError()) {
+ $output->writeln(sprintf(
+ '%s (%d)',
+ $response->getErrorMessage(),
+ $response->getErrorCode()
+ ));
+
+ return;
+ }
+
+ $output->writeln(' SEED LEECH ID NAME');
+
+ foreach ($response->getData()['torrents'] as $torrent) {
+ $output->writeln(sprintf(
+ '[%4d%6d] %9d %s',
+ $torrent['seeders'],
+ $torrent['leechers'],
+ $torrent['id'],
+ $torrent['name']
+ ));
+ }
+ }
}
diff --git a/src/Console/Command/TypesTreeCommand.php b/src/Console/Command/TypesTreeCommand.php
new file mode 100644
index 0000000..9464785
--- /dev/null
+++ b/src/Console/Command/TypesTreeCommand.php
@@ -0,0 +1,105 @@
+setName('types:tree')
+ ->setDescription('Show types')
+ ->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms')
+ ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter types by ID or by name')
+ ->setHelp("The %command.name% show the terms");
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output)
+ {
+ $client = new Client();
+ $configLoader = new ConfigLoader();
+
+ if (!isset($configLoader->getConfig()['auth']['token'])) {
+ $output->writeln('You must login.');
+
+ return;
+ }
+
+ $client->setAuthorization($configLoader->getConfig()['auth']['token']);
+
+ try {
+ $categoriesReponse = $client->getCategoriesTree();
+ $termsResponse = $client->getTermsTree();
+
+ foreach (array($categoriesReponse, $termsResponse) as $response) {
+ if ($response->hasError()) {
+ $output->writeln(sprintf(
+ '%s (%d)',
+ $response->getErrorMessage(),
+ $response->getErrorCode()
+ ));
+
+ return;
+ }
+ }
+
+ $filter = $input->getOption('filter');
+
+ if (!empty($filter)) {
+ if (is_numeric($filter)) {
+ $filter = (int) $filter;
+ }
+ }
+
+ foreach ($termsResponse->getData() as $categoryId => $termTypes) {
+ $stop = false;
+
+ foreach ($termTypes as $termTypeId => $termType) {
+ if ($filter !== null) {
+ if (is_int($filter)) {
+ if ((int) $termTypeId === $filter) {
+ $stop = true;
+ } else {
+ continue;
+ }
+ } else {
+ if (0 === preg_match(sprintf('/%s/U', preg_quote($filter)), $termType['type'])) {
+ continue;
+ }
+ }
+ }
+
+ $output->writeln(sprintf('%3d %s', $termTypeId, $termType['type']));
+
+ if ($input->getOption('terms')) {
+ $isFirst = true;
+ foreach ($termType['terms'] as $termId => $term) {
+ $char = '|';
+ if ($isFirst) {
+ $isFirst = false;
+ $char = '`';
+ }
+
+ $output->writeln(sprintf(' %s- %4d %s', $char, $termId, $term));
+ }
+
+ $output->writeln('');
+ }
+
+ if ($stop) {
+ return;
+ }
+ }
+ }
+ } catch (ClientException $e) {
+ $output->writeln(sprintf('An error occured. %s', $e->getMessage()));
+ }
+ }
+}