* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see . */ declare(strict_types=1); namespace OCA\Radio\Search; use OCA\Radio\AppInfo\Application; use OCP\Http\Client\IClientService; use OCP\IURLGenerator; use OCP\IUser; use OCP\Search\IProvider; use OCP\Search\ISearchQuery; use OCP\Search\SearchResult; use OCP\Search\SearchResultEntry; use Psr\Log\LoggerInterface; class SearchProvider implements IProvider { public function __construct( private readonly IClientService $clientService, private readonly IURLGenerator $url, private readonly LoggerInterface $logger, ) {} public function getId(): string { return Application::APP_ID; } public function getName(): string { return 'Radio'; } public function getOrder(string $route, array $routeParameters): int { if (str_starts_with($route, 'files.')) { return 25; } if (str_starts_with($route, Application::APP_ID.'.')) { return -1; } return 4; } public function search(IUser $user, ISearchQuery $query): SearchResult { $term = $query->getTerm(); $url = 'https://de1.api.radio-browser.info/json/stations/byname/'.$term.'?limit=20'; $client = $this->clientService->newClient(); try { $response = $client->get($url); } catch (\Exception $exception) { $this->logger->error('Could not search for radio stations: '.$exception->getMessage()); throw $exception; } $body = (string) $response->getBody(); /** @var array> $parsed */ $parsed = json_decode($body, true); $result = array_map(fn (array $result): SearchResultEntry => new SearchResultEntry( $result['favicon'], $result['name'], str_replace(',', ', ', $result['tags']), $this->url->linkToRouteAbsolute('radio.page.index').'#/search/'.$term, 'icon-radio-trans' ), $parsed); return SearchResult::complete( $this->getName(), $result ); } }