Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Apple Podcasts): add Presence #8726

Merged
merged 12 commits into from
Sep 19, 2024
31 changes: 31 additions & 0 deletions websites/A/Apple Podcasts/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://schemas.premid.app/metadata/1.10",
"author": {
"name": "ray",
"id": "516333697163853828"
},
"service": "Apple Podcasts",
"description": {
"en": "Apple Podcasts is an audio streaming service and media player application developed by Apple for playing podcasts."
},
"url": "podcasts.apple.com",
"version": "1.0.0",
"logo": "https://i.imgur.com/ibW2CvC.png",
"thumbnail": "https://i.imgur.com/uwYJseY.jpeg",
"color": "#6b2ad0",
"category": "music",
"tags": [
"apple",
"podcast",
"audio",
"listen"
],
"settings": [
{
"id": "privacy",
"title": "Privacy Mode",
"icon": "fad fa-user-secret",
"value": false
}
]
}
101 changes: 101 additions & 0 deletions websites/A/Apple Podcasts/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
const presence = new Presence({
clientId: "1281538997235224596",
injectOnComplete: true,
}),
browsingTimestamp = Math.floor(Date.now() / 1000);

const enum Assets { // Other default assets can be found at index.d.ts
Logo = "https://i.imgur.com/ibW2CvC.png",
}

presence.on("UpdateData", async () => {
const presenceData: PresenceData = {
largeImageKey: Assets.Logo,
startTimestamp: browsingTimestamp,
},
strings = await presence.getStrings({
play: "general.playing",
pause: "general.paused",
}),
[privacy] = await Promise.all([presence.getSetting("privacy")]),
1RB marked this conversation as resolved.
Show resolved Hide resolved
shadowRootDir = document.querySelector(".lcd").shadowRoot;
// document.querySelectorAll(".player > .artwork-container > .artwork-component > picture > img")[0].currentSrc
if (shadowRootDir?.querySelector(".lcd__active").ariaHidden !== "true") {
1RB marked this conversation as resolved.
Show resolved Hide resolved
presenceData.type = ActivityType.Listening;

const episodeImage = (
shadowRootDir.querySelectorAll(
".lcd__artwork > picture > img"
)[0] as HTMLImageElement
1RB marked this conversation as resolved.
Show resolved Hide resolved
).src.replace("88x88", "450x450"),
playingShadow = document.querySelector(".chrome-player").shadowRoot;

presenceData.name = shadowRootDir.querySelectorAll(
".lcd-meta-line__fragment"
)[0].textContent;
1RB marked this conversation as resolved.
Show resolved Hide resolved
presenceData.largeImageKey = episodeImage;
const progress = shadowRootDir.querySelectorAll("#playback-progress")[0],
1RB marked this conversation as resolved.
Show resolved Hide resolved
elapsedSeconds = Number(progress.ariaValueNow);

if (
playingShadow.querySelector(".playback-play__pause").ariaHidden !== "true"
) {
const ts = Date.now() / 1000;
presenceData.startTimestamp = ts - elapsedSeconds;
presenceData.endTimestamp =
ts + Number(progress.ariaValueMax) - elapsedSeconds;

presenceData.smallImageKey = Assets.Play;
presenceData.smallImageText = strings.play;
} else if (
playingShadow.querySelector(".playback-play__play").ariaHidden !== "true"
) {
presenceData.startTimestamp = browsingTimestamp;
presenceData.smallImageKey = Assets.Pause;
presenceData.smallImageText = strings.pause;
}
presenceData.details = shadowRootDir.querySelectorAll(
".lcd-meta-line__fragment"
)[2].textContent;
presenceData.state = "on Apple Podcasts";
} else if (document.location.pathname.includes("/podcast")) {
presenceData.details = `Might listen to ${
document.querySelectorAll(".headings > .headings__title")[0].textContent
1RB marked this conversation as resolved.
Show resolved Hide resolved
}`;
presenceData.state = `by ${
document.querySelectorAll(".headings > .headings__subtitles")[0]
1RB marked this conversation as resolved.
Show resolved Hide resolved
.textContent
}`;
presenceData.largeImageKey = (
document.querySelector("picture > img") as HTMLImageElement
1RB marked this conversation as resolved.
Show resolved Hide resolved
).currentSrc;
} else if (document.location.pathname.includes("/home"))
presenceData.details = "On the home page";
else if (document.location.pathname.includes("/charts"))
presenceData.details = "Viewing top charts";
else if (document.location.pathname.includes("/library"))
presenceData.details = "Viewing library";
else if (document.location.pathname.includes("/settings"))
presenceData.details = "Viewing settings";
else if (document.location.pathname.includes("/search")) {
presenceData.details = "Searching for podcasts";
if (document.location.search.includes("?term=")) {
presenceData.details = `Searched for ${decodeURIComponent(
document.location.search.split("?term=")[1]
)}`;
} else {
presenceData.smallImageKey = Assets.Search;
presenceData.details = "Searching...";
}
} else presenceData.details = "Browsing...";

if (presenceData.state && privacy) delete presenceData.state;

if (presenceData.details && privacy) delete presenceData.details;

if (presenceData.details) presence.setActivity(presenceData);
else {
presenceData.type = ActivityType.Listening;
presence.setActivity();
}
});
Loading