diff --git a/composer.json b/composer.json index 9e7139a..c27b75b 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ } ], "require": { - "php": ">=5.5" + "php": ">=5.5", + "symfony/process": "~2.6" }, "autoload": { "psr-0": { diff --git a/src/Api/Client.php b/src/Api/Client.php index b6ce1e7..9d7ea91 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -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]; } diff --git a/src/Console/Command/AuthLoginCommand.php b/src/Console/Command/AuthLoginCommand.php index 747865a..c4a1a6f 100644 --- a/src/Console/Command/AuthLoginCommand.php +++ b/src/Console/Command/AuthLoginCommand.php @@ -15,7 +15,13 @@ class AuthLoginCommand extends Command $this ->setName('auth:login') ->setDescription('Login on t411') - ->setHelp("The %command.name% "); + ->setHelp("%command.name% + +Generate the config to access the API. You must have a valid login/password. + +The login and the password are not saved. + +Usage: auth:login"); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Console/Command/CategoriesTreeCommand.php b/src/Console/Command/CategoriesTreeCommand.php index 68fdbf9..f2f6f10 100644 --- a/src/Console/Command/CategoriesTreeCommand.php +++ b/src/Console/Command/CategoriesTreeCommand.php @@ -14,8 +14,12 @@ class CategoriesTreeCommand extends Command { $this ->setName('categories:tree') - ->setDescription('Show categories') - ->setHelp("The %command.name% show the categories"); + ->setDescription('Show categories and sub-categories') + ->setHelp("%command.name% + +List all categories and sub-categories with IDs. + +Usage: categories:tree"); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Console/Command/TorrentsDetailsCommand.php b/src/Console/Command/TorrentsDetailsCommand.php index b273319..1c55df1 100644 --- a/src/Console/Command/TorrentsDetailsCommand.php +++ b/src/Console/Command/TorrentsDetailsCommand.php @@ -15,9 +15,13 @@ class TorrentsDetailsCommand extends Command { $this ->setName('torrents:details') - ->setDescription('Show torrent details') - ->addArgument('id', InputArgument::REQUIRED, 'Torrent id') - ->setHelp("The %command.name% show torrent details"); + ->setDescription('Show a torrent details') + ->addArgument('id', InputArgument::REQUIRED, 'Torrent ID') + ->setHelp("%command.name% + +Show torrent details. + +Usage: torrents:details TORRENT_ID"); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Console/Command/TorrentsDownloadCommand.php b/src/Console/Command/TorrentsDownloadCommand.php index 56418f7..a5faa4c 100644 --- a/src/Console/Command/TorrentsDownloadCommand.php +++ b/src/Console/Command/TorrentsDownloadCommand.php @@ -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 %command.name% download torrent"); + ->addArgument('id', InputArgument::REQUIRED, 'Torrent ID') + ->addArgument('output_file', InputArgument::REQUIRED, 'Output') + ->setHelp("%command.name% + +Download a torrent. + +Usage: torrents:download TORRENT_ID OUTPUT + +OUTPUT could be a file or STDIN by using -."); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php index fbf87a1..1f8954b 100644 --- a/src/Console/Command/TorrentsSearchCommand.php +++ b/src/Console/Command/TorrentsSearchCommand.php @@ -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 %command.name% 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("%command.name% + +Search torrents. + +Usage: torrents:search QUERY [OPTIONS] + +--terms does not work (API bug)"); } 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( + '%s (%d)', + $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. %s', $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( '[%4d%6d] %9d %s', $torrent['seeders'], diff --git a/src/Console/Command/TorrentsTopCommand.php b/src/Console/Command/TorrentsTopCommand.php index 68e9170..29fa91c 100644 --- a/src/Console/Command/TorrentsTopCommand.php +++ b/src/Console/Command/TorrentsTopCommand.php @@ -17,8 +17,14 @@ class TorrentsTopCommand extends Command $this ->setName('torrents:top') ->setDescription('Top torrents') - ->addOption('period', 'p', InputOption::VALUE_OPTIONAL, 'Period') - ->setHelp("The %command.name% show top torrents"); + ->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period') + ->setHelp("%command.name% + +Show top torrents. + +Usage: torrents:search:top [OPTIONS] + +Period values: \"100\", \"day\", \"week\", \"month\""); } protected function execute(InputInterface $input, OutputInterface $output) diff --git a/src/Console/Command/TypesTreeCommand.php b/src/Console/Command/TypesTreeCommand.php index 9464785..09ad92d 100644 --- a/src/Console/Command/TypesTreeCommand.php +++ b/src/Console/Command/TypesTreeCommand.php @@ -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 %command.name% show the terms"); + ->setHelp("%command.name% + +List all types of terms and terms. + +Usage: types:tree [OPTIONS]"); } protected function execute(InputInterface $input, OutputInterface $output)