forked from deblan/t411-console
Merge branch 'dev-master'
This commit is contained in:
commit
b2991e6a9f
|
@ -183,6 +183,8 @@ Help:
|
||||||
|
|
||||||
Usage: torrents:search QUERY [OPTIONS]
|
Usage: torrents:search QUERY [OPTIONS]
|
||||||
|
|
||||||
|
Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\"
|
||||||
|
|
||||||
--terms does not work (API bug)
|
--terms does not work (API bug)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -274,7 +276,8 @@ Help:
|
||||||
|
|
||||||
Usage: torrents:search:top [OPTIONS]
|
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\"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ use Api\ConfigLoader;
|
||||||
use Api\ClientResponse;
|
use Api\ClientResponse;
|
||||||
use Api\ClientException;
|
use Api\ClientException;
|
||||||
use Helper\Formater;
|
use Helper\Formater;
|
||||||
|
use Helper\Render;
|
||||||
|
|
||||||
class TorrentsSearchCommand extends Command
|
class TorrentsSearchCommand extends Command
|
||||||
{
|
{
|
||||||
|
@ -26,12 +27,16 @@ class TorrentsSearchCommand extends Command
|
||||||
->addOption('sub-category', 's', InputOption::VALUE_REQUIRED, 'Filter by sub-category ID')
|
->addOption('sub-category', 's', InputOption::VALUE_REQUIRED, 'Filter by sub-category ID')
|
||||||
->addOption('category', 'c', InputOption::VALUE_REQUIRED, 'Filter by 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('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("<info>%command.name%</info>
|
->setHelp("<info>%command.name%</info>
|
||||||
|
|
||||||
Search torrents.
|
Search torrents.
|
||||||
|
|
||||||
Usage: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
|
Usage: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
|
||||||
|
|
||||||
|
<info>Sort values</info> \"seed\", \"leech\", \"size\", \"name\", \"id\"
|
||||||
|
|
||||||
<error>--terms does not work (API bug)</error>");
|
<error>--terms does not work (API bug)</error>");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +90,27 @@ Usage: <comment>torrents:search</comment> <info>QUERY</info> [OPTIONS]
|
||||||
|
|
||||||
$response = $this->searchTorrents($client, $input, $termsTree);
|
$response = $this->searchTorrents($client, $input, $termsTree);
|
||||||
|
|
||||||
$this->showResults($response, $output);
|
if ($response->hasError()) {
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'<error>%s</error> <comment>(%d)</comment>',
|
||||||
|
$response->getErrorMessage(),
|
||||||
|
$response->getErrorCode()
|
||||||
|
));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$options = [];
|
||||||
|
|
||||||
|
if ($input->getOption('sort')) {
|
||||||
|
$options['sort'] = $input->getOption('sort');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($input->getOption('asc')) {
|
||||||
|
$options['asc'] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
Render::torrents($response->getData()['torrents'], $output, $options);
|
||||||
} 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()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,8 @@ class TorrentsSearchMoviesCommand extends Command
|
||||||
->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
|
->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
|
||||||
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
|
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
|
||||||
->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")')
|
->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("<info>%command.name%</info>
|
->setHelp("<info>%command.name%</info>
|
||||||
|
|
||||||
Search movies.
|
Search movies.
|
||||||
|
@ -37,7 +39,7 @@ Usage: <comment>torrents:search:movies</comment> <info>QUERY</info> [OPTIONS]
|
||||||
'--sub-category' => 631,
|
'--sub-category' => 631,
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (['offset', 'limit', 'terms'] as $p) {
|
foreach (['offset', 'limit', 'terms', 'sort', 'asc'] as $p) {
|
||||||
$value = $input->getOption($p);
|
$value = $input->getOption($p);
|
||||||
|
|
||||||
if (null !== $value) {
|
if (null !== $value) {
|
||||||
|
|
|
@ -20,6 +20,8 @@ class TorrentsSearchSeriesCommand extends Command
|
||||||
->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
|
->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number')
|
||||||
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
|
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page')
|
||||||
->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")')
|
->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("<info>%command.name%</info>
|
->setHelp("<info>%command.name%</info>
|
||||||
|
|
||||||
Search series.
|
Search series.
|
||||||
|
@ -37,7 +39,7 @@ Usage: <comment>torrents:search:series</comment> <info>QUERY</info> [OPTIONS]
|
||||||
'--sub-category' => 433,
|
'--sub-category' => 433,
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach (['offset', 'limit', 'terms'] as $p) {
|
foreach (['offset', 'limit', 'terms', 'sort', 'asc'] as $p) {
|
||||||
$value = $input->getOption($p);
|
$value = $input->getOption($p);
|
||||||
|
|
||||||
if (null !== $value) {
|
if (null !== $value) {
|
||||||
|
|
|
@ -10,6 +10,7 @@ use Api\Client;
|
||||||
use Api\ConfigLoader;
|
use Api\ConfigLoader;
|
||||||
use Api\ClientResponse;
|
use Api\ClientResponse;
|
||||||
use Api\ClientException;
|
use Api\ClientException;
|
||||||
|
use Helper\Render;
|
||||||
|
|
||||||
class TorrentsTopCommand extends Command
|
class TorrentsTopCommand extends Command
|
||||||
{
|
{
|
||||||
|
@ -19,13 +20,17 @@ class TorrentsTopCommand extends Command
|
||||||
->setName('torrents:top')
|
->setName('torrents:top')
|
||||||
->setDescription('Top torrents')
|
->setDescription('Top torrents')
|
||||||
->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period')
|
->addOption('period', 'p', InputOption::VALUE_REQUIRED, 'Period')
|
||||||
|
->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Sort')
|
||||||
|
->addOption('asc', null, InputOption::VALUE_NONE, 'Ascending sort')
|
||||||
->setHelp("<info>%command.name%</info>
|
->setHelp("<info>%command.name%</info>
|
||||||
|
|
||||||
Show top torrents.
|
Show top torrents.
|
||||||
|
|
||||||
Usage: <comment>torrents:search:top</comment> [OPTIONS]
|
Usage: <comment>torrents:search:top</comment> [OPTIONS]
|
||||||
|
|
||||||
<info>Period values: \"100\" (default), \"day\", \"week\", \"month\"</info>");
|
<info>Period values</info> \"100\" (default), \"day\", \"week\", \"month\"
|
||||||
|
<info>Sort values</info> \"seed\", \"leech\", \"size\", \"name\", \"id\"
|
||||||
|
");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
|
@ -50,34 +55,29 @@ Usage: <comment>torrents:search:top</comment> [OPTIONS]
|
||||||
|
|
||||||
$response = $client->getTopTorrents($period);
|
$response = $client->getTopTorrents($period);
|
||||||
|
|
||||||
return $this->showResults($response, $output);
|
if ($response->hasError()) {
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'<error>%s</error> <comment>(%d)</comment>',
|
||||||
|
$response->getErrorMessage(),
|
||||||
|
$response->getErrorCode()
|
||||||
|
));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$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) {
|
} 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 showResults(ClientResponse $response, OutputInterface $output)
|
|
||||||
{
|
|
||||||
if ($response->hasError()) {
|
|
||||||
$output->writeln(sprintf(
|
|
||||||
'<error>%s</error> <comment>(%d)</comment>',
|
|
||||||
$response->getErrorMessage(),
|
|
||||||
$response->getErrorCode()
|
|
||||||
));
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$output->writeln(' SEED LEECH ID NAME');
|
|
||||||
|
|
||||||
foreach ($response->getData() as $torrent) {
|
|
||||||
$output->writeln(sprintf(
|
|
||||||
'[<info>%4d</info><comment>%6d</comment>] %9d %s',
|
|
||||||
$torrent['seeders'],
|
|
||||||
$torrent['leechers'],
|
|
||||||
$torrent['id'],
|
|
||||||
$torrent['name']
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
55
src/Helper/Render.php
Normal file
55
src/Helper/Render.php
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Helper;
|
||||||
|
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Render
|
||||||
|
* @author Simon Vieille <simon@deblan.fr>
|
||||||
|
*/
|
||||||
|
class Render
|
||||||
|
{
|
||||||
|
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) {
|
||||||
|
$output->writeln(sprintf(
|
||||||
|
'[<info>%4d</info><comment>%6d</comment>] [%8s] %7d %s',
|
||||||
|
$torrent['seeders'],
|
||||||
|
$torrent['leechers'],
|
||||||
|
Formater::humanSize((int) $torrent['size']),
|
||||||
|
$torrent['id'],
|
||||||
|
$torrent['name']
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue