repod/lib/Service/ItunesService.php

49 lines
1.1 KiB
PHP
Raw Normal View History

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