repod/lib/Service/FyydService.php

129 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2023-07-28 00:37:57 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
2023-08-22 17:41:17 +00:00
use OCA\GPodderSync\Core\PodcastData\PodcastData;
2023-07-28 00:37:57 +00:00
use OCP\Http\Client\IClientService;
2023-07-28 01:09:06 +00:00
use Psr\Log\LoggerInterface;
2023-07-28 00:37:57 +00:00
2024-01-18 10:43:58 +00:00
class FyydService implements IPodProvider
2023-07-28 00:37:57 +00:00
{
2023-07-28 01:00:38 +00:00
private const BASE_URL = 'https://api.fyyd.de/0.2/';
2023-07-28 00:37:57 +00:00
public function __construct(
private IClientService $clientService,
2024-01-18 10:43:58 +00:00
private LoggerInterface $logger,
private UserService $userService
2023-12-23 16:25:20 +00:00
) {}
2023-07-28 00:37:57 +00:00
2023-12-23 16:25:20 +00:00
public function search(string $value): array {
2023-07-28 00:37:57 +00:00
$podcasts = [];
$client = $this->clientService->newClient();
2023-07-28 01:00:38 +00:00
$response = $client->get(self::BASE_URL.'search/podcast', [
2023-07-28 00:37:57 +00:00
'query' => [
'title' => $value,
2023-07-28 01:00:38 +00:00
'url' => $value,
2023-07-28 00:37:57 +00:00
'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) {
2024-02-11 11:43:13 +00:00
if ($feed['title']) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
2023-07-28 00:37:57 +00:00
}
}
return $podcasts;
}
/**
2023-08-22 17:41:17 +00:00
* @return PodcastData[]
*/
2024-01-10 16:50:06 +00:00
public function latest(): array {
$podcasts = [];
$podcastClient = $this->clientService->newClient();
$podcastResponse = $podcastClient->get(self::BASE_URL.'podcast/latest');
$podcastJson = (array) json_decode((string) $podcastResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $podcastJson) && is_array($podcastJson['data'])) {
/** @var string[] $feed */
foreach ($podcastJson['data'] as $feed) {
2024-02-11 11:43:13 +00:00
if ($feed['title']) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
2024-01-10 16:50:06 +00:00
}
}
return $podcasts;
}
/**
* @return PodcastData[]
*/
public function hot(): array {
$podcasts = [];
2023-07-28 00:37:57 +00:00
$language = 'en';
2023-07-28 01:00:38 +00:00
$userLang = $this->userService->getLangCode();
2023-07-28 00:37:57 +00:00
try {
$langClient = $this->clientService->newClient();
2023-07-28 01:00:38 +00:00
$langResponse = $langClient->get(self::BASE_URL.'feature/podcast/hot/languages');
2023-07-28 00:37:57 +00:00
$langJson = (array) json_decode((string) $langResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
2024-01-18 16:06:26 +00:00
2023-07-28 00:37:57 +00:00
if (array_key_exists('data', $langJson) && is_array($langJson['data'])) {
2023-07-28 01:00:38 +00:00
$language = in_array($userLang, $langJson['data']) ? $userLang : 'en';
2023-07-28 00:37:57 +00:00
}
} catch (\Exception $e) {
2023-07-28 01:09:06 +00:00
$this->logger->error($e->getMessage(), $e->getTrace());
2023-07-28 00:37:57 +00:00
}
$podcastClient = $this->clientService->newClient();
$podcastResponse = $podcastClient->get(self::BASE_URL.'feature/podcast/hot', [
2023-07-28 00:37:57 +00:00
'query' => [
'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) {
2024-02-11 11:43:13 +00:00
if ($feed['title']) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
}
}
return $podcasts;
2023-07-28 00:37:57 +00:00
}
}