From b9c098cde319975f6c87e1aee5be2052182cb9e4 Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Tue, 15 Jul 2025 10:42:49 +0200 Subject: [PATCH] improve error handling for youtube requests, skip video category check if unnecessary --- src/lib/server/timeline.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/lib/server/timeline.ts b/src/lib/server/timeline.ts index 944c83d..b5be6a7 100644 --- a/src/lib/server/timeline.ts +++ b/src/lib/server/timeline.ts @@ -66,7 +66,11 @@ export class TimelineReader { const youtubeVideoUrl = new URL(`https://www.googleapis.com/youtube/v3/videos?${searchParams}`); const resp = await fetch(youtubeVideoUrl); const respObj = await resp.json(); - if (!respObj.items.length) { + if (respObj.error) { + this.logger.warn('YT API error', respObj.error); + return false; + } + if (!respObj.items?.length) { this.logger.warn('Could not find video with id', videoId); return false; } @@ -76,7 +80,7 @@ export class TimelineReader { this.logger.warn('Could not load snippet for video', videoId, item); return false; } - if (item.snippet.tags?.includes('music')) { + if (item.snippet?.tags?.includes('music')) { return true; } @@ -159,8 +163,14 @@ export class TimelineReader { if (!odesliInfo || !odesliInfo.entitiesByUniqueId || !odesliInfo.entityUniqueId) { return null; } + const spotify: Platform = 'spotify'; + const tidal: Platform = 'tidal'; + const deezer: Platform = 'deezer'; + const tidalId = odesliInfo.linksByPlatform[tidal]?.entityUniqueId; + const tidalUri = tidalId ? odesliInfo.entitiesByUniqueId[tidalId].id : undefined; + const info = odesliInfo.entitiesByUniqueId[odesliInfo.entityUniqueId]; - //this.logger.debug('odesli response', info); + const platform: Platform = 'youtube'; if (info.platforms.includes(platform)) { const youtubeId = @@ -175,16 +185,16 @@ export class TimelineReader { ); return null; } - const isMusic = await this.isMusicVideo(youtubeId); + // If it is on tidal or deezer, it's probably music + // Do not check spotify, they carry too much other stuff (podcasts, audiobooks, etc) + let isMusic = odesliInfo.linksByPlatform[tidal] || odesliInfo.linksByPlatform[deezer]; + // If not, check the YT API + isMusic = isMusic || (await this.isMusicVideo(youtubeId)); if (!isMusic) { this.logger.debug('Probably not a music video', youtubeId); return null; } } - const spotify: Platform = 'spotify'; - const tidal: Platform = 'tidal'; - const tidalId = odesliInfo.linksByPlatform[tidal]?.entityUniqueId; - const tidalUri = tidalId ? odesliInfo.entitiesByUniqueId[tidalId].id : undefined; const songInfo = { ...info,