nextcloud-app-radio/lib/Search/SearchProvider.php

98 lines
2.5 KiB
PHP
Raw Permalink Normal View History

2020-11-04 09:32:06 +00:00
<?php
2020-11-24 14:16:53 +00:00
/**
* Radio App.
2020-11-24 14:16:53 +00:00
*
* @author Jonas Heinrich
2021-01-15 10:21:53 +00:00
* @copyright 2021 Jonas Heinrich <onny@project-insanity.org>
2020-11-24 14:16:53 +00:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
2020-11-04 09:32:06 +00:00
declare(strict_types=1);
namespace OCA\Radio\Search;
use OCA\Radio\AppInfo\Application;
use OCP\Http\Client\IClientService;
2020-11-04 09:32:06 +00:00
use OCP\IURLGenerator;
use OCP\IUser;
2020-11-04 09:32:06 +00:00
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;
use Psr\Log\LoggerInterface;
2020-11-04 09:32:06 +00:00
class SearchProvider implements IProvider
{
2020-11-04 09:32:06 +00:00
public function __construct(
private readonly IClientService $clientService,
private readonly IURLGenerator $url,
private readonly LoggerInterface $logger,
) {}
2020-11-04 09:32:06 +00:00
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.')) {
2020-11-04 09:32:06 +00:00
return 25;
}
if (str_starts_with($route, Application::APP_ID.'.')) {
2020-11-04 09:32:06 +00:00
return -1;
}
2020-11-04 09:32:06 +00:00
return 4;
}
public function search(IUser $user, ISearchQuery $query): SearchResult {
2020-11-09 12:17:18 +00:00
$term = $query->getTerm();
$url = 'https://de1.api.radio-browser.info/json/stations/byname/'.$term.'?limit=20';
2020-11-09 12:17:18 +00:00
$client = $this->clientService->newClient();
2020-11-09 12:17:18 +00:00
try {
$response = $client->get($url);
} catch (\Exception $exception) {
$this->logger->error('Could not search for radio stations: '.$exception->getMessage());
throw $exception;
2020-11-09 12:17:18 +00:00
}
$body = (string) $response->getBody();
/** @var array<array<string, string>> $parsed */
2020-11-09 12:17:18 +00:00
$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);
2020-11-09 12:17:18 +00:00
2020-11-04 09:32:06 +00:00
return SearchResult::complete(
$this->getName(),
$result
);
}
}