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;
|
|
|
|
use OCA\RePod\AppInfo\Application;
|
|
|
|
use OCA\RePod\Service\UserService;
|
|
|
|
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 UserService $userService,
|
|
|
|
private PodcastDataReader $podcastDataReader
|
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_ID, $request);
|
|
|
|
}
|
|
|
|
|
2023-08-22 18:14:15 +00:00
|
|
|
public function index(string $url): JSONResponse
|
2023-08-22 17:41:17 +00:00
|
|
|
{
|
2023-08-23 07:08:13 +00:00
|
|
|
$podcasts = $this->podcastDataReader->getCachedOrFetchPodcastData($url, $this->userService->getUserUID());
|
2023-08-22 17:41:17 +00:00
|
|
|
|
2023-08-23 07:08:13 +00:00
|
|
|
if ($podcasts) {
|
|
|
|
return new JSONResponse(['data' => $podcasts]);
|
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-22 17:41:17 +00:00
|
|
|
$statusCode = $feed->getStatusCode();
|
|
|
|
|
|
|
|
if ($statusCode < 200 || $statusCode >= 300) {
|
|
|
|
throw new \ErrorException("Web request returned non-2xx status code: {$statusCode}");
|
|
|
|
}
|
|
|
|
|
2023-08-23 07:08:13 +00:00
|
|
|
$podcasts = PodcastData::parseRssXml((string) $feed->getBody());
|
2023-08-22 17:41:17 +00:00
|
|
|
|
2023-08-24 15:43:10 +00:00
|
|
|
return new JSONResponse(['data' => $podcasts], $statusCode);
|
2023-08-22 17:41:17 +00:00
|
|
|
}
|
|
|
|
}
|