2020-11-04 09:32:06 +00:00
|
|
|
<?php
|
|
|
|
|
2020-11-24 14:16:53 +00:00
|
|
|
/**
|
|
|
|
* Radio App
|
|
|
|
*
|
|
|
|
* @author Jonas Heinrich
|
|
|
|
* @copyright 2020 Jonas Heinrich <onny@project-insanity.org>
|
|
|
|
*
|
|
|
|
* 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\IUser;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\Search\IProvider;
|
|
|
|
use OCP\Search\ISearchQuery;
|
|
|
|
use OCP\Search\SearchResult;
|
|
|
|
use OCP\Search\SearchResultEntry;
|
|
|
|
|
2020-11-09 12:17:18 +00:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
|
2020-11-04 09:32:06 +00:00
|
|
|
use function urlencode;
|
|
|
|
|
|
|
|
class SearchProvider implements IProvider {
|
|
|
|
|
2020-11-09 12:17:18 +00:00
|
|
|
/** @var IClientService */
|
|
|
|
private $clientService;
|
|
|
|
|
2020-11-04 09:32:06 +00:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
public function __construct(
|
2020-11-09 12:17:18 +00:00
|
|
|
IClientService $clientService,
|
2020-11-04 09:32:06 +00:00
|
|
|
IURLGenerator $url
|
|
|
|
) {
|
2020-11-09 12:17:18 +00:00
|
|
|
$this->clientService = $clientService;
|
2020-11-04 09:32:06 +00:00
|
|
|
$this->url = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getId(): string {
|
|
|
|
return Application::APP_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
return 'Radio';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOrder(string $route, array $routeParameters): int {
|
|
|
|
if (strpos($route, 'files' . '.') === 0) {
|
|
|
|
return 25;
|
|
|
|
} elseif (strpos($route, Application::APP_ID . '.') === 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function search(IUser $user, ISearchQuery $query): SearchResult {
|
2020-11-09 12:17:18 +00:00
|
|
|
|
|
|
|
$term = $query->getTerm();
|
2020-11-10 13:27:01 +00:00
|
|
|
$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();
|
|
|
|
try {
|
|
|
|
$response = $client->get($url);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->logger->error("Could not search for radio stations: " . $e->getMessage());
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
$body = $response->getBody();
|
|
|
|
$parsed = json_decode($body, true);
|
|
|
|
|
2020-11-10 16:45:12 +00:00
|
|
|
$result = array_map(function (array $result) use ($term) {
|
2020-11-09 12:17:18 +00:00
|
|
|
return new SearchResultEntry(
|
|
|
|
$result['favicon'],
|
|
|
|
$result['name'],
|
2020-11-10 12:53:45 +00:00
|
|
|
str_replace(",",", ",$result['tags']),
|
2020-11-11 14:53:33 +00:00
|
|
|
$this->url->linkToRouteAbsolute('radio.page.index') . '#/search/' . $term,
|
2020-11-11 09:51:20 +00:00
|
|
|
'icon-radio-trans'
|
2020-11-09 12:17:18 +00:00
|
|
|
);
|
|
|
|
}, $parsed);
|
|
|
|
|
2020-11-04 09:32:06 +00:00
|
|
|
return SearchResult::complete(
|
|
|
|
$this->getName(),
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|