setName('torrents:download')
->setDescription('Download a torrent')
->addArgument('id', InputArgument::REQUIRED, 'Torrent ID')
->addArgument('output_file', InputArgument::REQUIRED, 'Output')
->setHelp("%command.name%
Download a torrent.
Usage: torrents:download TORRENT_ID OUTPUT
OUTPUT could be a file or STDIN by using -.");
}
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()));
}
}
}