From d8241e1ac3498ddb2fbf4cdac7d1a3e479438abf Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Fri, 28 Jul 2023 03:00:38 +0200 Subject: [PATCH] Refacto again --- lib/Service/FyydService.php | 16 +++++++++------- lib/Service/ItunesService.php | 11 +++++------ lib/Service/UserService.php | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 lib/Service/UserService.php diff --git a/lib/Service/FyydService.php b/lib/Service/FyydService.php index 03b7e2e..fb7115e 100644 --- a/lib/Service/FyydService.php +++ b/lib/Service/FyydService.php @@ -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, ], diff --git a/lib/Service/ItunesService.php b/lib/Service/ItunesService.php index 1700a35..927d700 100644 --- a/lib/Service/ItunesService.php +++ b/lib/Service/ItunesService.php @@ -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); diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php new file mode 100644 index 0000000..4906371 --- /dev/null +++ b/lib/Service/UserService.php @@ -0,0 +1,36 @@ +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]; + } +}