forked from deblan/t411-console
70 lines
2.2 KiB
PHP
70 lines
2.2 KiB
PHP
<?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()));
|
|
}
|
|
}
|
|
}
|