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\"
Sort values \"seed\", \"leech\", \"size\", \"name\", \"id\"
");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$client = new Client();
$configLoader = new ConfigLoader();
if (!isset($configLoader->getConfig()['auth']['token'])) {
$output->writeln('You must login.');
return;
}
$client->setAuthorization($configLoader->getConfig()['auth']['token']);
try {
$period = $input->getOption('period');
if (!in_array($period, ['100', 'today', 'week', 'month'])) {
$period = '100';
}
$response = $client->getTopTorrents($period);
if ($response->hasError()) {
$output->writeln(sprintf(
'%s (%d)',
$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) {
$output->writeln(sprintf('An error occured. %s', $e->getMessage()));
}
}
}