repod/lib/Controller/SearchController.php

56 lines
1.3 KiB
PHP
Raw Normal View History

2023-07-25 23:26:46 +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\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
class SearchController extends Controller
{
public function __construct(
IRequest $request,
private IClientService $clientService
) {
parent::__construct(Application::APP_ID, $request);
}
public function index(string $value): JSONResponse {
$podcasts = [];
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) {
$podcasts[] = [
'id' => $fyydFeed['id'],
'title' => $fyydFeed['title'],
'description' => $fyydFeed['description'],
'image' => $fyydFeed['imgURL'],
'url' => $fyydFeed['xmlURL'],
'lang' => $fyydFeed['language'],
'lastpub' => $fyydFeed['lastpub']
];
}
}
} catch (Exception $e) {
}
return new JSONResponse($podcasts);
}
}