Refactoring, new render, search engine improved

This commit is contained in:
Simon Vieille 2015-02-10 13:41:11 +01:00
parent 6b5767ac2d
commit 784bf1c0ab
5 changed files with 186 additions and 111 deletions

View file

@ -50,12 +50,16 @@ class Client
$url = '/torrents/search/'.urlencode($query);
$query = array();
foreach (array('offset', 'limit') as $p) {
foreach (array('offset', 'limit', 'cat') as $p) {
if (!empty($options[$p])) {
$query[$p] = $options[$p];
}
}
if (!empty($options['terms'])) {
$query['terms'] = $options['terms'];
}
$url.= '?'.http_build_query($query);
return $this->get(true, $url);

View file

@ -45,17 +45,23 @@ class CategoriesTreeCommand extends Command
}
foreach ($response->getData() as $category) {
if (isset($category['name'])) {
$output->writeln(sprintf('`- %s', $category['name']));
if (!isset($category['name'])) {
$category['name'] = 'Category name not defined';
}
$output->writeln(sprintf('<comment>%3d</comment> <info>%s</info>', isset($category['id']) ? $category['id'] : null, $category['name']));
if (!empty($category['cats'])) {
$isFirst = true;
foreach ($category['cats'] as $subCategory) {
if (isset($subCategory['name'])) {
$output->writeln(sprintf(' |- %s', $subCategory['name']));
foreach ($category['cats'] as $subCategoryId => $subCategory) {
$char = '|';
if ($isFirst) {
$isFirst = false;
$char = '`';
}
$output->writeln(sprintf(' %s- <comment>%4d</comment> %s', $char, $subCategoryId, $subCategory['name']));
}
}

View file

@ -1,76 +0,0 @@
<?php
namespace Console\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
class TermsTreeCommand extends Command
{
protected function configure()
{
$this
->setName('terms:tree')
->setDescription('Show terms')
->setHelp("The <info>%command.name%</info> show the terms");
}
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 {
$categoriesReponse = $client->getCategoriesTree();
$termsResponse = $client->getTermsTree();
foreach (array($categoriesReponse, $termsResponse) as $response) {
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
}
foreach ($termsResponse->getData() as $categoryId => $termTypes) {
foreach ($categoriesReponse->getData() as $id => $category) {
if ($categoryId === $id) {
if (isset($category['name'])) {
$output->writeln(sprintf('`- %s', $category['name']));
} else {
$output->writeln(sprintf('`- %s', 'Not defined'));
}
}
foreach ($termTypes as $termType) {
if (isset($termType['type'])) {
$output->writeln(sprintf(' +- %s', $termType['type']));
}
foreach ($termType['terms'] as $term) {
$output->writeln(sprintf(' | %s', $term));
}
}
$output->writeln('');
}
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
}

View file

@ -9,6 +9,7 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
use Api\ClientResponse;
class TorrentsSearchCommand extends Command
{
@ -18,11 +19,10 @@ class TorrentsSearchCommand extends Command
->setName('torrents:search')
->setDescription('Search torrents')
->addArgument('query', InputArgument::REQUIRED, 'Query')
->addOption('offset', null, InputOption::VALUE_OPTIONAL, 'Search offset')
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'Search limit')
->addOption('category', null, InputOption::VALUE_OPTIONAL, 'Category')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'Type')
->addOption('term', null, InputOption::VALUE_OPTIONAL, 'Term')
->addOption('offset', 'o', InputOption::VALUE_OPTIONAL, 'Search offset')
->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Search limit')
->addOption('category', 'c', InputOption::VALUE_OPTIONAL, 'Category')
->addOption('terms', 't', InputOption::VALUE_OPTIONAL, 'Terms')
->setHelp("The <info>%command.name%</info> search torrents");
}
@ -45,35 +45,71 @@ class TorrentsSearchCommand extends Command
array(
'offset' => (int) $input->getOption('offset'),
'limit' => (int) $input->getOption('limit'),
'category' => $input->getOption('category'),
'type' => $input->getOption('type'),
'term' => $input->getOption('term'),
'cat' => (int) $input->getOption('category'),
'terms' => $this->convertTerms($input->getOption('terms'), $client->getTermsTree()->getData()),
)
);
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$output->writeln(' ID NAME');
foreach ($response->getData()['torrents'] as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%4d</comment>] %9d %s',
$torrent['seeders'],
$torrent['leechers'],
$torrent['id'],
$torrent['name']
));
}
return $this->showResults($response, $output);
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
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] = array();
}
if (!in_array($term, $finalTerms[$termTypeId])) {
$finalTerms[$termTypeId][] = $term;
}
}
}
}
}
return $finalTerms;
}
protected function showResults(ClientResponse $response, OutputInterface $output)
{
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
$output->writeln(' SEED LEECH ID NAME');
foreach ($response->getData()['torrents'] as $torrent) {
$output->writeln(sprintf(
'[<info>%4d</info><comment>%6d</comment>] %9d %s',
$torrent['seeders'],
$torrent['leechers'],
$torrent['id'],
$torrent['name']
));
}
}
}

View file

@ -0,0 +1,105 @@
<?php
namespace Console\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Api\Client;
use Api\ConfigLoader;
use Symfony\Component\Console\Input\InputOption;
class TypesTreeCommand extends Command
{
protected function configure()
{
$this
->setName('types:tree')
->setDescription('Show types')
->addOption('terms', 't', InputOption::VALUE_NONE, 'Show terms')
->addOption('filter', 'f', InputOption::VALUE_OPTIONAL, 'Filter types by ID or by name')
->setHelp("The <info>%command.name%</info> show the terms");
}
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 {
$categoriesReponse = $client->getCategoriesTree();
$termsResponse = $client->getTermsTree();
foreach (array($categoriesReponse, $termsResponse) as $response) {
if ($response->hasError()) {
$output->writeln(sprintf(
'<error>%s</error> <comment>(%d)</comment>',
$response->getErrorMessage(),
$response->getErrorCode()
));
return;
}
}
$filter = $input->getOption('filter');
if (!empty($filter)) {
if (is_numeric($filter)) {
$filter = (int) $filter;
}
}
foreach ($termsResponse->getData() as $categoryId => $termTypes) {
$stop = false;
foreach ($termTypes as $termTypeId => $termType) {
if ($filter !== null) {
if (is_int($filter)) {
if ((int) $termTypeId === $filter) {
$stop = true;
} else {
continue;
}
} else {
if (0 === preg_match(sprintf('/%s/U', preg_quote($filter)), $termType['type'])) {
continue;
}
}
}
$output->writeln(sprintf('<comment>%3d</comment> <info>%s</info>', $termTypeId, $termType['type']));
if ($input->getOption('terms')) {
$isFirst = true;
foreach ($termType['terms'] as $termId => $term) {
$char = '|';
if ($isFirst) {
$isFirst = false;
$char = '`';
}
$output->writeln(sprintf(' %s- <comment>%4d</comment> %s', $char, $termId, $term));
}
$output->writeln('');
}
if ($stop) {
return;
}
}
}
} catch (ClientException $e) {
$output->writeln(sprintf('An error occured. <error>%s</error>', $e->getMessage()));
}
}
}