diff --git a/src/Api/Client.php b/src/Api/Client.php
index 1adeb7f..8059609 100644
--- a/src/Api/Client.php
+++ b/src/Api/Client.php
@@ -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);
diff --git a/src/Api/ClientResponse.php b/src/Api/ClientResponse.php
index e374be7..92f6b8a 100644
--- a/src/Api/ClientResponse.php
+++ b/src/Api/ClientResponse.php
@@ -40,4 +40,9 @@ class ClientResponse
{
return $this->response->json();
}
+
+ public function getBody()
+ {
+ return $this->response->getBody();
+ }
}
diff --git a/src/Console/Command/TorrentsDownloadCommand.php b/src/Console/Command/TorrentsDownloadCommand.php
new file mode 100644
index 0000000..56418f7
--- /dev/null
+++ b/src/Console/Command/TorrentsDownloadCommand.php
@@ -0,0 +1,69 @@
+setName('torrents:download')
+ ->setDescription('Download a torrent')
+ ->addArgument('id', InputArgument::REQUIRED, 'Torrent id')
+ ->addArgument('output_file', InputArgument::REQUIRED, 'Output file')
+ ->setHelp("The %command.name% 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(
+ '%s (%d)',
+ $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. %s', $e->getMessage()));
+ }
+ }
+}
diff --git a/src/Console/Command/TorrentsSearchCommand.php b/src/Console/Command/TorrentsSearchCommand.php
new file mode 100644
index 0000000..d964b5c
--- /dev/null
+++ b/src/Console/Command/TorrentsSearchCommand.php
@@ -0,0 +1,79 @@
+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 %command.name% 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(
+ '%s (%d)',
+ $response->getErrorMessage(),
+ $response->getErrorCode()
+ ));
+
+ return;
+ }
+
+ $output->writeln(' ID NAME');
+
+ foreach ($response->getData()['torrents'] as $torrent) {
+ $output->writeln(sprintf(
+ '[%4d%4d] %9d %s',
+ $torrent['seeders'],
+ $torrent['leechers'],
+ $torrent['id'],
+ $torrent['name']
+ ));
+ }
+ } catch (ClientException $e) {
+ $output->writeln(sprintf('An error occured. %s', $e->getMessage()));
+ }
+ }
+}