repod/lib/Controller/TopController.php
Michel Roux 2a4662e85b
All checks were successful
repod / nextcloud (push) Successful in 1m6s
repod / nodejs (push) Successful in 1m22s
search in PHP completed
2023-07-27 23:01:24 +02:00

64 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\RePod\AppInfo\Application;
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
) {
parent::__construct(Application::APP_ID, $request);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function index(int $count = 10): 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) {
}
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());
} catch (\Exception $e) {
return new JSONResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
}
}
}