Skip to content

Commit

Permalink
itunes compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed May 26, 2022
1 parent 57d7242 commit 455c00f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# apple-music-discord-rpc

**[Deno](https://deno.land) + JavaScript for Automation (JXA) Discord Rich Presence Client for the macOS Apple Music app (Catalina and later).**
**[Deno](https://deno.land) + JavaScript for Automation (JXA) Discord Rich Presence Client for the macOS Apple Music app (Catalina and later) and legacy iTunes.**

Works with local tracks and Apple Music streaming service.

Expand Down
24 changes: 18 additions & 6 deletions music-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { iTunes } from "https://raw.githubusercontent.com/NextFire/jxa/64b6
// Cache

class Cache {
static VERSION = 1;
static VERSION = 2;
static CACHE_FILE = "cache.json";
static #data: Map<string, iTunesInfos> = new Map();

Expand Down Expand Up @@ -53,7 +53,10 @@ class Cache {

// Main part

const CLIENT_ID = "773825528921849856";
const MACOS_VER = await getMacOSVersion();
const IS_APPLE_MUSIC = MACOS_VER >= 10.15;
const APP_NAME: iTunesAppName = IS_APPLE_MUSIC ? "Music" : "iTunes";
const CLIENT_ID = IS_APPLE_MUSIC ? "773825528921849856" : "979297966739300416";
start();

async function start() {
Expand Down Expand Up @@ -84,7 +87,16 @@ async function main() {

// Utils functions

const APP_NAME: iTunesAppName = "Music"; // macOS < Catalina ? "iTunes": "Music"
async function getMacOSVersion(): Promise<number> {
const proc = Deno.run({
cmd: ["sw_vers", "-productVersion"],
stdout: "piped",
});
const rawOutput = await proc.output();
const output = new TextDecoder().decode(rawOutput);
const version = parseFloat(output.match(/\d+\.\d+/)![0]);
return version;
}

function isOpen(): Promise<boolean> {
return run((appName: iTunesAppName) => {
Expand Down Expand Up @@ -121,7 +133,7 @@ async function searchAlbum(props: iTunesProps): Promise<iTunesInfos> {
);
const result = await resp.json();

const artwork = result.results[0]?.artworkUrl100 ?? "appicon";
const artwork = result.results[0]?.artworkUrl100 ?? null;
const url = result.results[0]?.collectionViewUrl ?? null;
infos = { artwork, url };
Cache.set(query, infos);
Expand Down Expand Up @@ -168,7 +180,7 @@ async function setActivity(rpc: Client) {
state: limitStr(props.artist, 128),
timestamps: { end },
assets: {
large_image: infos.artwork,
large_image: infos.artwork ?? "appicon",
large_text: limitStr(props.album, 128),
},
};
Expand Down Expand Up @@ -211,6 +223,6 @@ interface iTunesProps {
}

interface iTunesInfos {
artwork: string;
artwork: string | null;
url: string | null;
}

0 comments on commit 455c00f

Please sign in to comment.