repod/lib/Controller/TopController.php

63 lines
1.8 KiB
PHP
Raw Normal View History

2023-07-02 22:12:40 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use Exception;
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;
2023-07-02 22:52:14 +00:00
use OCP\IUserSession;
use OCP\L10N\IFactory;
2023-07-02 22:12:40 +00:00
class TopController extends Controller
{
2023-07-02 22:52:14 +00:00
public function __construct(
IRequest $request,
private IClientService $clientService,
2023-07-04 15:43:58 +00:00
private IFactory $l10n,
2023-07-02 22:52:14 +00:00
private IUserSession $userSession
) {
2023-07-02 22:12:40 +00:00
parent::__construct(Application::APP_ID, $request);
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
2023-07-25 19:44:25 +00:00
public function index(int $count = 10): JSONResponse {
2023-07-25 23:26:46 +00:00
$language = 'en';
2023-07-02 22:12:40 +00:00
try {
2023-07-25 19:44:25 +00:00
$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'])) {
2023-07-25 23:26:46 +00:00
$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';
2023-07-25 19:44:25 +00:00
}
} catch (Exception $e) {
}
2023-07-02 22:52:14 +00:00
2023-07-25 19:44:25 +00:00
try {
$podcastClient = $this->clientService->newClient();
2023-07-25 23:26:46 +00:00
$podcastReponse = $podcastClient->get("https://api.fyyd.de/0.2/feature/podcast/hot", [
'query' => [
'count' => $count,
'language' => $language
]
]);
2023-07-25 19:44:25 +00:00
$podcastJson = (array) json_decode((string) $podcastReponse->getBody(), true, flags: JSON_THROW_ON_ERROR);
return new JSONResponse($podcastJson, $podcastReponse->getStatusCode());
2023-07-02 22:12:40 +00:00
} catch (Exception $e) {
2023-07-02 22:20:18 +00:00
return new JSONResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
2023-07-02 22:12:40 +00:00
}
}
}