Refacto again
All checks were successful
repod / nextcloud (push) Successful in 44s
repod / nodejs (push) Successful in 1m14s

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

View File

@ -5,15 +5,14 @@ declare(strict_types=1);
namespace OCA\RePod\Service; namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService; use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class ItunesService implements IProvider class ItunesService implements IProvider
{ {
private const BASE_URL = 'https://itunes.apple.com/';
public function __construct( public function __construct(
private IClientService $clientService, private IClientService $clientService,
private IFactory $l10n, private UserService $userService
private IUserSession $userSession
) { ) {
} }
@ -22,11 +21,11 @@ class ItunesService implements IProvider
$podcasts = []; $podcasts = [];
$client = $this->clientService->newClient(); $client = $this->clientService->newClient();
$response = $client->get('https://itunes.apple.com/search', [ $response = $client->get(self::BASE_URL.'search', [
'query' => [ 'query' => [
'media' => 'podcast', 'media' => 'podcast',
'term' => $value, '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); $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];
}
}