repod/lib/Controller/PodcastController.php

48 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2023-08-22 17:41:17 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Controller;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
2023-08-22 17:41:17 +00:00
use OCA\RePod\AppInfo\Application;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\JSONResponse;
use OCP\Http\Client\IClientService;
use OCP\IRequest;
2023-08-24 10:48:10 +00:00
class PodcastController extends Controller
2023-08-22 17:41:17 +00:00
{
public function __construct(
IRequest $request,
private IClientService $clientService,
private PodcastDataReader $podcastDataReader
2023-08-22 17:41:17 +00:00
) {
parent::__construct(Application::APP_ID, $request);
}
2023-12-23 16:25:20 +00:00
public function index(string $url): JSONResponse {
try {
$podcast = $this->podcastDataReader->tryGetCachedPodcastData($url);
} catch (\Exception $e) {
$podcast = null;
}
if ($podcast) {
return new JSONResponse($podcast);
}
2023-08-22 17:41:17 +00:00
$client = $this->clientService->newClient();
2023-08-22 18:14:15 +00:00
$feed = $client->get($url);
2023-08-24 18:53:54 +00:00
$podcast = PodcastData::parseRssXml((string) $feed->getBody());
try {
$this->podcastDataReader->trySetCachedPodcastData($url, $podcast);
} catch (\Exception $e) {
}
2023-08-22 17:41:17 +00:00
return new JSONResponse($podcast, $feed->getStatusCode());
2023-08-22 17:41:17 +00:00
}
}