Merge pull request #115 from ArabCoders/dev

Added servers:remote --search-id option to get metadata from backend.
This commit is contained in:
Abdulmohsen
2022-05-20 22:36:28 +03:00
committed by GitHub
4 changed files with 120 additions and 0 deletions

View File

@@ -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('<error>No meta data found for id \'%s\'.</error>', $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));
}
}
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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.
*