92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import { z } from "zod";
|
|
|
|
export interface Song {
|
|
title: string;
|
|
artist: string;
|
|
apiProvider: string;
|
|
thumbnailUrl: string;
|
|
link: string;
|
|
}
|
|
const songLinkShape = z.object({
|
|
userCountry: z.string(),
|
|
entitiesByUniqueId: z.record(
|
|
z.string(),
|
|
z.object({
|
|
id: z.string(),
|
|
type: z.string(),
|
|
title: z.string(),
|
|
thumbnailUrl: z.string().optional(),
|
|
apiProvider: z.string(),
|
|
artistName: z.string(),
|
|
})
|
|
),
|
|
linksByPlatform: z.record(
|
|
z.string(),
|
|
z.object({
|
|
country: z.string(),
|
|
url: z.string().url(),
|
|
entityUniqueId: z.string(),
|
|
}))
|
|
});
|
|
//i hate this
|
|
export const preferredProviders = [
|
|
"spotify",
|
|
"deezer",
|
|
"youtubeMusic",
|
|
"tidal",
|
|
"itunes"
|
|
];
|
|
|
|
export function getSongOnPreferredProvider(json: unknown, link: string): Song | null {
|
|
const maybesong = songLinkShape.safeParse(json);
|
|
if (!maybesong.success) {
|
|
return null;
|
|
}
|
|
const song = maybesong.data;
|
|
for (const platform of preferredProviders) {
|
|
if (!song.linksByPlatform[platform]) {
|
|
console.log(`couldnt find song on ${platform}`)
|
|
continue
|
|
}
|
|
const entityId = song.linksByPlatform[platform].entityUniqueId;
|
|
const songInfo = song.entitiesByUniqueId[entityId]
|
|
|
|
return {
|
|
title: songInfo.title,
|
|
artist: songInfo.artistName,
|
|
apiProvider: songInfo.apiProvider,
|
|
thumbnailUrl: songInfo.thumbnailUrl!,
|
|
link: song.linksByPlatform[platform].url,
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
|
|
export function kyzaify(input: string): string {
|
|
//im gonna write this as shittily as possible just because.
|
|
if (input === "youtube") {
|
|
return "YouTube";
|
|
} else if (input === "youtubeMusic") {
|
|
return "YouTube Music";
|
|
} else if (input === "itunes") {
|
|
return "iTunes";
|
|
} else if (input === "soundcloud") {
|
|
return "SoundCloud";
|
|
}
|
|
if (input.length === 0) return input;
|
|
|
|
let result = input.charAt(0).toUpperCase();
|
|
|
|
for (let i = 1; i < input.length; i++) {
|
|
const char = input.charAt(i);
|
|
|
|
if (char === char.toUpperCase()) {
|
|
result += ' ' + char;
|
|
} else {
|
|
result += char;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|