diff --git a/composer.json b/composer.json index d6cdc98..b8a4650 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "symfony/finder": "~2.6", "symfony/yaml": "~2.6", "symfony/filesystem": "3.0.x-dev", - "guzzlehttp/guzzle": "~5.2" + "guzzlehttp/guzzle": "~5.2", + "vohof/transmission": "~1.0" } } diff --git a/console b/console index f682396..38e88c4 100755 --- a/console +++ b/console @@ -7,6 +7,8 @@ use Console\Application; $app = new Application(); -$app->chdir(__DIR__); -$app->loadCommands(); -$app->run(); +$app + ->chdir(__DIR__) + ->addCommandsPath('src/Transmission/Command', 'Transmission\\Command') + ->loadCommands() + ->run(); diff --git a/src/Console/Application.php b/src/Console/Application.php index f32bba6..00ff196 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -28,7 +28,7 @@ class Application extends BaseApplication public function loadCommands() { foreach ($this->commandsPaths as $path => $namespace) { - $finder = new Finder(); + $finder = new Finder(); $finder->name('*Command.php')->in($path); foreach ($finder as $file) { diff --git a/src/Transmission/Client/GuzzleClient.php b/src/Transmission/Client/GuzzleClient.php new file mode 100644 index 0000000..633b18c --- /dev/null +++ b/src/Transmission/Client/GuzzleClient.php @@ -0,0 +1,15 @@ +client->setDefaultOption('verify', false); + } +} diff --git a/src/Transmission/Command/TransmissionConfigureCommand.php b/src/Transmission/Command/TransmissionConfigureCommand.php new file mode 100644 index 0000000..025574b --- /dev/null +++ b/src/Transmission/Command/TransmissionConfigureCommand.php @@ -0,0 +1,47 @@ +setName('transmission:configure') + ->setDescription('') + // ->addArgument('foo', InputArgument::OPTIONAL, '') + // ->addOption('bar', null, InputOption::VALUE_NONE, '') + ->setHelp("%command.name% + +Configure your transmission web remote access. + +Usage: transmission:configure"); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $configLoader = new ConfigLoader(); + $dialog = $this->getHelperSet()->get('dialog'); + + $host = $dialog->ask($output, 'Host (eg: https://seedox.example.com/): ', null); + $endPoint = $dialog->ask($output, 'End point [/transmission/rpc]: ', null); + $username = $dialog->ask($output, 'Username: ', null); + $password = $dialog->askHiddenResponse($output, 'Password (hidden): ', null); + + $configLoader->save(array( + 'transmission' => array( + 'host' => $host, + 'endpoint' => $endPoint ? $endPoint : '/transmission/rpc', + 'username' => $username, + 'password' => $password, + ) + )); + } +} diff --git a/src/Transmission/Command/TransmissionDownloadCommand.php b/src/Transmission/Command/TransmissionDownloadCommand.php new file mode 100644 index 0000000..5d05a3c --- /dev/null +++ b/src/Transmission/Command/TransmissionDownloadCommand.php @@ -0,0 +1,88 @@ +setName('transmission:download') + ->setDescription('Download a torrent') + ->addArgument('id', InputArgument::REQUIRED, 'Torrent ID') + ->setHelp("%command.name% + +Download a torrent. + +Usage: transmission:download 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('.%d', time()); + + $inputData = array( + 'command' => 'torrents:download', + 'id' => $input->getArgument('id'), + 'output_file' => $outputFile, + '-q' => true, + ); + + $options = []; + + if (!empty($config['username']) and !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)); + + $torrent = $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)); + } +}