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

82 lines
2.0 KiB
PHP
Raw Normal View History

2023-07-28 02:37:57 +02:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IUserSession;
use OCP\L10N\IFactory;
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,
private IUserSession $userSession
) {
}
public function search(string $value): array
{
$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) {
$podcasts[] = [
'provider' => 'fyyd',
'id' => $feed['id'],
'title' => $feed['title'],
'author' => $feed['author'],
'image' => $feed['imgURL'],
'provider_url' => $feed['htmlURL'],
'feed_url' => $feed['xmlURL'],
'last_pub' => $feed['lastpub'],
'nb_episodes' => $feed['episode_count'],
];
}
}
return $podcasts;
}
public function hot(): IResponse
{
$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) {
}
$podcastClient = $this->clientService->newClient();
2023-07-28 03:00:38 +02:00
return $podcastClient->get(self::BASE_URL.'feature/podcast/hot', [
2023-07-28 02:37:57 +02:00
'query' => [
'language' => $language,
],
]);
}
}