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);
|
|
|
|
|
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) {
|
|
|
|
$podcasts[] = [
|
|
|
|
'id' => $feed['id'],
|
2023-08-01 21:18:37 +00:00
|
|
|
'provider' => 'itunes',
|
|
|
|
'website' => $feed['trackViewUrl'],
|
|
|
|
'description' => $feed['primaryGenreName'],
|
2023-07-28 00:37:57 +00:00
|
|
|
'title' => $feed['trackName'],
|
|
|
|
'author' => $feed['artistName'],
|
2023-08-01 21:18:37 +00:00
|
|
|
'url' => $feed['feedUrl'],
|
|
|
|
'position_last_week' => null,
|
|
|
|
'mygpo_link' => $feed['trackViewUrl'],
|
|
|
|
'logo_url' => $feed['artworkUrl600'],
|
|
|
|
'lastpub' => $feed['releaseDate'],
|
|
|
|
'episode_count' => $feed['trackCount'],
|
2023-07-28 00:37:57 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $podcasts;
|
|
|
|
}
|
|
|
|
}
|