torrent details command

This commit is contained in:
Simon Vieille 2015-02-10 09:53:19 +01:00
parent d748a0ea88
commit 6b5767ac2d
2 changed files with 78 additions and 0 deletions

View file

@ -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);

View file

@ -0,0 +1,73 @@
<?php
namespace Console\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
class TorrentsDetailsCommand extends Command
{
protected function configure()
{
$this
->setName('torrents:details')
->setDescription('Show torrent details')
->addArgument('id', InputArgument::REQUIRED, 'Torrent id')
->setHelp("The <info>%command.name%</info> 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(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$data = $response->getData();
$output->writeln(sprintf('<info>%s</info>', $data['name']));
$output->writeln('');
$output->writeln(sprintf('Category : <comment>%s</comment>', $data['categoryname']));
foreach ($data['terms'] as $title => $value) {
$output->writeln(sprintf('%-16s: <comment>%s</comment>', $title, $value));
}
$output->writeln('');
$output->writeln($this->parseDescription($data['description']));
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
protected function parseDescription($description)
{
$description = str_replace('<br>', PHP_EOL, $description);
$description = trim(html_entity_decode(strip_tags($description)));
return $description;
}
}