setName('transmission:download')
->setDescription('Download a torrent')
->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
->setHelp("%command.name%
Download a torrent.
Usage: %command.name% TORRENT_ID");
}
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'];
$outputFile = sprintf(
'%s/t411-console-%d',
sys_get_temp_dir(),
time()
);
$inputData = array(
'command' => 'torrents:download',
'id' => $input->getArgument('id'),
'output_file' => $outputFile,
'-q' => true,
);
$options = [];
if (!empty($config['username']) && !empty($config['password'])) {
$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));
$transmission->add($content, true);
} catch (\Exception $e) {
unlink($outputFile);
$output->writeln(sprintf(
'An error occured. %s',
$e->getMessage()
));
$output->writeln(sprintf('Torrent %s removed', $outputFile));
return;
}
unlink($outputFile);
$output->writeln(sprintf('Download started.', $outputFile));
$output->writeln(sprintf('Torrent %s removed', $outputFile));
}
}