diff --git a/src/Api/Client.php b/src/Api/Client.php index c2566ba..b6ce1e7 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -75,6 +75,11 @@ class Client return $this->get(true, '/torrents/details/'.$id); } + public function getTopTorrents($period) + { + return $this->get(true, '/torrents/top/'.$period); + } + public function get($needAuthorization, $uri, array $options = array()) { return $this->send($needAuthorization, 'get', $uri, $options); diff --git a/src/Console/Application.php b/src/Console/Application.php index a14f715..a5e434e 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -40,5 +40,4 @@ class Application extends BaseApplication } } } - } diff --git a/src/Console/Command/TorrentsTopCommand.php b/src/Console/Command/TorrentsTopCommand.php new file mode 100644 index 0000000..68e9170 --- /dev/null +++ b/src/Console/Command/TorrentsTopCommand.php @@ -0,0 +1,76 @@ +setName('torrents:top') + ->setDescription('Top torrents') + ->addOption('period', 'p', InputOption::VALUE_OPTIONAL, 'Period') + ->setHelp("The %command.name% show top torrents"); + } + + 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); + + return $this->showResults($response, $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'] + )); + } + } +}