2023-07-03 00:12:40 +02: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-03 00:52:14 +02:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\L10N\IFactory;
|
2023-07-03 00:12:40 +02:00
|
|
|
|
|
|
|
class TopController extends Controller
|
|
|
|
{
|
2023-07-03 00:52:14 +02:00
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
private IClientService $clientService,
|
2023-07-04 17:43:58 +02:00
|
|
|
private IFactory $l10n,
|
2023-07-03 00:52:14 +02:00
|
|
|
private IUserSession $userSession
|
|
|
|
) {
|
2023-07-03 00:12:40 +02:00
|
|
|
parent::__construct(Application::APP_ID, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*/
|
|
|
|
public function index(int $limit = 10): JSONResponse {
|
|
|
|
if (!in_array($limit, [10, 25, 50])) {
|
2023-07-03 00:20:18 +02:00
|
|
|
return new JSONResponse(['Invalid limit, can be 10, 25 or 50.'], Http::STATUS_BAD_REQUEST);
|
2023-07-03 00:12:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2023-07-04 17:43:58 +02:00
|
|
|
$lang = $this->l10n->getUserLanguage($this->userSession->getUser());
|
2023-07-03 00:52:14 +02:00
|
|
|
$lang = explode('_', $lang);
|
|
|
|
$lang = count($lang) > 1 ? $lang[1] : $lang[0];
|
|
|
|
$lang = $lang === 'en' ? 'us' : $lang;
|
|
|
|
|
2023-07-03 00:12:40 +02:00
|
|
|
$client = $this->clientService->newClient();
|
2023-07-03 00:52:14 +02:00
|
|
|
$response = $client->get("https://rss.applemarketingtools.com/api/v2/{$lang}/podcasts/top/{$limit}/podcasts.json");
|
2023-07-03 00:20:18 +02:00
|
|
|
/** @var array $json */
|
2023-07-04 17:43:58 +02:00
|
|
|
$json = json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);
|
2023-07-03 00:12:40 +02:00
|
|
|
return new JSONResponse($json, $response->getStatusCode());
|
|
|
|
} catch (Exception $e) {
|
2023-07-03 00:20:18 +02:00
|
|
|
return new JSONResponse([$e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR);
|
2023-07-03 00:12:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|