repod/lib/Service/ItunesService.php
Michel Roux 1d8ea08798
All checks were successful
repod / xml (push) Successful in 22s
repod / php (push) Successful in 1m15s
repod / nodejs (push) Successful in 1m6s
repod / release (push) Has been skipped
style: 🎨 add nextcloud/rector
2024-11-12 10:11:21 +01:00

49 lines
1.1 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCA\GPodderSync\Core\PodcastData\PodcastData;
use OCP\Http\Client\IClientService;
class ItunesService implements IPodProvider
{
private const BASE_URL = 'https://itunes.apple.com/';
public function __construct(
private readonly IClientService $clientService,
private readonly UserService $userService
) {}
public function search(string $value): array {
$podcasts = [];
$client = $this->clientService->newClient();
$response = $client->get(self::BASE_URL.'search', [
'query' => [
'media' => 'podcast',
'term' => $value,
'country' => $this->userService->getCountryCode(),
],
]);
$json = (array) json_decode((string) $response->getBody(), true, flags: JSON_THROW_ON_ERROR);
if (array_key_exists('results', $json) && is_array($json['results'])) {
/** @var string[] $feed */
foreach ($json['results'] as $feed) {
$podcasts[] = new PodcastData(
$feed['trackName'],
$feed['artistName'],
$feed['feedUrl'],
$feed['primaryGenreName'],
$feed['artworkUrl600'],
strtotime($feed['releaseDate'])
);
}
}
return $podcasts;
}
}