repod/lib/Service/ItunesService.php
Michel Roux 4503df9d33
All checks were successful
repod / nextcloud (push) Successful in 44s
repod / nodejs (push) Successful in 1m15s
Refacto to services
2023-07-28 02:37:57 +02:00

54 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
use OCP\IUserSession;
use OCP\L10N\IFactory;
class ItunesService implements IProvider
{
public function __construct(
private IClientService $clientService,
private IFactory $l10n,
private IUserSession $userSession
) {
}
public function search(string $value): array
{
$podcasts = [];
$client = $this->clientService->newClient();
$response = $client->get('https://itunes.apple.com/search', [
'query' => [
'media' => 'podcast',
'term' => $value,
'lang' => $this->l10n->getUserLanguage($this->userSession->getUser()),
],
]);
$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;
}
}