AppleMusicRichPresence: fix metadata fetching (#2864)
This commit is contained in:
parent
b822542352
commit
292f7d71d3
|
@ -120,7 +120,7 @@ const settings = definePluginSettings({
|
||||||
stateString: {
|
stateString: {
|
||||||
type: OptionType.STRING,
|
type: OptionType.STRING,
|
||||||
description: "Activity state format string",
|
description: "Activity state format string",
|
||||||
default: "{artist}"
|
default: "{artist} · {album}"
|
||||||
},
|
},
|
||||||
largeImageType: {
|
largeImageType: {
|
||||||
type: OptionType.SELECT,
|
type: OptionType.SELECT,
|
||||||
|
|
|
@ -11,37 +11,11 @@ import type { TrackData } from ".";
|
||||||
|
|
||||||
const exec = promisify(execFile);
|
const exec = promisify(execFile);
|
||||||
|
|
||||||
// function exec(file: string, args: string[] = []) {
|
|
||||||
// return new Promise<{ code: number | null, stdout: string | null, stderr: string | null; }>((resolve, reject) => {
|
|
||||||
// const process = spawn(file, args, { stdio: [null, "pipe", "pipe"] });
|
|
||||||
|
|
||||||
// let stdout: string | null = null;
|
|
||||||
// process.stdout.on("data", (chunk: string) => { stdout ??= ""; stdout += chunk; });
|
|
||||||
// let stderr: string | null = null;
|
|
||||||
// process.stderr.on("data", (chunk: string) => { stdout ??= ""; stderr += chunk; });
|
|
||||||
|
|
||||||
// process.on("exit", code => { resolve({ code, stdout, stderr }); });
|
|
||||||
// process.on("error", err => reject(err));
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
async function applescript(cmds: string[]) {
|
async function applescript(cmds: string[]) {
|
||||||
const { stdout } = await exec("osascript", cmds.map(c => ["-e", c]).flat());
|
const { stdout } = await exec("osascript", cmds.map(c => ["-e", c]).flat());
|
||||||
return stdout;
|
return stdout;
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeSearchUrl(type: string, query: string) {
|
|
||||||
const url = new URL("https://tools.applemediaservices.com/api/apple-media/music/US/search.json");
|
|
||||||
url.searchParams.set("types", type);
|
|
||||||
url.searchParams.set("limit", "1");
|
|
||||||
url.searchParams.set("term", query);
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
const requestOptions: RequestInit = {
|
|
||||||
headers: { "user-agent": "Mozilla/5.0 (Windows NT 10.0; rv:125.0) Gecko/20100101 Firefox/125.0" },
|
|
||||||
};
|
|
||||||
|
|
||||||
interface RemoteData {
|
interface RemoteData {
|
||||||
appleMusicLink?: string,
|
appleMusicLink?: string,
|
||||||
songLink?: string,
|
songLink?: string,
|
||||||
|
@ -51,6 +25,24 @@ interface RemoteData {
|
||||||
|
|
||||||
let cachedRemoteData: { id: string, data: RemoteData; } | { id: string, failures: number; } | null = null;
|
let cachedRemoteData: { id: string, data: RemoteData; } | { id: string, failures: number; } | null = null;
|
||||||
|
|
||||||
|
const APPLE_MUSIC_BUNDLE_REGEX = /<script type="module" crossorigin src="([a-zA-Z0-9.\-/]+)"><\/script>/;
|
||||||
|
const APPLE_MUSIC_TOKEN_REGEX = /\w+="([A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*)",\w+="x-apple-jingle-correlation-key"/;
|
||||||
|
|
||||||
|
let cachedToken: string | undefined = undefined;
|
||||||
|
|
||||||
|
const getToken = async () => {
|
||||||
|
if (cachedToken) return cachedToken;
|
||||||
|
|
||||||
|
const html = await fetch("https://music.apple.com/").then(r => r.text());
|
||||||
|
const bundleUrl = new URL(html.match(APPLE_MUSIC_BUNDLE_REGEX)![1], "https://music.apple.com/");
|
||||||
|
|
||||||
|
const bundle = await fetch(bundleUrl).then(r => r.text());
|
||||||
|
const token = bundle.match(APPLE_MUSIC_TOKEN_REGEX)![1];
|
||||||
|
|
||||||
|
cachedToken = token;
|
||||||
|
return token;
|
||||||
|
};
|
||||||
|
|
||||||
async function fetchRemoteData({ id, name, artist, album }: { id: string, name: string, artist: string, album: string; }) {
|
async function fetchRemoteData({ id, name, artist, album }: { id: string, name: string, artist: string, album: string; }) {
|
||||||
if (id === cachedRemoteData?.id) {
|
if (id === cachedRemoteData?.id) {
|
||||||
if ("data" in cachedRemoteData) return cachedRemoteData.data;
|
if ("data" in cachedRemoteData) return cachedRemoteData.data;
|
||||||
|
@ -58,21 +50,39 @@ async function fetchRemoteData({ id, name, artist, album }: { id: string, name:
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [songData, artistData] = await Promise.all([
|
const dataUrl = new URL("https://amp-api-edge.music.apple.com/v1/catalog/us/search");
|
||||||
fetch(makeSearchUrl("songs", artist + " " + album + " " + name), requestOptions).then(r => r.json()),
|
dataUrl.searchParams.set("platform", "web");
|
||||||
fetch(makeSearchUrl("artists", artist.split(/ *[,&] */)[0]), requestOptions).then(r => r.json())
|
dataUrl.searchParams.set("l", "en-US");
|
||||||
]);
|
dataUrl.searchParams.set("limit", "1");
|
||||||
|
dataUrl.searchParams.set("with", "serverBubbles");
|
||||||
|
dataUrl.searchParams.set("types", "songs");
|
||||||
|
dataUrl.searchParams.set("term", `${name} ${artist} ${album}`);
|
||||||
|
dataUrl.searchParams.set("include[songs]", "artists");
|
||||||
|
|
||||||
const appleMusicLink = songData?.songs?.data[0]?.attributes.url;
|
const token = await getToken();
|
||||||
const songLink = songData?.songs?.data[0]?.id ? `https://song.link/i/${songData?.songs?.data[0]?.id}` : undefined;
|
|
||||||
|
|
||||||
const albumArtwork = songData?.songs?.data[0]?.attributes.artwork.url.replace("{w}", "512").replace("{h}", "512");
|
const songData = await fetch(dataUrl, {
|
||||||
const artistArtwork = artistData?.artists?.data[0]?.attributes.artwork.url.replace("{w}", "512").replace("{h}", "512");
|
headers: {
|
||||||
|
"accept": "*/*",
|
||||||
|
"accept-language": "en-US,en;q=0.9",
|
||||||
|
"authorization": `Bearer ${token}`,
|
||||||
|
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
|
||||||
|
"origin": "https://music.apple.com",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(r => r.json())
|
||||||
|
.then(data => data.results.song.data[0]);
|
||||||
|
|
||||||
cachedRemoteData = {
|
cachedRemoteData = {
|
||||||
id,
|
id,
|
||||||
data: { appleMusicLink, songLink, albumArtwork, artistArtwork }
|
data: {
|
||||||
|
appleMusicLink: songData.attributes.url,
|
||||||
|
songLink: `https://song.link/i/${songData.id}`,
|
||||||
|
albumArtwork: songData.attributes.artwork.url.replace("{w}x{h}", "512x512"),
|
||||||
|
artistArtwork: songData.relationships.artists.data[0].attributes.artwork.url.replace("{w}x{h}", "512x512"),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return cachedRemoteData.data;
|
return cachedRemoteData.data;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("[AppleMusicRichPresence] Failed to fetch remote data:", e);
|
console.error("[AppleMusicRichPresence] Failed to fetch remote data:", e);
|
||||||
|
|
Loading…
Reference in a new issue