feat: update YouTube API integration with enhanced release date parsing, improved topic channel resolution, and localization updates for music productions
This commit is contained in:
@@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class YouTubeService
|
||||
{
|
||||
@@ -168,13 +169,14 @@ class YouTubeService
|
||||
}
|
||||
|
||||
$expectedTopicTitle = $artistTitle . ' - Topic';
|
||||
$mainThumbnail = $channel['snippet']['thumbnails']['default']['url'] ?? null;
|
||||
|
||||
$response = $this->client()->get('https://www.googleapis.com/youtube/v3/search', [
|
||||
'key' => $this->apiKey,
|
||||
'part' => 'snippet',
|
||||
'type' => 'channel',
|
||||
'q' => $expectedTopicTitle,
|
||||
'maxResults' => 5,
|
||||
'maxResults' => 10,
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
@@ -187,22 +189,77 @@ class YouTubeService
|
||||
return null;
|
||||
}
|
||||
|
||||
$topicCandidates = [];
|
||||
|
||||
foreach ($response->json('items') ?? [] as $item) {
|
||||
$channelId = $item['snippet']['channelId'] ?? $item['id']['channelId'] ?? null;
|
||||
$title = trim($item['snippet']['title'] ?? '');
|
||||
|
||||
if (blank($channelId)) {
|
||||
if (blank($channelId) || $channelId === $mainChannelId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($title === $expectedTopicTitle || str_ends_with($title, ' - Topic')) {
|
||||
if ($this->titlesMatch($title, $expectedTopicTitle)) {
|
||||
Log::info('YouTube topic channel resolved by exact title match', [
|
||||
'main_channel_id' => $mainChannelId,
|
||||
'topic_channel_id' => $channelId,
|
||||
'title' => $title,
|
||||
]);
|
||||
|
||||
return $channelId;
|
||||
}
|
||||
|
||||
if (str_ends_with($title, ' - Topic')) {
|
||||
$topicCandidates[] = [
|
||||
'channel_id' => $channelId,
|
||||
'title' => $title,
|
||||
'thumbnail' => $item['snippet']['thumbnails']['default']['url'] ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($topicCandidates as $candidate) {
|
||||
if ($this->thumbnailsMatch($mainThumbnail, $candidate['thumbnail'])) {
|
||||
Log::info('YouTube topic channel resolved by avatar match', [
|
||||
'main_channel_id' => $mainChannelId,
|
||||
'topic_channel_id' => $candidate['channel_id'],
|
||||
'title' => $candidate['title'],
|
||||
]);
|
||||
|
||||
return $candidate['channel_id'];
|
||||
}
|
||||
}
|
||||
|
||||
Log::warning('YouTube topic channel could not be resolved safely', [
|
||||
'main_channel_id' => $mainChannelId,
|
||||
'expected_title' => $expectedTopicTitle,
|
||||
'candidates' => collect($topicCandidates)->pluck('title')->all(),
|
||||
]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function titlesMatch(string $left, string $right): bool
|
||||
{
|
||||
return mb_strtolower(trim($left)) === mb_strtolower(trim($right));
|
||||
}
|
||||
|
||||
protected function thumbnailsMatch(?string $left, ?string $right): bool
|
||||
{
|
||||
if (blank($left) || blank($right)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->normalizeThumbnailUrl($left) === $this->normalizeThumbnailUrl($right);
|
||||
}
|
||||
|
||||
protected function normalizeThumbnailUrl(string $url): string
|
||||
{
|
||||
$path = parse_url($url, PHP_URL_PATH) ?: $url;
|
||||
|
||||
return preg_replace('/=s\d+-/', '=s88-', $path) ?? $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>|null
|
||||
*/
|
||||
@@ -271,10 +328,28 @@ class YouTubeService
|
||||
'description' => $description,
|
||||
'cover_url' => $coverUrl,
|
||||
'published_at' => $snippet['publishedAt'] ?? null,
|
||||
'release_date' => $this->parseReleaseDate($description, $snippet['publishedAt'] ?? null),
|
||||
'youtube_url' => 'https://www.youtube.com/watch?v=' . $videoId,
|
||||
];
|
||||
}
|
||||
|
||||
public function parseReleaseDate(string $description, ?string $publishedAt = null): ?string
|
||||
{
|
||||
if (preg_match('/Released on:\s*(\d{4}-\d{2}-\d{2})/i', $description, $matches)) {
|
||||
return Carbon::parse($matches[1])->toDateString();
|
||||
}
|
||||
|
||||
if (preg_match('/Release date:\s*(\d{4}-\d{2}-\d{2})/i', $description, $matches)) {
|
||||
return Carbon::parse($matches[1])->toDateString();
|
||||
}
|
||||
|
||||
if (filled($publishedAt)) {
|
||||
return Carbon::parse($publishedAt)->toDateString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function downloadCoverImage(?string $imageUrl, string $videoId): ?string
|
||||
{
|
||||
if (blank($imageUrl)) {
|
||||
|
||||
Reference in New Issue
Block a user