2021-04-07 10:37:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2024-11-19 19:41:46 +00:00
|
|
|
* Radio App.
|
2021-04-07 10:37:46 +00:00
|
|
|
*
|
|
|
|
* @author Jonas Heinrich
|
|
|
|
* @copyright 2021 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\Radio\Service;
|
|
|
|
|
|
|
|
use OCP\Http\Client\IClientService;
|
2024-11-19 19:41:46 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
class RadioBrowserApiService
|
|
|
|
{
|
|
|
|
public string $baseUrl = 'https://api.fyyd.de/0.2';
|
2021-04-07 10:37:46 +00:00
|
|
|
|
|
|
|
public function __construct(
|
2024-11-19 19:41:46 +00:00
|
|
|
private readonly IClientService $clientService,
|
|
|
|
private readonly LoggerInterface $logger,
|
|
|
|
) {}
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function queryEpisodes(int $podcast_id, int $count = 20, int $page = 0): mixed {
|
|
|
|
$url = $this->baseUrl.'/podcast/episodes';
|
|
|
|
$options = [];
|
2021-04-07 10:37:46 +00:00
|
|
|
|
|
|
|
$options['query'] = [
|
|
|
|
'podcast_id' => $podcast_id,
|
|
|
|
'count' => $count,
|
2024-11-19 19:41:46 +00:00
|
|
|
'page' => $page,
|
2021-04-07 10:37:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$client = $this->clientService->newClient();
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2021-04-07 10:37:46 +00:00
|
|
|
try {
|
|
|
|
$response = $client->get($url, $options);
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->error('Could not search for podcasts: '.$exception->getMessage());
|
|
|
|
|
|
|
|
throw $exception;
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
$body = (string) $response->getBody();
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
return json_decode($body, true);
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function queryEpisode(int $episode_id): mixed {
|
|
|
|
$url = $this->baseUrl.'/episode';
|
|
|
|
$options = [];
|
2021-04-07 10:37:46 +00:00
|
|
|
|
|
|
|
$options['query'] = [
|
|
|
|
'episode_id' => $episode_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$client = $this->clientService->newClient();
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2021-04-07 10:37:46 +00:00
|
|
|
try {
|
|
|
|
$response = $client->get($url, $options);
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->error('Could not search for podcasts: '.$exception->getMessage());
|
|
|
|
|
|
|
|
throw $exception;
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
$body = (string) $response->getBody();
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
return json_decode($body, true);
|
|
|
|
}
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function queryPodcast(int $podcast_id): mixed {
|
|
|
|
$url = $this->baseUrl.'/podcast';
|
|
|
|
$options = [];
|
2021-04-07 10:37:46 +00:00
|
|
|
|
|
|
|
$options['query'] = [
|
|
|
|
'podcast_id' => $podcast_id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$client = $this->clientService->newClient();
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2021-04-07 10:37:46 +00:00
|
|
|
try {
|
|
|
|
$response = $client->get($url, $options);
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->error('Could not search for podcasts: '.$exception->getMessage());
|
|
|
|
|
|
|
|
throw $exception;
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
$body = (string) $response->getBody();
|
|
|
|
|
|
|
|
return json_decode($body, true);
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
public function queryCategory(
|
|
|
|
string $category,
|
|
|
|
int $count = 20,
|
|
|
|
int $page = 0
|
|
|
|
): mixed {
|
|
|
|
$options = [];
|
2021-04-07 10:37:46 +00:00
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
if ('hot' === $category) {
|
|
|
|
$url = $this->baseUrl.'/feature/podcast/hot';
|
2021-04-07 10:37:46 +00:00
|
|
|
$options['query'] = [
|
|
|
|
'count' => $count,
|
|
|
|
'page' => $page,
|
|
|
|
];
|
2024-11-19 19:41:46 +00:00
|
|
|
} elseif ('latest' === $category) {
|
|
|
|
$url = $this->baseUrl.'/podcast/latest';
|
2021-04-07 10:37:46 +00:00
|
|
|
$options['query'] = [
|
|
|
|
'count' => $count,
|
|
|
|
'page' => $page,
|
|
|
|
];
|
|
|
|
} else {
|
2024-11-19 19:41:46 +00:00
|
|
|
$url = $this->baseUrl.'/category';
|
2021-04-07 10:37:46 +00:00
|
|
|
$options['query'] = [
|
|
|
|
'count' => $count,
|
|
|
|
'page' => $page,
|
|
|
|
'category_id' => $category,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = $this->clientService->newClient();
|
2024-11-19 19:41:46 +00:00
|
|
|
|
2021-04-07 10:37:46 +00:00
|
|
|
try {
|
|
|
|
$response = $client->get($url, $options);
|
2024-11-19 19:41:46 +00:00
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->logger->error('Could not search for podcasts: '.$exception->getMessage());
|
|
|
|
|
|
|
|
throw $exception;
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
|
2024-11-19 19:41:46 +00:00
|
|
|
$body = (string) $response->getBody();
|
|
|
|
|
|
|
|
return json_decode($body, true);
|
2021-04-07 10:37:46 +00:00
|
|
|
}
|
|
|
|
}
|