From 292f7d71d3bf1ce8115bd6b7b73597ade12d0645 Mon Sep 17 00:00:00 2001 From: Ryan Cao <70191398+ryanccn@users.noreply.github.com> Date: Sat, 14 Sep 2024 22:59:05 +0800 Subject: [PATCH] AppleMusicRichPresence: fix metadata fetching (#2864) --- src/plugins/appleMusic.desktop/index.tsx | 2 +- src/plugins/appleMusic.desktop/native.ts | 80 +++++++++++++----------- 2 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/plugins/appleMusic.desktop/index.tsx b/src/plugins/appleMusic.desktop/index.tsx index 6fa989cd..327fafc3 100644 --- a/src/plugins/appleMusic.desktop/index.tsx +++ b/src/plugins/appleMusic.desktop/index.tsx @@ -120,7 +120,7 @@ const settings = definePluginSettings({ stateString: { type: OptionType.STRING, description: "Activity state format string", - default: "{artist}" + default: "{artist} ยท {album}" }, largeImageType: { type: OptionType.SELECT, diff --git a/src/plugins/appleMusic.desktop/native.ts b/src/plugins/appleMusic.desktop/native.ts index 2eb2a075..7d69a85a 100644 --- a/src/plugins/appleMusic.desktop/native.ts +++ b/src/plugins/appleMusic.desktop/native.ts @@ -11,37 +11,11 @@ import type { TrackData } from "."; 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[]) { const { stdout } = await exec("osascript", cmds.map(c => ["-e", c]).flat()); 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 { appleMusicLink?: string, songLink?: string, @@ -51,6 +25,24 @@ interface RemoteData { let cachedRemoteData: { id: string, data: RemoteData; } | { id: string, failures: number; } | null = null; +const APPLE_MUSIC_BUNDLE_REGEX = /