From 6b5767ac2d3164cf003444d37044ad41f1e1ff21 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 10 Feb 2015 09:53:19 +0100 Subject: [PATCH] torrent details command --- src/Api/Client.php | 5 ++ .../Command/TorrentsDetailsCommand.php | 73 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 src/Console/Command/TorrentsDetailsCommand.php diff --git a/src/Api/Client.php b/src/Api/Client.php index 48c93f9..00060b1 100644 --- a/src/Api/Client.php +++ b/src/Api/Client.php @@ -66,6 +66,11 @@ class Client return $this->get(true, '/torrents/download/'.$id); } + public function getTorrentDetails($id) + { + return $this->get(true, '/torrents/details/'.$id); + } + public function get($needAuthorization, $uri, array $options = array()) { return $this->send($needAuthorization, 'get', $uri, $options); diff --git a/src/Console/Command/TorrentsDetailsCommand.php b/src/Console/Command/TorrentsDetailsCommand.php new file mode 100644 index 0000000..b273319 --- /dev/null +++ b/src/Console/Command/TorrentsDetailsCommand.php @@ -0,0 +1,73 @@ +setName('torrents:details') + ->setDescription('Show torrent details') + ->addArgument('id', InputArgument::REQUIRED, 'Torrent id') + ->setHelp("The %command.name% show torrent details"); + } + + 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 { + $response = $client->getTorrentDetails($input->getArgument('id')); + + if ($response->hasError()) { + $output->writeln(sprintf( + '%s (%d)', + $response->getErrorMessage(), + $response->getErrorCode() + )); + + return; + } + + $data = $response->getData(); + + $output->writeln(sprintf('%s', $data['name'])); + $output->writeln(''); + $output->writeln(sprintf('Category : %s', $data['categoryname'])); + + foreach ($data['terms'] as $title => $value) { + $output->writeln(sprintf('%-16s: %s', $title, $value)); + } + + $output->writeln(''); + $output->writeln($this->parseDescription($data['description'])); + } catch (ClientException $e) { + $output->writeln(sprintf('An error occured. %s', $e->getMessage())); + } + } + + protected function parseDescription($description) + { + $description = str_replace('
', PHP_EOL, $description); + $description = trim(html_entity_decode(strip_tags($description))); + + return $description; + } +}