setName('torrents:search') ->setDescription('Search torrents') ->addArgument('query', InputArgument::REQUIRED, 'Query') ->addOption('offset', 'o', InputOption::VALUE_REQUIRED, 'Page number') ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of results per page') ->addOption('sub-category', 's', InputOption::VALUE_REQUIRED, 'Filter by sub-category ID') ->addOption('category', 'c', InputOption::VALUE_REQUIRED, 'Filter by category ID') ->addOption('terms', 't', InputOption::VALUE_REQUIRED, 'Filter by terms IDs (separated by ",")') ->setHelp("%command.name% Search torrents. Usage: torrents:search QUERY [OPTIONS] --terms does not work (API bug)"); } 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 { $categoryId = (int) $input->getOption('category'); $termsTree = $client->getTermsTree(); /** * API HACK * Category filter does not work */ if (!empty($categoryId)) { $categoriesResponse = $client->getCategoriesTree(); if ($categoriesResponse->hasError()) { $output->writeln(sprintf( '%s (%d)', $response->getErrorMessage(), $response->getErrorCode() )); return; } foreach ($categoriesResponse->getData() as $category) { if (isset($category['id']) && (int) $category['id'] === $categoryId) { foreach (array_keys($category['cats']) as $cid) { $response = $this->searchTorrents($client, $input, $termsTree, $cid); $this->showResults($response, $output); $output->writeln(''); } } } return; } $response = $this->searchTorrents($client, $input, $termsTree); $this->showResults($response, $output); } catch (ClientException $e) { $output->writeln(sprintf('An error occured. %s', $e->getMessage())); } } protected function searchTorrents(Client $client, InputInterface $input, ClientResponse $termsTree, $cid = null) { return $client->searchTorrents( $input->getArgument('query'), array( 'offset' => (int) $input->getOption('offset'), 'limit' => (int) $input->getOption('limit'), 'cid' => $cid !== null ? $cid : (int) $input->getOption('sub-category'), 'terms' => $this->convertTerms($input->getOption('terms'), $termsTree->getData()), ) ); } public function convertTerms($value, array $termTypesTree) { $value = trim($value); $terms = array_map( function ($v) { return (int) trim($v); }, explode(',', $value) ); $finalTerms = array(); foreach ($termTypesTree as $termTypes) { foreach ($termTypes as $termTypeId => $termType) { foreach ($terms as $term) { if (isset($termType['terms'][$term])) { if (!isset($finalTerms[$termTypeId])) { $finalTerms[$termTypeId] = []; } if (!in_array($term, $finalTerms[$termTypeId])) { $finalTerms[$termTypeId][] = $term; } } } } } return $finalTerms; } protected function showResults(ClientResponse $response, OutputInterface $output) { if ($response->hasError()) { $output->writeln(sprintf( '%s (%d)', $response->getErrorMessage(), $response->getErrorCode() )); return; } $torrents = $response->getData()['torrents']; if (empty($torrents)) { return; } $output->writeln(' SEED LEECH SIZE ID NAME'); foreach ($torrents as $torrent) { $output->writeln(sprintf( '[%4d%6d] [%8s] %7d %s', $torrent['seeders'], $torrent['leechers'], Formater::humanSize((int) $torrent['size']), $torrent['id'], $torrent['name'] )); } } }