repod/lib/Service/ItunesService.php

53 lines
1.2 KiB
PHP
Raw Normal View History

2023-07-28 00:37:57 +00:00
<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
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-07-28 00:37:57 +00:00
) {
}
public function search(string $value): array
{
$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);
if (array_key_exists('data', $json) && is_array($json['data'])) {
/** @var string[] $feed */
foreach ($json['results'] as $feed) {
$podcasts[] = [
'provider' => 'itunes',
'id' => $feed['id'],
'title' => $feed['trackName'],
'author' => $feed['artistName'],
'image' => $feed['artworkUrl600'],
'provider_url' => $feed['trackViewUrl'],
'feed_url' => $feed['feedUrl'],
'last_pub' => $feed['releaseDate'],
'nb_episodes' => $feed['trackCount'],
];
}
}
return $podcasts;
}
}