Fix youtube links not being parsed for song info

This commit is contained in:
Max Nuding 2023-04-23 13:07:52 +02:00
parent 971c846dd1
commit 42d91a097f
Signed by: phlaym
GPG Key ID: A06651BAB6777237

View File

@ -84,19 +84,12 @@ export class TimelineReader {
return null;
}
private static async getSongInfo(url: string, remainingTries = 6): Promise<SongInfo | null> {
private static async getSongInfo(url: URL, remainingTries = 6): Promise<SongInfo | null> {
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);
}
}