Skip to content

Commit

Permalink
tidying a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
NextFire committed Dec 18, 2022
1 parent ec5e120 commit a18bb3c
Showing 1 changed file with 67 additions and 64 deletions.
131 changes: 67 additions & 64 deletions music-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function main() {
}
}

// Utils functions
// macOS/JXA functions

async function getMacOSVersion(): Promise<number> {
const proc = Deno.run({
Expand Down Expand Up @@ -122,6 +122,8 @@ function getProps(): Promise<iTunesProps> {
}, APP_NAME);
}

// iTunes Search API

async function searchAlbum(props: iTunesProps): Promise<iTunesInfos> {
const { artist, album } = props;
const cacheIndex = `${artist} ${album}`;
Expand Down Expand Up @@ -168,87 +170,88 @@ async function _searchAlbum(
return { artwork, url };
}

/**
* Format string to specified char limits.
* Will output the string with 3 chars at the end replaced by '...'.
* @param s string
* @param minLength
* @param maxLength
* @returns Formatted string
*/
function formatStr(s: string, minLength = 2, maxLength = 128) {
return s.length <= maxLength
? s.padEnd(minLength)
: `${s.slice(0, maxLength - 3)}...`;
}

// Activity setter

async function setActivity(rpc: Client) {
const open = await isOpen();
console.log("isOpen:", open);

if (open) {
const state = await getState();
console.log("state:", state);
if (!open) {
await rpc.clearActivity();
return;
}

switch (state) {
case "playing": {
const props = await getProps();
console.log("props:", props);
const state = await getState();
console.log("state:", state);

let end;
if (props.duration) {
const delta = (props.duration - props.playerPosition) * 1000;
end = Math.ceil(Date.now() + delta);
}
switch (state) {
case "playing": {
const props = await getProps();
console.log("props:", props);

// EVERYTHING must be less than or equal to 128 chars long
const activity: Activity = {
details: formatStr(props.name),
timestamps: { end },
assets: { large_image: "appicon" },
};

if (props.artist.length > 0) {
activity.state = formatStr(props.artist);
}
let end;
if (props.duration) {
const delta = (props.duration - props.playerPosition) * 1000;
end = Math.ceil(Date.now() + delta);
}

// album.length == 0 for radios
if (props.album.length > 0) {
const infos = await searchAlbum(props);
console.log("infos:", infos);

activity.assets = {
large_image: infos.artwork ?? "appicon",
large_text: formatStr(props.album),
};

if (infos.url) {
activity.buttons = [
{
label: "Play on Apple Music",
url: infos.url,
},
];
}
}
// EVERYTHING must be less than or equal to 128 chars long
const activity: Activity = {
details: formatStr(props.name),
timestamps: { end },
assets: { large_image: "appicon" },
};

await rpc.setActivity(activity);
break;
if (props.artist.length > 0) {
activity.state = formatStr(props.artist);
}

case "paused":
case "stopped": {
await rpc.clearActivity();
break;
// album.length == 0 for radios
if (props.album.length > 0) {
const infos = await searchAlbum(props);
console.log("infos:", infos);

activity.assets = {
large_image: infos.artwork ?? "appicon",
large_text: formatStr(props.album),
};

if (infos.url) {
activity.buttons = [
{
label: "Play on Apple Music",
url: infos.url,
},
];
}
}

await rpc.setActivity(activity);
break;
}

case "paused":
case "stopped": {
await rpc.clearActivity();
break;
}
} else {
await rpc.clearActivity();
}
}

/**
* Format string to specified char limits.
* Will output the string with 3 chars at the end replaced by '...'.
* @param s string
* @param minLength
* @param maxLength
* @returns Formatted string
*/
function formatStr(s: string, minLength = 2, maxLength = 128) {
return s.length <= maxLength
? s.padEnd(minLength)
: `${s.slice(0, maxLength - 3)}...`;
}

// TypeScript

type iTunesAppName = "iTunes" | "Music";
Expand Down

0 comments on commit a18bb3c

Please sign in to comment.