From 5e52fe861f96397ff90f8389168bf23b6dd75d2e Mon Sep 17 00:00:00 2001 From: "Abdulmhsen B. A. A" Date: Mon, 30 May 2022 00:33:07 +0300 Subject: [PATCH] Updates to clear --search-mismatch output. --- src/Commands/Servers/RemoteCommand.php | 82 +++++++++++++++++++++----- src/Libs/Servers/JellyfinServer.php | 14 ++--- src/Libs/Servers/PlexServer.php | 14 ++--- 3 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/Commands/Servers/RemoteCommand.php b/src/Commands/Servers/RemoteCommand.php index 251cafbc..5a9cecec 100644 --- a/src/Commands/Servers/RemoteCommand.php +++ b/src/Commands/Servers/RemoteCommand.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Yaml\Yaml; use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; +use Throwable; final class RemoteCommand extends Command { @@ -117,7 +118,7 @@ final class RemoteCommand extends Command } if ($input->getOption('search-mismatch')) { - $this->searchMismatch($server, $output, $input); + return $this->searchMismatch($server, $output, $input); } return self::SUCCESS; @@ -235,33 +236,82 @@ final class RemoteCommand extends Command } } - private function searchMismatch(ServerInterface $server, OutputInterface $output, InputInterface $input): void + private function searchMismatch(ServerInterface $server, OutputInterface $output, InputInterface $input): int { $id = $input->getOption('search-mismatch'); $percentage = (float)$input->getOption('search-coef'); + $mode = $input->getOption('search-output'); - $result = $server->searchMismatch(id: $id, opts: ['coef' => $percentage]); - - if (empty($result)) { - $output->writeln( - sprintf( - 'No mismatched items were identified in library id \'%s\' with less than \'%d\' percentage.', - $id, - $percentage - ) - ); - exit(1); + try { + $result = $server->searchMismatch(id: $id, opts: ['coef' => $percentage]); + } catch (Throwable $e) { + $this->setOutputContent(['error' => $e->getMessage()], $output, $mode); + return self::FAILURE; } - if ('json' === $input->getOption('search-output')) { + if (empty($result)) { + $this->setOutputContent( + [ + 'info' => sprintf( + 'We are %1$02.2f%3$s sure there are no mis-identified items in library \'%2$s\'.', + $percentage, + $id, + '%', + ) + ], + $output, + $mode + ); + return self::SUCCESS; + } + + $this->setOutputContent($result, $output, $mode); + + return self::SUCCESS; + } + + private function setOutputContent(array $content, OutputInterface $output, string $mode = 'json'): void + { + if ('json' === $mode) { $output->writeln( json_encode( - value: $result, + value: $content, flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE ) ); + } elseif ('table' === $mode) { + $list = []; + $x = 0; + $count = count($content); + + foreach ($content as $_ => $item) { + if (false === is_array($item)) { + $item = [$_ => $item]; + } + + $subItem = []; + + foreach ($item as $key => $leaf) { + if (true === is_array($leaf)) { + continue; + } + $subItem[$key] = $leaf; + } + + $x++; + $list[] = $subItem; + if ($x < $count) { + $list[] = new TableSeparator(); + } + } + + if (!empty($list)) { + (new Table($output))->setStyle('box')->setHeaders( + array_map(fn($title) => is_string($title) ? ucfirst($title) : $title, array_keys($list[0])) + )->setRows($list)->render(); + } } else { - $output->writeln(Yaml::dump($result, 8, 2)); + $output->writeln(Yaml::dump($content, 8, 2)); } } diff --git a/src/Libs/Servers/JellyfinServer.php b/src/Libs/Servers/JellyfinServer.php index 85cac6d1..50acc70e 100644 --- a/src/Libs/Servers/JellyfinServer.php +++ b/src/Libs/Servers/JellyfinServer.php @@ -666,23 +666,21 @@ class JellyfinServer implements ServerInterface $metadata = [ 'id' => ag($item, 'Id'), - 'url' => (string)$url, 'type' => ucfirst($type), + 'url' => [(string)$url], 'title' => ag($item, $possibleTitlesList, '??'), 'guids' => $guids, 'paths' => $locations, 'matching' => $matches, - 'comments' => [], + 'comments' => (empty($paths)) ? 'No path found.' : 'Title does not match path.', ]; - if (empty($paths)) { - $metadata['comments'][] = sprintf('No Path found for %s.', $type); - } else { - $metadata['comments'][] = 'No title match path, Possible mismatch or outdated metadata.'; + if (empty($guids)) { + $metadata['guids'] = 'None.'; } - if (empty($guids)) { - $metadata['comment'] = 'No external ids were found. Indicate the possibility of unmatched item.'; + if (count($locations) <= 1) { + $metadata['paths'] = $locations[0]; } $list[] = $metadata; diff --git a/src/Libs/Servers/PlexServer.php b/src/Libs/Servers/PlexServer.php index 876d6026..57674d90 100644 --- a/src/Libs/Servers/PlexServer.php +++ b/src/Libs/Servers/PlexServer.php @@ -692,23 +692,21 @@ class PlexServer implements ServerInterface $metadata = [ 'id' => (int)ag($item, 'ratingKey'), - 'url' => (string)$url, 'type' => ucfirst($type), + 'url' => [(string)$url], 'title' => ag($item, $possibleTitlesList, '??'), 'guids' => $guids, 'paths' => $locations, 'matching' => $matches, - 'comments' => [], + 'comments' => (empty($paths)) ? 'No path found.' : 'Title does not match path.', ]; - if (empty($paths)) { - $metadata['comments'][] = sprintf('No Path found for %s.', $type); - } else { - $metadata['comments'][] = 'No title match path, Possible mismatch or outdated metadata.'; + if (empty($guids)) { + $metadata['guids'] = 'No external ids found.'; } - if (empty($guids)) { - $metadata['comment'] = 'No external ids were found. Indicate the possibility of unmatched item.'; + if (count($locations) <= 1) { + $metadata['paths'] = $locations[0]; } $list[] = $metadata;