fuck song.link
This commit is contained in:
parent
a5efac6346
commit
4904b1d305
2 changed files with 15 additions and 3 deletions
|
|
@ -31,6 +31,7 @@ export default class PingCommand extends Command {
|
||||||
await interaction.deferReply()
|
await interaction.deferReply()
|
||||||
const user = interaction.options.getString("user") ?? config.listenbrainzAccount;
|
const user = interaction.options.getString("user") ?? config.listenbrainzAccount;
|
||||||
const usesonglink = interaction.options.getBoolean("usesonglink") ?? true
|
const usesonglink = interaction.options.getBoolean("usesonglink") ?? true
|
||||||
|
const useitunes = interaction.options.getBoolean("useitunes") ?? true
|
||||||
|
|
||||||
const meow = await fetch(`https://api.listenbrainz.org/1/user/${user}/playing-now`).then((res) => res.json());
|
const meow = await fetch(`https://api.listenbrainz.org/1/user/${user}/playing-now`).then((res) => res.json());
|
||||||
if (!meow) {
|
if (!meow) {
|
||||||
|
|
@ -44,7 +45,10 @@ export default class PingCommand extends Command {
|
||||||
const paramsObj = {entity: "song", term: track_metadata.artist_name + " " + track_metadata.track_name};
|
const paramsObj = {entity: "song", term: track_metadata.artist_name + " " + track_metadata.track_name};
|
||||||
const searchParams = new URLSearchParams(paramsObj);
|
const searchParams = new URLSearchParams(paramsObj);
|
||||||
const itunesinfo = (await (await fetch(`https://itunes.apple.com/search?${searchParams.toString()}`)).json()).results[0];
|
const itunesinfo = (await (await fetch(`https://itunes.apple.com/search?${searchParams.toString()}`)).json()).results[0];
|
||||||
const link = itunesinfo.trackViewUrl
|
let link = track_metadata.additional_info.origin_url
|
||||||
|
if (useitunes) {
|
||||||
|
link = itunesinfo.trackViewUrl
|
||||||
|
}
|
||||||
const songlink = await fetch(`https://api.song.link/v1-alpha.1/links?url=${link}`).then(a => a.json())
|
const songlink = await fetch(`https://api.song.link/v1-alpha.1/links?url=${link}`).then(a => a.json())
|
||||||
const preferredApi = getSongOnPreferredProvider(songlink, link)
|
const preferredApi = getSongOnPreferredProvider(songlink, link)
|
||||||
|
|
||||||
|
|
@ -108,6 +112,9 @@ export default class PingCommand extends Command {
|
||||||
.addBooleanOption(option => {
|
.addBooleanOption(option => {
|
||||||
return option.setName("usesonglink").setDescription("use songlink or not").setRequired(false)
|
return option.setName("usesonglink").setDescription("use songlink or not").setRequired(false)
|
||||||
})
|
})
|
||||||
|
.addBooleanOption(option => {
|
||||||
|
return option.setName("useitunes").setDescription("use itunes or not").setRequired(false)
|
||||||
|
})
|
||||||
.addStringOption(option => {
|
.addStringOption(option => {
|
||||||
return option.setName("user").setDescription("listenbrainz username").setRequired(false)
|
return option.setName("user").setDescription("listenbrainz username").setRequired(false)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ const songLinkShape = z.object({
|
||||||
id: z.string(),
|
id: z.string(),
|
||||||
type: z.string(),
|
type: z.string(),
|
||||||
title: z.string(),
|
title: z.string(),
|
||||||
thumbnailUrl: z.string(),
|
thumbnailUrl: z.string().optional(),
|
||||||
apiProvider: z.string(),
|
apiProvider: z.string(),
|
||||||
artistName: z.string(),
|
artistName: z.string(),
|
||||||
})
|
})
|
||||||
|
|
@ -38,6 +38,10 @@ export const preferredProviders = [
|
||||||
];
|
];
|
||||||
|
|
||||||
export function getSongOnPreferredProvider(json: any, link: string): Song | null {
|
export function getSongOnPreferredProvider(json: any, link: string): Song | null {
|
||||||
|
if (json.statusCode === 500) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
console.log(json)
|
||||||
const song = songLinkShape.parse(json);
|
const song = songLinkShape.parse(json);
|
||||||
for (const platform of preferredProviders) {
|
for (const platform of preferredProviders) {
|
||||||
if (!song.linksByPlatform[platform]) {
|
if (!song.linksByPlatform[platform]) {
|
||||||
|
|
@ -46,11 +50,12 @@ export function getSongOnPreferredProvider(json: any, link: string): Song | null
|
||||||
}
|
}
|
||||||
const entityId = song.linksByPlatform[platform].entityUniqueId;
|
const entityId = song.linksByPlatform[platform].entityUniqueId;
|
||||||
const songInfo = song.entitiesByUniqueId[entityId]
|
const songInfo = song.entitiesByUniqueId[entityId]
|
||||||
|
|
||||||
return {
|
return {
|
||||||
title: songInfo.title,
|
title: songInfo.title,
|
||||||
artist: songInfo.artistName,
|
artist: songInfo.artistName,
|
||||||
apiProvider: songInfo.apiProvider,
|
apiProvider: songInfo.apiProvider,
|
||||||
thumbnailUrl: songInfo.thumbnailUrl,
|
thumbnailUrl: songInfo.thumbnailUrl!,
|
||||||
link: song.linksByPlatform[platform].url,
|
link: song.linksByPlatform[platform].url,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue