nowplaying: half-assed lastfm support #4

Merge aplicado
amy mesclou 2 commits from lastfm into main 2025-08-01 10:22:30 +00:00
Mostrando apenas as alterações do commit 4bb4afe182 - Mostrar todos os commits

avoid fetching link if its not needed

sunnie 2025-08-01 10:38:39 +03:00

Ver arquivo

@ -15,7 +15,7 @@ import { getSongOnPreferredProvider, lobotomizedSongButton, musicCache, nowPlayi
import { type Config } from "../config.ts"; import { type Config } from "../config.ts";
import { hash } from "crypto" import { hash } from "crypto"
async function getNowPlaying(username: string, lastFMApiKey?: string): Promise<{ async function getNowPlaying(username: string, lastFMApiKey?: string, lastFMFetchLink?: boolean): Promise<{
songName: string, artistName: string, link: string songName: string, artistName: string, link: string
} | false | undefined> { } | false | undefined> {
if (!lastFMApiKey) { if (!lastFMApiKey) {
@ -40,8 +40,12 @@ async function getNowPlaying(username: string, lastFMApiKey?: string): Promise<{
// yes its a string, horror // yes its a string, horror
if (track["@attr"]?.nowplaying !== "true") return false if (track["@attr"]?.nowplaying !== "true") return false
// it also doesnt provide a streaming platform url, im sorry i have to do this // it also doesnt provide a streaming platform url, im sorry i have to do this
let link = ""
if (lastFMFetchLink) {
const page = await (await fetch(track.url)).text() const page = await (await fetch(track.url)).text()
const [, link] = page.match(/class="header-new-playlink"\s+href="(.+?)"/m) || [] const match = page.match(/class="header-new-playlink"\s+href="(.+?)"/m)
if (match) link = match[1]
}
return { return {
songName: track.name, songName: track.name,
artistName: track.artist["#text"], artistName: track.artist["#text"],
@ -60,7 +64,7 @@ export default class PingCommand extends Command {
let useSonglink = interaction.options.getBoolean("usesonglink") ?? config.commandDefaults.nowplaying.useSonglink let useSonglink = interaction.options.getBoolean("usesonglink") ?? config.commandDefaults.nowplaying.useSonglink
const useiTunes = interaction.options.getBoolean("useitunes") ?? config.commandDefaults.nowplaying.useItunes const useiTunes = interaction.options.getBoolean("useitunes") ?? config.commandDefaults.nowplaying.useItunes
const nowPlaying = await getNowPlaying(user, useLastFM ? config.lastFMApiKey : undefined) const nowPlaying = await getNowPlaying(user, useLastFM ? config.lastFMApiKey : undefined, !useiTunes)
if (typeof nowPlaying === "undefined") { if (typeof nowPlaying === "undefined") {
await interaction.followUp("something shat itself!"); await interaction.followUp("something shat itself!");
return; return;