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