RePod-Nextcloud-App/lib/Service/FyydService.php

101 lines
2.6 KiB
PHP
Raw Normal View History

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;
use OCP\IUserSession;
use OCP\L10N\IFactory;
2023-07-28 03:09:06 +02:00
use Psr\Log\LoggerInterface;
2023-07-28 02:37:57 +02:00
class FyydService implements IProvider
{
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(
2023-07-28 03:00:38 +02:00
private UserService $userService,
2023-07-28 02:37:57 +02:00
private IClientService $clientService,
private IFactory $l10n,
2023-07-28 03:09:06 +02:00
private IUserSession $userSession,
private LoggerInterface $logger
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-22 19:41:17 +02:00
* @return PodcastData[]
*/
2023-12-23 17:25:20 +01:00
public function hot(int $count = 10): array {
$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();
$podcastResponse = $podcastClient->get(self::BASE_URL.'feature/podcast/hot', [
2023-07-28 02:37:57 +02:00
'query' => [
'count' => $count,
2023-07-28 02:37:57 +02:00
'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 19:41:17 +02:00
$podcasts[] = new PodcastData(
$feed['title'],
$feed['author'],
$feed['xmlURL'],
$feed['description'],
$feed['imgURL'],
strtotime($feed['lastpub'])
);
}
}
return $podcasts;
2023-07-28 02:37:57 +02:00
}
}