Refacto again

This commit is contained in:
Michel Roux 2023-07-28 03:00:38 +02:00
parent 4503df9d33
commit d8241e1ac3
3 changed files with 50 additions and 13 deletions

View File

@ -11,7 +11,10 @@ use OCP\L10N\IFactory;
class FyydService implements IProvider
{
private const BASE_URL = 'https://api.fyyd.de/0.2/';
public function __construct(
private UserService $userService,
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
@ -23,9 +26,10 @@ class FyydService implements IProvider
$podcasts = [];
$client = $this->clientService->newClient();
$response = $client->get('https://api.fyyd.de/0.2/search/podcast', [
$response = $client->get(self::BASE_URL.'search/podcast', [
'query' => [
'title' => $value,
'url' => $value,
'term' => $value,
],
]);
@ -54,23 +58,21 @@ class FyydService implements IProvider
public function hot(): IResponse
{
$language = 'en';
$userLang = $this->userService->getLangCode();
try {
$langClient = $this->clientService->newClient();
$langResponse = $langClient->get('https://api.fyyd.de/0.2/feature/podcast/hot/languages');
$langResponse = $langClient->get(self::BASE_URL.'feature/podcast/hot/languages');
$langJson = (array) json_decode((string) $langResponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('data', $langJson) && is_array($langJson['data'])) {
$language = $this->l10n->getUserLanguage($this->userSession->getUser());
$language = explode('_', $language);
$language = count($language) > 1 ? $language[1] : $language[0];
$language = in_array($language, $langJson['data']) ? $language : 'en';
$language = in_array($userLang, $langJson['data']) ? $userLang : 'en';
}
} catch (\Exception $e) {
}
$podcastClient = $this->clientService->newClient();
return $podcastClient->get('https://api.fyyd.de/0.2/feature/podcast/hot', [
return $podcastClient->get(self::BASE_URL.'feature/podcast/hot', [
'query' => [
'language' => $language,
],

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class ItunesService implements IProvider
{
private const BASE_URL = 'https://itunes.apple.com/';
public function __construct(
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
private UserService $userService
) {
}
@ -22,11 +21,11 @@ class ItunesService implements IProvider
$podcasts = [];
$client = $this->clientService->newClient();
$response = $client->get('https://itunes.apple.com/search', [
$response = $client->get(self::BASE_URL.'search', [
'query' => [
'media' => 'podcast',
'term' => $value,
'lang' => $this->l10n->getUserLanguage($this->userSession->getUser()),
'country' => $this->userService->getCountryCode(),
],
]);
$json = (array) json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class UserService
{
public function __construct(
private IFactory $l10n,
private IUserSession $userSession
) {
}
public function getIsoCode(): string
{
return $this->l10n->getUserLanguage($this->userSession->getUser());
}
public function getCountryCode(): string
{
$isoCodes = explode('_', $this->getIsoCode());
return isset($isoCodes[1]) ? $isoCodes[1] : 'us';
}
public function getLangCode(): string
{
$isoCodes = explode('_', $this->getIsoCode());
return $isoCodes[0];
}
}