Refacto to services
All checks were successful
repod / nextcloud (push) Successful in 44s
repod / nodejs (push) Successful in 1m15s

This commit is contained in:
Michel Roux 2023-07-28 02:37:57 +02:00
parent 5a590bd210
commit 4503df9d33
8 changed files with 184 additions and 139 deletions

View File

@ -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'],
],
];

View File

@ -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']));

View File

@ -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);
}

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
use OCP\Http\Client\IResponse;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class FyydService implements IProvider
{
public function __construct(
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
) {
}
public function search(string $value): array
{
$podcasts = [];
$client = $this->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,
],
]);
}
}

23
lib/Service/IProvider.php Normal file
View File

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
interface IProvider
{
/**
* @return array<array{
* provider: string,
* id: string,
* title: string,
* author: string,
* image: string,
* provider_url: string,
* feed_url: string,
* last_pub: string,
* nb_episodes: string
* }>
*/
public function search(string $value): array;
}

View File

@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class ItunesService implements IProvider
{
public function __construct(
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
) {
}
public function search(string $value): array
{
$podcasts = [];
$client = $this->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;
}
}

View File

@ -1,12 +1,7 @@
<template>
<fragment>
<p class="more">
<p>
<span>{{ t('Discover') }}</span>
<NcSelect v-model="count"
class="select"
:components="Deselect"
:options="[...Array(10).keys()].map(x=>(x+1)*10)"
@input="fetch" />
</p>
<p>
<NcLoadingIcon v-if="loading" />
@ -21,7 +16,7 @@
</template>
<script>
import { NcLoadingIcon, NcSelect } from '@nextcloud/vue'
import { NcLoadingIcon } from '@nextcloud/vue'
import TopItem from './TopItem.vue'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
@ -31,35 +26,25 @@ export default {
name: 'TopList',
components: {
NcLoadingIcon,
NcSelect,
TopItem,
},
data() {
return {
tops: [],
loading: true,
count: 10,
Deselect: {
render: null,
},
}
},
async mounted() {
await this.fetch()
},
methods: {
async fetch() {
try {
this.loading = true
const top = await axios.get(generateUrl('/apps/repod/top/{count}', { count: this.count }))
this.tops = top.data.data
} catch (e) {
console.error(e)
showError(t('Could not fetch tops'))
} finally {
this.loading = false
}
},
try {
this.loading = true
const top = await axios.get(generateUrl('/apps/repod/top'))
this.tops = top.data.data
} catch (e) {
console.error(e)
showError(t('Could not fetch tops'))
} finally {
this.loading = false
}
},
}
</script>
@ -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;

View File

@ -37,7 +37,7 @@ export default {
<style scoped>
.main {
margin: 22px 55px;
margin: 15px 51px;
display: flex;
flex-direction: column;
gap: 1rem;