From 42d91a097f5e1e781e626f8599fc3f74547db11e Mon Sep 17 00:00:00 2001 From: Max Nuding Date: Sun, 23 Apr 2023 13:07:52 +0200 Subject: [PATCH] Fix youtube links not being parsed for song info --- src/lib/server/timeline.ts | 43 ++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/lib/server/timeline.ts b/src/lib/server/timeline.ts index daa3a0d..9b49f93 100644 --- a/src/lib/server/timeline.ts +++ b/src/lib/server/timeline.ts @@ -84,19 +84,12 @@ export class TimelineReader { return null; } - private static async getSongInfo(url: string, remainingTries = 6): Promise { + private static async getSongInfo(url: URL, remainingTries = 6): Promise { if (remainingTries === 0) { log.error('No tries remaining. Lookup failed!'); return null; } - let hostname: string; - try { - hostname = new URL(url).hostname; - } catch (e) { - log.error(`Could not construct URL ${url}`, e); - return null; - } - if (hostname === 'songwhip.com') { + if (url.hostname === 'songwhip.com') { // song.link doesn't support songwhip links and songwhip themselves will provide metadata if you pass in a // Apple Music/Spotify/etc link, but won't when provided with their own link, so no way to extract song info // except maybe scraping their HTML @@ -104,7 +97,7 @@ export class TimelineReader { } const odesliParams = new URLSearchParams(); - odesliParams.append('url', url); + odesliParams.append('url', url.toString()); odesliParams.append('userCountry', 'DE'); odesliParams.append('songIfSingle', 'true'); if (ODESLI_API_KEY && ODESLI_API_KEY !== 'CHANGE_ME') { @@ -123,7 +116,7 @@ export class TimelineReader { ...info, pageUrl: odesliInfo.pageUrl, youtubeUrl: odesliInfo.linksByPlatform[platform]?.url, - postedUrl: url + postedUrl: url.toString() } as SongInfo; }); }); @@ -189,21 +182,39 @@ export class TimelineReader { // Looks like we're stuck with regex for now instead of using preview cards. // Might as well use it to find URLs. Could also use this for YouTube: If Odesli finds something, it's a song, // if not, ignore it. No need to consult the YT API and give those links a special handling - const musicUrls: string[] = []; + const musicUrls: URL[] = []; const musicUrl = await TimelineReader.getUrlFromPreviewCard(post); if (musicUrl) { - musicUrls.push(musicUrl); + try { + musicUrls.push(new URL(musicUrl)); + } catch (e) { + log.error( + 'URL received from preview card does not seem to be a valid URL', + musicUrl, + e + ); + } } else { const urlMatches = post.content.matchAll(URL_REGEX); for (const match of urlMatches) { if (match === undefined || match.groups === undefined) { + console.warn( + 'Match listed in allMatches, but either it or its groups are undefined', + match + ); continue; } const urlMatch = match.groups.postUrl.toString(); - const musicUrl = urls.find((u) => urlMatch.includes(u)); - if (musicUrl) { - musicUrls.push(urlMatch); + let url: URL; + try { + url = new URL(urlMatch); + } catch (e) { + log.error('URL found via Regex does not seem to be a valud url', urlMatch, e); + continue; } + + // Check *all* found url and let odesli determine if it is music or not + musicUrls.push(url); } }