diff --git a/src/Commands/Servers/RemoteCommand.php b/src/Commands/Servers/RemoteCommand.php index 31cad5c6..e63389c3 100644 --- a/src/Commands/Servers/RemoteCommand.php +++ b/src/Commands/Servers/RemoteCommand.php @@ -29,6 +29,7 @@ final class RemoteCommand extends Command ->addOption('list-users-with-tokens', null, InputOption::VALUE_NONE, 'Show users list with tokens.') ->addOption('use-token', null, InputOption::VALUE_REQUIRED, 'Override server config token.') ->addOption('search', null, InputOption::VALUE_REQUIRED, 'Search query') + ->addOption('search-id', null, InputOption::VALUE_REQUIRED, 'Get metadata related to given id') ->addOption('search-limit', null, InputOption::VALUE_REQUIRED, 'Search limit', 25) ->addOption('search-output', null, InputOption::VALUE_REQUIRED, 'Search output style [json,yaml]', 'json') ->addOption('config', 'c', InputOption::VALUE_REQUIRED, 'Use Alternative config file.') @@ -84,6 +85,14 @@ final class RemoteCommand extends Command $this->search($server, $output, $input); } + if ($input->getOption('search') && $input->getOption('search-limit')) { + $this->search($server, $output, $input); + } + + if ($input->getOption('search-id')) { + $this->searchId($server, $output, $input); + } + return self::SUCCESS; } @@ -167,4 +176,27 @@ final class RemoteCommand extends Command $output->writeln(Yaml::dump($result, 8, 2)); } } + + private function searchId(ServerInterface $server, OutputInterface $output, InputInterface $input): void + { + $result = $server->searchId($input->getOption('search-id')); + + if (empty($result)) { + $output->writeln( + sprintf('No meta data found for id \'%s\'.', $input->getOption('search-id')) + ); + exit(1); + } + + if ('json' === $input->getOption('search-output')) { + $output->writeln( + json_encode( + value: $result, + flags: JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_IGNORE + ) + ); + } else { + $output->writeln(Yaml::dump($result, 8, 2)); + } + } } diff --git a/src/Libs/Servers/JellyfinServer.php b/src/Libs/Servers/JellyfinServer.php index fe523e2c..e07226d5 100644 --- a/src/Libs/Servers/JellyfinServer.php +++ b/src/Libs/Servers/JellyfinServer.php @@ -439,6 +439,50 @@ class JellyfinServer implements ServerInterface } } + public function searchId(string|int $id): array + { + $this->checkConfig(); + + try { + $url = $this->url->withPath(sprintf('/Users/%s/items/' . $id, $this->user))->withQuery( + http_build_query( + [ + 'Recursive' => 'false', + 'Fields' => 'ProviderIds', + 'enableUserData' => 'true', + 'enableImages' => 'false', + 'IncludeItemTypes' => 'Episode,Movie,Series', + ] + ) + ); + + $this->logger->debug(sprintf('%s: Sending get meta data for id \'%s\'.', $this->name, $id), [ + 'url' => $url + ]); + + $response = $this->http->request('GET', (string)$url, $this->getHeaders()); + + if (200 !== $response->getStatusCode()) { + throw new RuntimeException( + sprintf( + '%s: Get metadata request for id \'%s\' responded with unexpected http status code \'%d\'.', + $this->name, + $id, + $response->getStatusCode() + ) + ); + } + + return json_decode( + json: $response->getContent(), + associative: true, + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + ); + } catch (ExceptionInterface|JsonException $e) { + throw new RuntimeException(get_class($e) . ': ' . $e->getMessage(), $e->getCode(), $e); + } + } + public function listLibraries(): array { $this->checkConfig(true); diff --git a/src/Libs/Servers/PlexServer.php b/src/Libs/Servers/PlexServer.php index fcc3f995..b3fc7f1c 100644 --- a/src/Libs/Servers/PlexServer.php +++ b/src/Libs/Servers/PlexServer.php @@ -493,6 +493,42 @@ class PlexServer implements ServerInterface } } + public function searchId(string|int $id): array + { + $this->checkConfig(); + + try { + $url = $this->url->withPath('/library/metadata/' . $id)->withQuery( + http_build_query(['includeGuids' => 1]) + ); + + $this->logger->debug(sprintf('%s: Sending get meta data for id \'%s\'.', $this->name, $id), [ + 'url' => $url + ]); + + $response = $this->http->request('GET', (string)$url, $this->getHeaders()); + + if (200 !== $response->getStatusCode()) { + throw new RuntimeException( + sprintf( + '%s: Get metadata request for id \'%s\' responded with unexpected http status code \'%d\'.', + $this->name, + $id, + $response->getStatusCode() + ) + ); + } + + return json_decode( + json: $response->getContent(), + associative: true, + flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE + ); + } catch (ExceptionInterface|JsonException $e) { + throw new RuntimeException(get_class($e) . ': ' . $e->getMessage(), $e->getCode(), $e); + } + } + public function listLibraries(): array { $this->checkConfig(); diff --git a/src/Libs/Servers/ServerInterface.php b/src/Libs/Servers/ServerInterface.php index 2ca943a6..775bdc60 100644 --- a/src/Libs/Servers/ServerInterface.php +++ b/src/Libs/Servers/ServerInterface.php @@ -113,6 +113,14 @@ interface ServerInterface */ public function search(string $query, int $limit = 25): array; + /** + * Server Backend id. + * + * @param string|int $id + * @return array + */ + public function searchId(string|int $id): array; + /** * Get all persistent data. *