From 4503df9d33a40d9131579bd6f17a943a847c9ba1 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Fri, 28 Jul 2023 02:37:57 +0200 Subject: [PATCH] Refacto to services --- appinfo/routes.php | 2 +- lib/Controller/SearchController.php | 78 ++++------------------------ lib/Controller/TopController.php | 37 +++----------- lib/Service/FyydService.php | 79 +++++++++++++++++++++++++++++ lib/Service/IProvider.php | 23 +++++++++ lib/Service/ItunesService.php | 53 +++++++++++++++++++ src/components/TopList.vue | 49 +++++------------- src/views/Discover.vue | 2 +- 8 files changed, 184 insertions(+), 139 deletions(-) create mode 100644 lib/Service/FyydService.php create mode 100644 lib/Service/IProvider.php create mode 100644 lib/Service/ItunesService.php diff --git a/appinfo/routes.php b/appinfo/routes.php index 08ed701..f56040d 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -13,7 +13,7 @@ declare(strict_types=1); return [ 'routes' => [ ['name' => 'page#index', 'url' => '/', 'verb' => 'GET'], - ['name' => 'top#index', 'url' => '/top/{count}', 'verb' => 'GET'], + ['name' => 'top#index', 'url' => '/top', 'verb' => 'GET'], ['name' => 'search#index', 'url' => '/search/{value}', 'verb' => 'GET'], ], ]; diff --git a/lib/Controller/SearchController.php b/lib/Controller/SearchController.php index 8ed8704..1b9c131 100644 --- a/lib/Controller/SearchController.php +++ b/lib/Controller/SearchController.php @@ -5,20 +5,18 @@ declare(strict_types=1); namespace OCA\RePod\Controller; use OCA\RePod\AppInfo\Application; +use OCA\RePod\Service\FyydService; +use OCA\RePod\Service\ItunesService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; -use OCP\Http\Client\IClientService; use OCP\IRequest; -use OCP\IUserSession; -use OCP\L10N\IFactory; class SearchController extends Controller { public function __construct( IRequest $request, - private IClientService $clientService, - private IFactory $l10n, - private IUserSession $userSession + private FyydService $fyydService, + private ItunesService $itunesService ) { parent::__construct(Application::APP_ID, $request); } @@ -26,71 +24,13 @@ class SearchController extends Controller public function index(string $value): JSONResponse { $podcasts = []; + $providers = [$this->fyydService, $this->itunesService]; - try { - $fyydClient = $this->clientService->newClient(); - $fyydReponse = $fyydClient->get('https://api.fyyd.de/0.2/search/podcast', [ - 'query' => [ - 'title' => $value, - 'term' => $value, - ], - ]); - $fyydJson = (array) json_decode((string) $fyydReponse->getBody(), true, flags: JSON_THROW_ON_ERROR); - - if (array_key_exists('data', $fyydJson) && is_array($fyydJson['data'])) { - /** @var string[] $fyydFeed */ - foreach ($fyydJson['data'] as $fyydFeed) { - $lastPub = date_format(new \DateTime($fyydFeed['lastpub']), DATE_ATOM); - - if ($lastPub) { - $podcasts[] = [ - 'provider' => 'fyyd', - 'id' => $fyydFeed['id'], - 'title' => $fyydFeed['title'], - 'author' => $fyydFeed['author'], - 'image' => $fyydFeed['imgURL'], - 'provider_url' => $fyydFeed['htmlURL'], - 'feed_url' => $fyydFeed['xmlURL'], - 'last_pub' => $lastPub, - 'nb_episodes' => $fyydFeed['episode_count'], - ]; - } - } + foreach ($providers as $provider) { + try { + $podcasts = [...$podcasts, ...$provider->search($value)]; + } catch (\Exception $e) { } - } catch (\Exception $e) { - } - - try { - $itunesClient = $this->clientService->newClient(); - $itunesResponse = $itunesClient->get('https://itunes.apple.com/search', [ - 'query' => [ - 'media' => 'podcast', - 'term' => $value, - 'lang' => $this->l10n->getUserLanguage($this->userSession->getUser()), - ], - ]); - $itunesJson = (array) json_decode((string) $itunesResponse->getBody(), true, flags: JSON_THROW_ON_ERROR); - - if (array_key_exists('data', $itunesJson) && is_array($itunesJson['data'])) { - /** @var string[] $itunesFeed */ - foreach ($itunesJson['results'] as $itunesFeed) { - $lastPub = date_format(new \DateTime($itunesFeed['releaseDate']), DATE_ATOM); - - if ($lastPub) { - $podcasts[] = [ - 'id' => $itunesFeed['id'], - 'title' => $itunesFeed['trackName'], - 'author' => $itunesFeed['artistName'], - 'image' => $itunesFeed['artworkUrl600'], - 'provider_url' => $itunesFeed['trackViewUrl'], - 'feed_url' => $itunesFeed['feedUrl'], - 'last_pub' => $lastPub, - 'nb_episodes' => $itunesFeed['trackCount'], - ]; - } - } - } - } catch (\Exception $e) { } usort($podcasts, fn (array $a, array $b) => new \DateTime((string) $b['last_pub']) <=> new \DateTime((string) $a['last_pub'])); diff --git a/lib/Controller/TopController.php b/lib/Controller/TopController.php index 85a454a..5ecf185 100644 --- a/lib/Controller/TopController.php +++ b/lib/Controller/TopController.php @@ -5,21 +5,17 @@ declare(strict_types=1); namespace OCA\RePod\Controller; use OCA\RePod\AppInfo\Application; +use OCA\RePod\Service\FyydService; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; use OCP\AppFramework\Http\JSONResponse; -use OCP\Http\Client\IClientService; use OCP\IRequest; -use OCP\IUserSession; -use OCP\L10N\IFactory; class TopController extends Controller { public function __construct( IRequest $request, - private IClientService $clientService, - private IFactory $l10n, - private IUserSession $userSession + private FyydService $fyydService ) { parent::__construct(Application::APP_ID, $request); } @@ -28,34 +24,13 @@ class TopController extends Controller * @NoAdminRequired * @NoCSRFRequired */ - public function index(int $count = 10): JSONResponse + public function index(): JSONResponse { - $language = 'en'; - try { - $langClient = $this->clientService->newClient(); - $langResponse = $langClient->get('https://api.fyyd.de/0.2/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'; - } - } catch (\Exception $e) { - } + $response = $this->fyydService->hot(); + $json = (array) json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR); - try { - $podcastClient = $this->clientService->newClient(); - $podcastReponse = $podcastClient->get('https://api.fyyd.de/0.2/feature/podcast/hot', [ - 'query' => [ - 'count' => $count, - 'language' => $language, - ], - ]); - $podcastJson = (array) json_decode((string) $podcastReponse->getBody(), true, flags: JSON_THROW_ON_ERROR); - - return new JSONResponse($podcastJson, $podcastReponse->getStatusCode()); + return new JSONResponse($json, $response->getStatusCode()); } catch (\Exception $e) { return new JSONResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } diff --git a/lib/Service/FyydService.php b/lib/Service/FyydService.php new file mode 100644 index 0000000..03b7e2e --- /dev/null +++ b/lib/Service/FyydService.php @@ -0,0 +1,79 @@ +clientService->newClient(); + $response = $client->get('https://api.fyyd.de/0.2/search/podcast', [ + 'query' => [ + 'title' => $value, + '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'; + + try { + $langClient = $this->clientService->newClient(); + $langResponse = $langClient->get('https://api.fyyd.de/0.2/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'; + } + } catch (\Exception $e) { + } + + $podcastClient = $this->clientService->newClient(); + + return $podcastClient->get('https://api.fyyd.de/0.2/feature/podcast/hot', [ + 'query' => [ + 'language' => $language, + ], + ]); + } +} diff --git a/lib/Service/IProvider.php b/lib/Service/IProvider.php new file mode 100644 index 0000000..56c14a6 --- /dev/null +++ b/lib/Service/IProvider.php @@ -0,0 +1,23 @@ + + */ + public function search(string $value): array; +} diff --git a/lib/Service/ItunesService.php b/lib/Service/ItunesService.php new file mode 100644 index 0000000..1700a35 --- /dev/null +++ b/lib/Service/ItunesService.php @@ -0,0 +1,53 @@ +clientService->newClient(); + $response = $client->get('https://itunes.apple.com/search', [ + 'query' => [ + 'media' => 'podcast', + 'term' => $value, + 'lang' => $this->l10n->getUserLanguage($this->userSession->getUser()), + ], + ]); + $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['results'] as $feed) { + $podcasts[] = [ + 'provider' => 'itunes', + 'id' => $feed['id'], + 'title' => $feed['trackName'], + 'author' => $feed['artistName'], + 'image' => $feed['artworkUrl600'], + 'provider_url' => $feed['trackViewUrl'], + 'feed_url' => $feed['feedUrl'], + 'last_pub' => $feed['releaseDate'], + 'nb_episodes' => $feed['trackCount'], + ]; + } + } + + return $podcasts; + } +} diff --git a/src/components/TopList.vue b/src/components/TopList.vue index 611d4be..517080b 100644 --- a/src/components/TopList.vue +++ b/src/components/TopList.vue @@ -1,12 +1,7 @@ @@ -76,16 +61,6 @@ export default { flex-basis: 15%; } - .select { - min-width: 160px; - } - - .more { - display: flex; - justify-content: space-between; - align-items: center; - } - .caption { float: right; font-size: small; diff --git a/src/views/Discover.vue b/src/views/Discover.vue index 2ebd984..d91c03c 100644 --- a/src/views/Discover.vue +++ b/src/views/Discover.vue @@ -37,7 +37,7 @@ export default {