From 40c83d79eeb802dfe643060159d6b0ace4204a32 Mon Sep 17 00:00:00 2001 From: Jonas Heinrich Date: Mon, 9 Nov 2020 13:17:18 +0100 Subject: [PATCH] further improvements to unified search --- lib/Search/SearchProvider.php | 46 +++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/lib/Search/SearchProvider.php b/lib/Search/SearchProvider.php index e3be9f7..cbc829b 100644 --- a/lib/Search/SearchProvider.php +++ b/lib/Search/SearchProvider.php @@ -12,16 +12,23 @@ use OCP\Search\ISearchQuery; use OCP\Search\SearchResult; use OCP\Search\SearchResultEntry; +use OCP\Http\Client\IClientService; + use function urlencode; class SearchProvider implements IProvider { + /** @var IClientService */ + private $clientService; + /** @var IURLGenerator */ private $url; public function __construct( + IClientService $clientService, IURLGenerator $url ) { + $this->clientService = $clientService; $this->url = $url; } @@ -43,14 +50,37 @@ class SearchProvider implements IProvider { } public function search(IUser $user, ISearchQuery $query): SearchResult { - // create SearchResultEntry from Note - $result = [new SearchResultEntry( - '', - 'SRF Virus', - 'alternative, youth', - $this->url->linkToRouteAbsolute('radio.page.index') . 'search/' . urlencode($query->getTerm()), // FIXME: urlencode working? - 'icon-notes-trans' - )]; + + $term = $query->getTerm(); + $url = "https://de1.api.radio-browser.info/json/stations/byname/" . $term; + + $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); + + $result = array_map(function (array $result) { + return new SearchResultEntry( + $result['favicon'], + $result['name'], + $result['tags'], + '#/search/virus', + 'icon-notes-trans' + ); + }, $parsed); + + // $result = [new SearchResultEntry( + // '', + // 'SRF Virus', + // 'alternative, youth', + // $this->url->linkToRouteAbsolute('radio.page.index') . 'search/' . urlencode($query->getTerm()), // FIXME: urlencode working? + // 'icon-notes-trans' + // )]; return SearchResult::complete( $this->getName(), $result