repod/lib/Service/FyydService.php

118 lines
3.1 KiB
PHP
Raw Normal View History

2023-07-28 00:37:57 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
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
/**
* @psalm-import-type Podcast from IProvider
*/
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-07-28 00:37:57 +00:00
) {
}
public function search(string $value): array
{
$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) {
$podcasts[] = [
'id' => $feed['id'],
'provider' => 'fyyd',
'website' => $feed['htmlURL'],
'description' => $feed['description'],
2023-07-28 00:37:57 +00:00
'title' => $feed['title'],
'author' => $feed['author'],
'url' => $feed['xmlURL'],
'position_last_week' => $feed['rank'],
'mygpo_link' => $feed['url_fyyd'],
'logo_url' => $feed['imgURL'],
'lastpub' => $feed['lastpub'],
'episode_count' => $feed['episode_count'],
2023-07-28 00:37:57 +00:00
];
}
}
return $podcasts;
}
/**
* @return Podcast[]
*/
public function hot(int $count = 10): array
2023-07-28 00:37:57 +00:00
{
$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' => [
'count' => $count,
2023-07-28 00:37:57 +00: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) {
$podcasts[] = [
'id' => $feed['id'],
'provider' => 'fyyd',
'website' => $feed['htmlURL'],
'description' => $feed['description'],
'title' => $feed['title'],
'author' => $feed['author'],
'url' => $feed['xmlURL'],
'position_last_week' => $feed['rank'],
'mygpo_link' => $feed['url_fyyd'],
'logo_url' => $feed['imgURL'],
'lastpub' => $feed['lastpub'],
'episode_count' => $feed['episode_count'],
];
}
}
return $podcasts;
2023-07-28 00:37:57 +00:00
}
}