repod/lib/Service/FyydService.php

126 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;
use OCP\IUserSession;
use OCP\L10N\IFactory;
2023-07-28 01:09:06 +00:00
use Psr\Log\LoggerInterface;
2023-07-28 00:37:57 +00:00
class FyydService implements IProvider
{
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(
2023-07-28 01:00:38 +00:00
private UserService $userService,
2023-07-28 00:37:57 +00:00
private IClientService $clientService,
private IFactory $l10n,
2023-07-28 01:09:06 +00:00
private IUserSession $userSession,
private LoggerInterface $logger
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) {
2023-08-22 17:41:17 +00:00
$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) {
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
}
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);
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) {
2023-08-22 17:41:17 +00:00
$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
}
}