Filter by category and sub-categories, helpers

This commit is contained in:
Simon Vieille 2015-02-10 20:13:46 +01:00
parent 417093ae9b
commit cfd9fef29c
9 changed files with 113 additions and 30 deletions

View file

@ -12,7 +12,8 @@
} }
], ],
"require": { "require": {
"php": ">=5.5" "php": ">=5.5",
"symfony/process": "~2.6"
}, },
"autoload": { "autoload": {
"psr-0": { "psr-0": {

View file

@ -50,7 +50,7 @@ class Client
$url = '/torrents/search/'.urlencode($query); $url = '/torrents/search/'.urlencode($query);
$query = []; $query = [];
foreach (['offset', 'limit', 'cat'] as $p) { foreach (['offset', 'limit', 'cat', 'cid'] as $p) {
if (!empty($options[$p])) { if (!empty($options[$p])) {
$query[$p] = $options[$p]; $query[$p] = $options[$p];
} }

View file

@ -15,7 +15,13 @@ class AuthLoginCommand extends Command
$this $this
->setName('auth:login') ->setName('auth:login')
->setDescription('Login on t411') ->setDescription('Login on t411')
->setHelp("The <info>%command.name%</info> "); ->setHelp("<info>%command.name%</info>
Generate the config to access the API. You must have a valid login/password.
<comment>The login and the password are not saved.</comment>
Usage: <comment>auth:login</comment>");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)

View file

@ -14,8 +14,12 @@ class CategoriesTreeCommand extends Command
{ {
$this $this
->setName('categories:tree') ->setName('categories:tree')
->setDescription('Show categories') ->setDescription('Show categories and sub-categories')
->setHelp("The <info>%command.name%</info> show the categories"); ->setHelp("<info>%command.name%</info>
List all categories and sub-categories with IDs.
Usage: <comment>categories:tree</comment>");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)

View file

@ -15,9 +15,13 @@ class TorrentsDetailsCommand extends Command
{ {
$this $this
->setName('torrents:details') ->setName('torrents:details')
->setDescription('Show torrent details') ->setDescription('Show a torrent details')
->addArgument('id', InputArgument::REQUIRED, 'Torrent id') ->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
->setHelp("The <info>%command.name%</info> show torrent details"); ->setHelp("<info>%command.name%</info>
Show torrent details.
Usage: <comment>torrents:details</comment> <info>TORRENT_ID</info>");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)

View file

@ -18,9 +18,15 @@ class TorrentsDownloadCommand extends Command
$this $this
->setName('torrents:download') ->setName('torrents:download')
->setDescription('Download a torrent') ->setDescription('Download a torrent')
->addArgument('id', InputArgument::REQUIRED, 'Torrent id') ->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
->addArgument('output_file', InputArgument::REQUIRED, 'Output file') ->addArgument('output_file', InputArgument::REQUIRED, 'Output')
->setHelp("The <info>%command.name%</info> download torrent"); ->setHelp("<info>%command.name%</info>
Download a torrent.
Usage: <comment>torrents:download</comment> <info>TORRENT_ID OUTPUT</info>
<info>OUTPUT</info> could be a file or STDIN by using <info>-</info>.");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)

View file

@ -19,11 +19,18 @@ class TorrentsSearchCommand extends Command
->setName('torrents:search') ->setName('torrents:search')
->setDescription('Search torrents') ->setDescription('Search torrents')
->addArgument('query', InputArgument::REQUIRED, 'Query') ->addArgument('query', InputArgument::REQUIRED, 'Query')
->addOption('offset', 'o', InputOption::VALUE_OPTIONAL, 'Search offset') ->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Search limit') ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
->addOption('category', 'c', InputOption::VALUE_OPTIONAL, 'Category') ->addOption('sub-category', 's', InputOption::VALUE_REQUIRED, 'Filter by sub-category ID')
->addOption('terms', 't', InputOption::VALUE_OPTIONAL, 'Terms') ->addOption('category', 'c', InputOption::VALUE_REQUIRED, 'Filter by category ID')
->setHelp("The <info>%command.name%</info> search torrents"); ->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")')
->setHelp("<info>%command.name%</info>
Search torrents.
Usage: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
<error>--terms does not work (API bug)</error>");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
@ -40,22 +47,61 @@ class TorrentsSearchCommand extends Command
$client->setAuthorization($configLoader->getConfig()['auth']['token']); $client->setAuthorization($configLoader->getConfig()['auth']['token']);
try { try {
$response = $client->searchTorrents( $categoryId = (int) $input->getOption('category');
$input->getArgument('query'), $termsTree = $client->getTermsTree();
array(
'offset' => (int) $input->getOption('offset'),
'limit' => (int) $input->getOption('limit'),
'cat' => (int) $input->getOption('category'),
'terms' => $this->convertTerms($input->getOption('terms'), $client->getTermsTree()->getData()),
)
);
return $this->showResults($response, $output); /**
* API HACK
* Category filter does not work
*/
if (!empty($categoryId)) {
$categoriesResponse = $client->getCategoriesTree();
if ($categoriesResponse->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
foreach ($categoriesResponse->getData() as $category) {
if (isset($category['id']) && (int) $category['id'] === $categoryId) {
foreach (array_keys($category['cats']) as $cid) {
$response = $this->searchTorrents($client, $input, $termsTree, $cid);
$this->showResults($response, $output);
$output->writeln('');
}
}
}
return;
}
$response = $this->searchTorrents($client, $input, $termsTree);
$this->showResults($response, $output);
} catch (ClientException $e) { } catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage())); $output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
} }
} }
protected function searchTorrents(Client $client, InputInterface $input, ClientResponse $termsTree, $cid = null)
{
return $client->searchTorrents(
$input->getArgument('query'),
array(
'offset' => (int) $input->getOption('offset'),
'limit' => (int) $input->getOption('limit'),
'cid' => $cid !== null ? $cid : (int) $input->getOption('sub-category'),
'terms' => $this->convertTerms($input->getOption('terms'), $termsTree->getData()),
)
);
}
public function convertTerms($value, array $termTypesTree) public function convertTerms($value, array $termTypesTree)
{ {
$value = trim($value); $value = trim($value);
@ -100,9 +146,15 @@ class TorrentsSearchCommand extends Command
return; return;
} }
$torrents = $response->getData()['torrents'];
if (empty($torrents)) {
return;
}
$output->writeln(' SEED LEECH ID NAME'); $output->writeln(' SEED LEECH ID NAME');
foreach ($response->getData()['torrents'] as $torrent) { foreach ($torrents as $torrent) {
$output->writeln(sprintf( $output->writeln(sprintf(
'[<info>%4d</info><comment>%6d</comment>] %9d %s', '[<info>%4d</info><comment>%6d</comment>] %9d %s',
$torrent['seeders'], $torrent['seeders'],

View file

@ -17,8 +17,14 @@ class TorrentsTopCommand extends Command
$this $this
->setName('torrents:top') ->setName('torrents:top')
->setDescription('Top torrents') ->setDescription('Top torrents')
->addOption('period', 'p', InputOption::VALUE_OPTIONAL, 'Period') ->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period')
->setHelp("The <info>%command.name%</info> show top torrents"); ->setHelp("<info>%command.name%</info>
Show top torrents.
Usage: <comment>torrents:search:top</comment> [OPTIONS]
<info>Period values: \"100\", \"day\", \"week\", \"month\"</info>");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)

View file

@ -18,7 +18,11 @@ class TypesTreeCommand extends Command
->setDescription('Show types') ->setDescription('Show types')
->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms') ->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms')
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter types by ID or by name') ->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter types by ID or by name')
->setHelp("The <info>%command.name%</info> show the terms"); ->setHelp("<info>%command.name%</info>
List all types of terms and terms.
Usage: <comment>types:tree</comment> [OPTIONS]");
} }
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)