torrent basic search and torrent download

This commit is contained in:
Simon Vieille 2015-02-10 03:36:06 +01:00
parent cadc2c6edd
commit abd88f66a8
4 changed files with 174 additions and 0 deletions

View file

@ -45,6 +45,27 @@ class Client
return $this->get(true, '/terms/tree');
}
public function searchTorrents($query, $options)
{
$url = '/torrents/search/'.urlencode($query);
$query = array();
foreach (array('offset', 'limit') as $p) {
if (!empty($options[$p])) {
$query[$p] = $options[$p];
}
}
$url.= '?'.http_build_query($query);
return $this->get(true, $url);
}
public function downloadTorrent($id)
{
return $this->get(true, '/torrents/download/'.$id);
}
public function get($needAuthorization, $uri, array $options = array())
{
return $this->send($needAuthorization, 'get', $uri, $options);

View file

@ -40,4 +40,9 @@ class ClientResponse
{
return $this->response->json();
}
public function getBody()
{
return $this->response->getBody();
}
}

View file

@ -0,0 +1,69 @@
<?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;
use GuzzleHttp\Exception\ParseException;
use Symfony\Component\Filesystem\Filesystem;
class TorrentsDownloadCommand extends Command
{
protected function configure()
{
$this
->setName('torrents:download')
->setDescription('Download a torrent')
->addArgument('id', InputArgument::REQUIRED, 'Torrent id')
->addArgument('output_file', InputArgument::REQUIRED, 'Output file')
->setHelp("The <info>%command.name%</info> download torrent");
}
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->downloadTorrent($input->getArgument('id'));
try {
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
} catch (ParseException $e) {
}
$outputFile = $input->getArgument('output_file');
if ($outputFile === '-') {
echo $response->getBody();
} else {
$filesystem = new Filesystem();
$filesystem->dumpFile($outputFile, $response->getBody());
$output->writeln(sprintf('Torrent saved in %s', $outputFile));
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
}

View file

@ -0,0 +1,79 @@
<?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\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
class TorrentsSearchCommand extends Command
{
protected function configure()
{
$this
->setName('torrents:search')
->setDescription('Search torrents')
->addArgument('query', InputArgument::REQUIRED, 'Query')
->addOption('offset', null, InputOption::VALUE_OPTIONAL, 'Search offset')
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Search limit')
->addOption('category', null, InputOption::VALUE_OPTIONAL, 'Category')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Type')
->addOption('term', null, InputOption::VALUE_OPTIONAL, 'Term')
->setHelp("The <info>%command.name%</info> search 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 {
$response = $client->searchTorrents(
$input->getArgument('query'),
array(
'offset' => (int) $input->getOption('offset'),
'limit' => (int) $input->getOption('limit'),
'category' => $input->getOption('category'),
'type' => $input->getOption('type'),
'term' => $input->getOption('term'),
)
);
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$output->writeln(' ID NAME');
foreach ($response->getData()['torrents'] as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%4d</comment>] %9d %s',
$torrent['seeders'],
$torrent['leechers'],
$torrent['id'],
$torrent['name']
));
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
}