repod/lib/Controller/SearchController.php

106 lines
3.0 KiB
PHP
Raw Normal View History

2023-07-25 23:26:46 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\RePod\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
2023-07-27 21:01:24 +00:00
use OCP\IUserSession;
use OCP\L10N\IFactory;
2023-07-25 23:26:46 +00:00
class SearchController extends Controller
{
public function __construct(
IRequest $request,
2023-07-27 21:01:24 +00:00
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
2023-07-25 23:26:46 +00:00
) {
parent::__construct(Application::APP_ID, $request);
}
2023-07-27 21:01:24 +00:00
public function index(string $value): JSONResponse
{
2023-07-25 23:26:46 +00:00
$podcasts = [];
try {
$fyydClient = $this->clientService->newClient();
2023-07-27 21:01:24 +00:00
$fyydReponse = $fyydClient->get('https://api.fyyd.de/0.2/search/podcast', [
2023-07-25 23:26:46 +00:00
'query' => [
'title' => $value,
2023-07-27 21:01:24 +00:00
'term' => $value,
],
2023-07-25 23:26:46 +00:00
]);
$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) {
2023-07-27 21:01:24 +00:00
$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'],
];
}
2023-07-25 23:26:46 +00:00
}
}
2023-07-27 21:01:24 +00:00
} catch (\Exception $e) {
2023-07-25 23:26:46 +00:00
}
2023-07-27 21:01:24 +00:00
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) {
}
/**
* string[] $a
* string[] $b.
*/
usort($podcasts, fn (array $a, array $b) => $a['last_pub'] <=> $b['last_pub']);
$podcasts = array_intersect_key($podcasts, array_unique(array_map(fn (array $feed) => $feed['feed_url'], $podcasts)));
2023-07-25 23:26:46 +00:00
return new JSONResponse($podcasts);
}
}