RePod-Nextcloud-App/lib/Service/ItunesService.php

49 lines
1.1 KiB
PHP
Raw Normal View History

2023-07-28 02:37:57 +02:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
2023-08-22 19:41:17 +02:00
use OCA\GPodderSync\Core\PodcastData\PodcastData;
2023-07-28 02:37:57 +02:00
use OCP\Http\Client\IClientService;
2024-01-18 11:43:58 +01:00
class ItunesService implements IPodProvider
2023-07-28 02:37:57 +02:00
{
2023-07-28 03:00:38 +02:00
private const BASE_URL = 'https://itunes.apple.com/';
2023-07-28 02:37:57 +02:00
public function __construct(
private IClientService $clientService,
2023-07-28 03:00:38 +02:00
private UserService $userService
2023-12-23 17:25:20 +01:00
) {}
2023-07-28 02:37:57 +02:00
2023-12-23 17:25:20 +01:00
public function search(string $value): array {
2023-07-28 02:37:57 +02:00
$podcasts = [];
$client = $this->clientService->newClient();
2023-07-28 03:00:38 +02:00
$response = $client->get(self::BASE_URL.'search', [
2023-07-28 02:37:57 +02:00
'query' => [
'media' => 'podcast',
'term' => $value,
2023-07-28 03:00:38 +02:00
'country' => $this->userService->getCountryCode(),
2023-07-28 02:37:57 +02:00
],
]);
$json = (array) json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);
2023-07-28 03:09:06 +02:00
if (array_key_exists('results', $json) && is_array($json['results'])) {
2023-07-28 02:37:57 +02:00
/** @var string[] $feed */
foreach ($json['results'] as $feed) {
2023-08-22 19:41:17 +02:00
$podcasts[] = new PodcastData(
$feed['trackName'],
$feed['artistName'],
$feed['feedUrl'],
$feed['primaryGenreName'],
$feed['artworkUrl600'],
strtotime($feed['releaseDate'])
);
2023-07-28 02:37:57 +02:00
}
}
return $podcasts;
}
}