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": {
"php": ">=5.5"
"php": ">=5.5",
"symfony/process": "~2.6"
},
"autoload": {
"psr-0": {

View file

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

View file

@ -15,7 +15,13 @@ class AuthLoginCommand extends Command
$this
->setName('auth:login')
->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)

View file

@ -14,8 +14,12 @@ class CategoriesTreeCommand extends Command
{
$this
->setName('categories:tree')
->setDescription('Show categories')
->setHelp("The <info>%command.name%</info> show the categories");
->setDescription('Show categories and sub-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)

View file

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

View file

@ -18,9 +18,15 @@ class TorrentsDownloadCommand extends Command
$this
->setName('torrents:download')
->setDescription('Download a torrent')
->addArgument('id', InputArgument::REQUIRED, 'Torrent id')
->addArgument('output_file', InputArgument::REQUIRED, 'Output file')
->setHelp("The <info>%command.name%</info> download torrent");
->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
->addArgument('output_file', InputArgument::REQUIRED, 'Output')
->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)

View file

@ -19,11 +19,18 @@ class TorrentsSearchCommand extends Command
->setName('torrents:search')
->setDescription('Search torrents')
->addArgument('query', InputArgument::REQUIRED, 'Query')
->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 <info>%command.name%</info> search torrents");
->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
->addOption('sub-category', 's', InputOption::VALUE_REQUIRED, 'Filter by sub-category ID')
->addOption('category', 'c', InputOption::VALUE_REQUIRED, 'Filter by category ID')
->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)
@ -40,22 +47,61 @@ class TorrentsSearchCommand extends Command
$client->setAuthorization($configLoader->getConfig()['auth']['token']);
try {
$response = $client->searchTorrents(
$input->getArgument('query'),
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()),
)
);
$categoryId = (int) $input->getOption('category');
$termsTree = $client->getTermsTree();
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) {
$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)
{
$value = trim($value);
@ -100,9 +146,15 @@ class TorrentsSearchCommand extends Command
return;
}
$torrents = $response->getData()['torrents'];
if (empty($torrents)) {
return;
}
$output->writeln(' SEED LEECH ID NAME');
foreach ($response->getData()['torrents'] as $torrent) {
foreach ($torrents as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%6d</comment>] %9d %s',
$torrent['seeders'],

View file

@ -17,8 +17,14 @@ class TorrentsTopCommand extends Command
$this
->setName('torrents:top')
->setDescription('Top torrents')
->addOption('period', 'p', InputOption::VALUE_OPTIONAL, 'Period')
->setHelp("The <info>%command.name%</info> show top torrents");
->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period')
->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)

View file

@ -18,7 +18,11 @@ class TypesTreeCommand extends Command
->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 <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)