From 985e3ebbea646584b14d33ca52e9d14ee19f55b1 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 18 Nov 2015 19:14:21 +0100 Subject: [PATCH 1/2] Helper: render torrents list --- src/Console/Command/TorrentsSearchCommand.php | 13 ++++++- src/Console/Command/TorrentsTopCommand.php | 38 ++++++------------- src/Helper/Render.php | 34 +++++++++++++++++ 3 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 src/Helper/Render.php diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php index 9baafdb..07b0110 100644 --- a/src/Console/Command/TorrentsSearchCommand.php +++ b/src/Console/Command/TorrentsSearchCommand.php @@ -12,6 +12,7 @@ use Api\ConfigLoader; use Api\ClientResponse; use Api\ClientException; use Helper\Formater; +use Helper\Render; class TorrentsSearchCommand extends Command { @@ -84,8 +85,18 @@ Usage: torrents:search QUERY [OPTIONS] } $response = $this->searchTorrents($client, $input, $termsTree); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); - $this->showResults($response, $output); + return; + } + + Render::torrents($response->getData()['torrents'], $output); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } diff --git a/src/Console/Command/TorrentsTopCommand.php b/src/Console/Command/TorrentsTopCommand.php index 7123d82..0d8770b 100644 --- a/src/Console/Command/TorrentsTopCommand.php +++ b/src/Console/Command/TorrentsTopCommand.php @@ -10,6 +10,7 @@ use Api\Client; use Api\ConfigLoader; use Api\ClientResponse; use Api\ClientException; +use Helper\Render; class TorrentsTopCommand extends Command { @@ -49,35 +50,20 @@ Usage: torrents:search:top [OPTIONS] } $response = $client->getTopTorrents($period); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); - return $this->showResults($response, $output); + return; + } + + Render::torrents($response->getData(), $output); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } } - - 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() as $torrent) { - $output->writeln(sprintf( - '[%4d%6d] %9d %s', - $torrent['seeders'], - $torrent['leechers'], - $torrent['id'], - $torrent['name'] - )); - } - } } diff --git a/src/Helper/Render.php b/src/Helper/Render.php new file mode 100644 index 0000000..3387c7e --- /dev/null +++ b/src/Helper/Render.php @@ -0,0 +1,34 @@ + + */ +class Render +{ + public static function torrents(array $torrents, OutputInterface $output) + { + if (empty($torrents)) { + return; + } + + $output->writeln(' SEED LEECH SIZE ID NAME'); + + foreach ($torrents as $torrent) { + $output->writeln(sprintf( + '[%4d%6d] [%8s] %7d %s', + $torrent['seeders'], + $torrent['leechers'], + Formater::humanSize((int) $torrent['size']), + $torrent['id'], + $torrent['name'] + )); + } + } +} + From 9713b92674b86d807bb9935716a74c3436d013af Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Wed, 18 Nov 2015 20:53:37 +0100 Subject: [PATCH 2/2] Sorting option --- README.md | 5 +++- src/Console/Command/TorrentsSearchCommand.php | 16 ++++++++++- .../Command/TorrentsSearchMoviesCommand.php | 4 ++- .../Command/TorrentsSearchSeriesCommand.php | 4 ++- src/Console/Command/TorrentsTopCommand.php | 18 +++++++++++-- src/Helper/Render.php | 27 ++++++++++++++++--- 6 files changed, 65 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0bb5071..5f68fe3 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,8 @@ Help: Search torrents. Usage: torrents:search QUERY [OPTIONS] + + Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\" --terms does not work (API bug) ``` @@ -272,7 +274,8 @@ Help: Usage: torrents:search:top [OPTIONS] - Period values: "100", "day", "week", "month" + Period values \"100\" (default), \"day\", \"week\", \"month\" + Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\" ``` diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php index 07b0110..dc47f92 100644 --- a/src/Console/Command/TorrentsSearchCommand.php +++ b/src/Console/Command/TorrentsSearchCommand.php @@ -27,12 +27,16 @@ class TorrentsSearchCommand extends Command ->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 ",")') + ->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort') + ->addOption('asc', null, InputOption::VALUE_NONE, 'Ascending sort') ->setHelp("%command.name% Search torrents. Usage: torrents:search QUERY [OPTIONS] +Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\" + --terms does not work (API bug)"); } @@ -95,8 +99,18 @@ Usage: torrents:search QUERY [OPTIONS] return; } + + $options = []; + + if ($input->getOption('sort')) { + $options['sort'] = $input->getOption('sort'); + } + + if ($input->getOption('asc')) { + $options['asc'] = true; + } - Render::torrents($response->getData()['torrents'], $output); + Render::torrents($response->getData()['torrents'], $output, $options); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } diff --git a/src/Console/Command/TorrentsSearchMoviesCommand.php b/src/Console/Command/TorrentsSearchMoviesCommand.php index 9e3c431..222b3e6 100644 --- a/src/Console/Command/TorrentsSearchMoviesCommand.php +++ b/src/Console/Command/TorrentsSearchMoviesCommand.php @@ -20,6 +20,8 @@ class TorrentsSearchMoviesCommand extends Command ->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number') ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page') ->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")') + ->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort') + ->addOption('asc', null, InputOption::VALUE_NONE, 'Ascending sort') ->setHelp("%command.name% Search movies. @@ -37,7 +39,7 @@ Usage: torrents:search:movies QUERY [OPTIONS] '--sub-category' => 631, ); - foreach (['offset', 'limit', 'terms'] as $p) { + foreach (['offset', 'limit', 'terms', 'sort', 'asc'] as $p) { $value = $input->getOption($p); if (null !== $value) { diff --git a/src/Console/Command/TorrentsSearchSeriesCommand.php b/src/Console/Command/TorrentsSearchSeriesCommand.php index ccb2dc3..e8def61 100644 --- a/src/Console/Command/TorrentsSearchSeriesCommand.php +++ b/src/Console/Command/TorrentsSearchSeriesCommand.php @@ -20,6 +20,8 @@ class TorrentsSearchSeriesCommand extends Command ->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number') ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page') ->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")') + ->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort') + ->addOption('asc', null, InputOption::VALUE_NONE, 'Ascending sort') ->setHelp("%command.name% Search series. @@ -37,7 +39,7 @@ Usage: torrents:search:series QUERY [OPTIONS] '--sub-category' => 433, ); - foreach (['offset', 'limit', 'terms'] as $p) { + foreach (['offset', 'limit', 'terms', 'sort', 'asc'] as $p) { $value = $input->getOption($p); if (null !== $value) { diff --git a/src/Console/Command/TorrentsTopCommand.php b/src/Console/Command/TorrentsTopCommand.php index 0d8770b..8e1023c 100644 --- a/src/Console/Command/TorrentsTopCommand.php +++ b/src/Console/Command/TorrentsTopCommand.php @@ -20,13 +20,17 @@ class TorrentsTopCommand extends Command ->setName('torrents:top') ->setDescription('Top torrents') ->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period') + ->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort') + ->addOption('asc', null, InputOption::VALUE_NONE, 'Ascending sort') ->setHelp("%command.name% Show top torrents. Usage: torrents:search:top [OPTIONS] -Period values: \"100\" (default), \"day\", \"week\", \"month\""); +Period values \"100\" (default), \"day\", \"week\", \"month\" +Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\" +"); } protected function execute(InputInterface $input, OutputInterface $output) @@ -61,7 +65,17 @@ Usage: torrents:search:top [OPTIONS] return; } - Render::torrents($response->getData(), $output); + $options = []; + + if ($input->getOption('sort')) { + $options['sort'] = $input->getOption('sort'); + } + + if ($input->getOption('asc')) { + $options['asc'] = true; + } + + Render::torrents($response->getData(), $output, $options); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } diff --git a/src/Helper/Render.php b/src/Helper/Render.php index 3387c7e..5fdb68c 100644 --- a/src/Helper/Render.php +++ b/src/Helper/Render.php @@ -3,7 +3,7 @@ namespace Helper; use Symfony\Component\Console\Output\OutputInterface; -use Api\ClientResponse; +use InvalidArgumentException; /** * Class Render @@ -11,12 +11,34 @@ use Api\ClientResponse; */ class Render { - public static function torrents(array $torrents, OutputInterface $output) + public static function torrents(array $torrents, OutputInterface $output, array $options = []) { if (empty($torrents)) { return; } + if (isset($options['sort'])) { + $sort = $options['sort']; + + if (!in_array($sort, ['seed', 'leech', 'size', 'id', 'name'])) { + throw new InvalidArgumentException('Invalid option "sort".'); + } + + $sort = str_replace(['seed', 'leech'], ['seeders', 'leechers'], $sort); + $sortDatas = []; + + foreach ($torrents as $torrent) { + $sortDatas[] = $torrent[$sort]; + } + + array_multisort( + $sortDatas, + isset($options['asc']) ? SORT_ASC : SORT_DESC, + $sort === 'name' ? SORT_STRING : SORT_NUMERIC, + $torrents + ); + } + $output->writeln(' SEED LEECH SIZE ID NAME'); foreach ($torrents as $torrent) { @@ -31,4 +53,3 @@ class Render } } } -