repod/lib/Service/FyydService.php
Michel Roux 941e1cabbd
All checks were successful
repod / nextcloud (push) Successful in 1m15s
repod / nodejs (push) Successful in 1m52s
Back ready !
2023-08-22 19:41:17 +02:00

104 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use Psr\Log\LoggerInterface;
class FyydService implements IProvider
{
private const BASE_URL = 'https://api.fyyd.de/0.2/';
public function __construct(
private UserService $userService,
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession,
private LoggerInterface $logger
) {
}
public function search(string $value): array
{
$podcasts = [];
$client = $this->clientService->newClient();
$response = $client->get(self::BASE_URL.'search/podcast', [
'query' => [
'title' => $value,
'url' => $value,
'term' => $value,
],
]);
$json = (array) json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $json) && is_array($json['data'])) {
/** @var string[] $feed */
foreach ($json['data'] as $feed) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
}
return $podcasts;
}
/**
* @return PodcastData[]
*/
public function hot(int $count = 10): array
{
$podcasts = [];
$language = 'en';
$userLang = $this->userService->getLangCode();
try {
$langClient = $this->clientService->newClient();
$langResponse = $langClient->get(self::BASE_URL.'feature/podcast/hot/languages');
$langJson = (array) json_decode((string) $langResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $langJson) && is_array($langJson['data'])) {
$language = in_array($userLang, $langJson['data']) ? $userLang : 'en';
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), $e->getTrace());
}
$podcastClient = $this->clientService->newClient();
$podcastResponse = $podcastClient->get(self::BASE_URL.'feature/podcast/hot', [
'query' => [
'count' => $count,
'language' => $language,
],
]);
$postCastJson = (array) json_decode((string) $podcastResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $postCastJson) && is_array($postCastJson['data'])) {
/** @var string[] $feed */
foreach ($postCastJson['data'] as $feed) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
}
return $podcasts;
}
}