repod/lib/Service/ItunesService.php
Michel Roux 098db9f09e
All checks were successful
repod / nextcloud (push) Successful in 1m3s
repod / nodejs (push) Successful in 1m20s
Refacto API to be pseudo compatible with gpodder
2023-08-01 23:18:37 +02:00

56 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Service;
use OCP\Http\Client\IClientService;
class ItunesService implements IProvider
{
private const BASE_URL = 'https://itunes.apple.com/';
public function __construct(
private IClientService $clientService,
private 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[] = [
'id' => $feed['id'],
'provider' => 'itunes',
'website' => $feed['trackViewUrl'],
'description' => $feed['primaryGenreName'],
'title' => $feed['trackName'],
'author' => $feed['artistName'],
'url' => $feed['feedUrl'],
'position_last_week' => null,
'mygpo_link' => $feed['trackViewUrl'],
'logo_url' => $feed['artworkUrl600'],
'lastpub' => $feed['releaseDate'],
'episode_count' => $feed['trackCount'],
];
}
}
return $podcasts;
}
}