feat: enhance YouTube API integration by adding support for topic channel ID and improving release sync functionality with localization updates
This commit is contained in:
+146
-10
@@ -10,39 +10,87 @@ use Illuminate\Support\Str;
|
||||
|
||||
class YouTubeService
|
||||
{
|
||||
protected ?string $resolvedTopicChannelId = null;
|
||||
|
||||
protected ?string $resolvedReleasesSource = null;
|
||||
|
||||
public function __construct(
|
||||
protected ?string $apiKey = null,
|
||||
protected ?string $channelId = null,
|
||||
protected ?string $topicChannelId = null,
|
||||
protected ?string $playlistId = null,
|
||||
) {
|
||||
$this->apiKey = $apiKey ?? config('services.youtube.api_key');
|
||||
$this->channelId = $channelId ?? config('services.youtube.channel_id');
|
||||
$this->topicChannelId = $topicChannelId ?? config('services.youtube.topic_channel_id');
|
||||
$this->playlistId = $playlistId ?? config('services.youtube.playlist_id');
|
||||
}
|
||||
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
return filled($this->apiKey)
|
||||
&& (filled($this->channelId) || filled($this->playlistId));
|
||||
&& (filled($this->channelId) || filled($this->topicChannelId) || filled($this->playlistId));
|
||||
}
|
||||
|
||||
public function getReleasesSourceLabel(): string
|
||||
{
|
||||
$this->resolveReleasesPlaylistId();
|
||||
|
||||
return $this->resolvedReleasesSource ?? '-';
|
||||
}
|
||||
|
||||
public function resolveReleasesPlaylistId(): ?string
|
||||
{
|
||||
if (filled($this->playlistId)) {
|
||||
$this->resolvedReleasesSource = 'playlist:' . $this->playlistId;
|
||||
|
||||
return $this->playlistId;
|
||||
}
|
||||
|
||||
$channelId = $this->channelId;
|
||||
$topicChannelId = $this->resolveTopicChannelId();
|
||||
|
||||
if (filled($topicChannelId)) {
|
||||
$playlistId = $this->channelToUploadsPlaylistId($topicChannelId);
|
||||
$this->resolvedReleasesSource = 'topic:' . $topicChannelId;
|
||||
|
||||
return $playlistId;
|
||||
}
|
||||
|
||||
if (filled($this->channelId)) {
|
||||
Log::warning('YouTube Topic channel not found, falling back to main channel uploads', [
|
||||
'channel_id' => $this->channelId,
|
||||
]);
|
||||
|
||||
$playlistId = $this->channelToUploadsPlaylistId($this->channelId);
|
||||
$this->resolvedReleasesSource = 'main:' . $this->channelId;
|
||||
|
||||
return $playlistId;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function resolveTopicChannelId(): ?string
|
||||
{
|
||||
if ($this->resolvedTopicChannelId !== null) {
|
||||
return $this->resolvedTopicChannelId ?: null;
|
||||
}
|
||||
|
||||
if (filled($this->topicChannelId)) {
|
||||
$this->resolvedTopicChannelId = $this->topicChannelId;
|
||||
|
||||
return $this->resolvedTopicChannelId;
|
||||
}
|
||||
|
||||
if (blank($this->channelId)) {
|
||||
$this->resolvedTopicChannelId = '';
|
||||
|
||||
if (blank($channelId)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (str_starts_with($channelId, 'UC')) {
|
||||
return 'UU' . substr($channelId, 2);
|
||||
}
|
||||
$this->resolvedTopicChannelId = $this->discoverTopicChannelId($this->channelId) ?? '';
|
||||
|
||||
return $channelId;
|
||||
return $this->resolvedTopicChannelId ?: null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +106,7 @@ class YouTubeService
|
||||
|
||||
$releases = [];
|
||||
$pageToken = null;
|
||||
$distributorFilter = config('services.youtube.distributor_filter');
|
||||
|
||||
do {
|
||||
$params = [
|
||||
@@ -76,6 +125,7 @@ class YouTubeService
|
||||
if ($response->failed()) {
|
||||
Log::error('YouTube releases fetch failed', [
|
||||
'playlist_id' => $playlistId,
|
||||
'source' => $this->resolvedReleasesSource,
|
||||
'status' => $response->status(),
|
||||
'body' => $response->body(),
|
||||
]);
|
||||
@@ -90,7 +140,7 @@ class YouTubeService
|
||||
$items = $data['items'] ?? [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
$normalized = $this->normalizeReleaseItem($item);
|
||||
$normalized = $this->normalizeReleaseItem($item, $distributorFilter);
|
||||
|
||||
if ($normalized) {
|
||||
$releases[$normalized['video_id']] = $normalized;
|
||||
@@ -103,15 +153,97 @@ class YouTubeService
|
||||
return array_values($releases);
|
||||
}
|
||||
|
||||
protected function discoverTopicChannelId(string $mainChannelId): ?string
|
||||
{
|
||||
$channel = $this->getChannel($mainChannelId);
|
||||
|
||||
if (! $channel) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$artistTitle = trim($channel['snippet']['title'] ?? '');
|
||||
|
||||
if (blank($artistTitle)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$expectedTopicTitle = $artistTitle . ' - Topic';
|
||||
|
||||
$response = $this->client()->get('https://www.googleapis.com/youtube/v3/search', [
|
||||
'key' => $this->apiKey,
|
||||
'part' => 'snippet',
|
||||
'type' => 'channel',
|
||||
'q' => $expectedTopicTitle,
|
||||
'maxResults' => 5,
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
Log::warning('YouTube topic channel search failed', [
|
||||
'channel_id' => $mainChannelId,
|
||||
'query' => $expectedTopicTitle,
|
||||
'status' => $response->status(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach ($response->json('items') ?? [] as $item) {
|
||||
$channelId = $item['snippet']['channelId'] ?? $item['id']['channelId'] ?? null;
|
||||
$title = trim($item['snippet']['title'] ?? '');
|
||||
|
||||
if (blank($channelId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($title === $expectedTopicTitle || str_ends_with($title, ' - Topic')) {
|
||||
return $channelId;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
protected function getChannel(string $channelId): ?array
|
||||
{
|
||||
$response = $this->client()->get('https://www.googleapis.com/youtube/v3/channels', [
|
||||
'key' => $this->apiKey,
|
||||
'id' => $channelId,
|
||||
'part' => 'snippet,contentDetails',
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $response->json('items.0');
|
||||
}
|
||||
|
||||
protected function channelToUploadsPlaylistId(string $channelId): string
|
||||
{
|
||||
if (str_starts_with($channelId, 'UU') || str_starts_with($channelId, 'PL')) {
|
||||
return $channelId;
|
||||
}
|
||||
|
||||
if (str_starts_with($channelId, 'UC')) {
|
||||
return 'UU' . substr($channelId, 2);
|
||||
}
|
||||
|
||||
return $channelId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $item
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
protected function normalizeReleaseItem(array $item): ?array
|
||||
protected function normalizeReleaseItem(array $item, ?string $distributorFilter = null): ?array
|
||||
{
|
||||
$snippet = $item['snippet'] ?? [];
|
||||
$videoId = $snippet['resourceId']['videoId'] ?? null;
|
||||
$title = trim($snippet['title'] ?? '');
|
||||
$description = trim($snippet['description'] ?? '');
|
||||
|
||||
if (blank($videoId) || blank($title)) {
|
||||
return null;
|
||||
@@ -121,6 +253,10 @@ class YouTubeService
|
||||
return null;
|
||||
}
|
||||
|
||||
if (filled($distributorFilter) && ! str_contains($description, $distributorFilter)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$thumbnails = $snippet['thumbnails'] ?? [];
|
||||
$coverUrl = $thumbnails['maxres']['url']
|
||||
?? $thumbnails['standard']['url']
|
||||
@@ -132,7 +268,7 @@ class YouTubeService
|
||||
return [
|
||||
'video_id' => $videoId,
|
||||
'title' => $title,
|
||||
'description' => trim($snippet['description'] ?? ''),
|
||||
'description' => $description,
|
||||
'cover_url' => $coverUrl,
|
||||
'published_at' => $snippet['publishedAt'] ?? null,
|
||||
'youtube_url' => 'https://www.youtube.com/watch?v=' . $videoId,
|
||||
|
||||
Reference in New Issue
Block a user