2015-02-15 21:39:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Transmission\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\ConfigLoader;
|
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Vohof\Transmission;
|
|
|
|
use Transmission\Client\GuzzleClient;
|
|
|
|
|
|
|
|
class TransmissionDownloadCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('transmission:download')
|
|
|
|
->setDescription('Download a torrent')
|
|
|
|
->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
|
|
|
|
->setHelp("<info>%command.name%</info>
|
|
|
|
|
|
|
|
Download a torrent.
|
|
|
|
|
2016-05-09 23:08:44 +02:00
|
|
|
Usage: <comment>%command.name%</comment> <info>TORRENT_ID</info>");
|
2015-02-15 21:39:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$configLoader = new ConfigLoader();
|
|
|
|
|
|
|
|
if (!isset($configLoader->getConfig()['transmission'])) {
|
|
|
|
$output->writeln('No configuration found.');
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$config = $configLoader->getConfig()['transmission'];
|
|
|
|
|
2016-08-29 01:11:55 +02:00
|
|
|
$outputFile = sprintf(
|
|
|
|
'%s/t411-console-%d',
|
|
|
|
sys_get_temp_dir(),
|
|
|
|
time()
|
|
|
|
);
|
2015-02-15 21:39:44 +01:00
|
|
|
|
|
|
|
$inputData = array(
|
|
|
|
'command' => 'torrents:download',
|
|
|
|
'id' => $input->getArgument('id'),
|
|
|
|
'output_file' => $outputFile,
|
|
|
|
'-q' => true,
|
|
|
|
);
|
|
|
|
|
|
|
|
$options = [];
|
|
|
|
|
2015-03-05 15:04:08 +01:00
|
|
|
if (!empty($config['username']) && !empty($config['password'])) {
|
2015-02-15 21:39:44 +01:00
|
|
|
$options = array(
|
|
|
|
'request.options' => array(
|
|
|
|
'auth' => array($config['username'], $config['password'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new GuzzleClient($config['host'], $options);
|
|
|
|
|
|
|
|
$transmission = new Transmission($configLoader->getConfig()['transmission'], $client);
|
|
|
|
|
|
|
|
$this->getApplication()->doRun(new ArrayInput($inputData), $output);
|
|
|
|
|
|
|
|
$content = base64_encode(file_get_contents($outputFile));
|
|
|
|
|
2015-03-05 15:04:08 +01:00
|
|
|
$transmission->add($content, true);
|
2015-02-15 21:39:44 +01:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
unlink($outputFile);
|
|
|
|
|
|
|
|
$output->writeln(sprintf(
|
|
|
|
'An error occured. <error>%s</error>',
|
|
|
|
$e->getMessage()
|
|
|
|
));
|
|
|
|
|
|
|
|
$output->writeln(sprintf('Torrent %s removed', $outputFile));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink($outputFile);
|
|
|
|
|
|
|
|
$output->writeln(sprintf('Download <info>started</info>.', $outputFile));
|
|
|
|
$output->writeln(sprintf('Torrent %s removed', $outputFile));
|
|
|
|
}
|
|
|
|
}
|