Skip to content

Commit

Permalink
feat(Emby): add more settings, fix issues & takeover activity (#9209)
Browse files Browse the repository at this point in the history
* feat(Emby): add audio details & state variable + settings

* fix(Emby): fix audio not getting recognised. 

* chore(Emby): takeover presence
  • Loading branch information
darkvillager2 authored Jan 29, 2025
1 parent f49f6b6 commit eff5971
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
24 changes: 21 additions & 3 deletions websites/E/Emby/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"$schema": "https://schemas.premid.app/metadata/1.13",
"apiVersion": 1,
"author": {
"name": "alexbcberio",
"id": "202915432175239169"
"name": "Dark_Ville",
"id": "638080361179512853"
},
"contributors": [
{
"name": "Bas950",
"id": "241278257335500811"
},
{
"name": "alexbcberio",
"id": "202915432175239169"
}
],
"service": "Emby",
Expand All @@ -18,7 +22,7 @@
},
"url": "emby.media",
"regExp": ".*[/]web[/]index[.]html",
"version": "1.5.3",
"version": "1.6.0",
"logo": "https://cdn.rcd.gg/PreMiD/websites/E/Emby/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/E/Emby/assets/thumbnail.png",
"color": "#52B54B",
Expand Down Expand Up @@ -47,6 +51,20 @@
"title": "Show thumbnails",
"icon": "fad fa-image",
"value": true
},
{
"id": "audDetail",
"title": "Audio Details row1",
"icon": "fas fa-comment-alt-edit",
"value": "Listening to: %title%",
"placeholder": "Use %title%, %artist%, %albumartist%, %releasedate%, {0}"
},
{
"id": "audState",
"title": "Audio Details row2",
"icon": "fas fa-comment-alt-edit",
"value": "By: %artist%",
"placeholder": "Use %title%, %artist%, %albumartist%, %releasedate%, {0}"
}
]
}
41 changes: 33 additions & 8 deletions websites/E/Emby/presence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,42 @@ function mediaPrimaryImage(mediaId: string): string {
return `${embyBasenameURL()}/emby/Items/${mediaId}/Images/Primary?height=256`;
}

function audioVariablesReplaced(str: string, info: MediaInfo) {
if (!str || !info) {
presence.error(
"Can't find audio variables str or info. Contact the presence developer."
);
return "";
}
return str
?.replace("%title%", info?.Name ?? "unknown title")
?.replace(
"%artist%",
info?.Artists.length === 0 ? "unknown artists" : info?.Artists?.join(", ")
)
?.replace("%releasedate%", info?.DateCreated ?? "unknown date")
?.replace(
"%albumartist%",
info?.AlbumArtists?.map(x => x.Name).join(", ") ?? "unknown album artists"
);
}
/**
* handleAudioPlayback - handles the presence when the audio player is active
*/
async function handleAudioPlayback(): Promise<void> {
// sometimes the buttons are not created fast enough
try {
const [audioDetails, audioState] = await Promise.all([
presence.getSetting<string>("audDetail"),
presence.getSetting<string>("audState"),
]);
presenceData.type = ActivityType.Listening;
const audioElement = document.querySelector<HTMLAudioElement>("audio"),
regexResult = /\/Audio\/(\w+)\/universal/.exec(audioElement.src);
regexResult =
/\/Audio\/(\w+)\/universal/.exec(audioElement?.src) ||
/(\d+)/gm.exec(
document.querySelector(".nowPlayingBar").getAttribute("data-id")
);

if (!regexResult) {
presence.error("Could not obtain audio itemId");
Expand All @@ -444,8 +471,9 @@ async function handleAudioPlayback(): Promise<void> {

const [, mediaId] = regexResult,
info = await obtainMediaInfo(mediaId);
presenceData.details = `Listening to ${info.Name ?? "Unknown title"}`;
presenceData.state = `By ${info.AlbumArtist ?? "Unknown artist"}`;
presenceData.details = audioVariablesReplaced(audioDetails, info);
presenceData.state = audioVariablesReplaced(audioState, info);

if (
(await presence.getSetting("showThumbnails")) &&
// some songs might not have albumart
Expand Down Expand Up @@ -582,11 +610,9 @@ async function handleVideoPlayback(): Promise<void> {
document.querySelector<HTMLDivElement>(".itemBackdrop")?.style
?.backgroundImage
) ||
/\/[0-9]+\//.exec(
document.querySelector<HTMLVideoElement>(".htmlVideoPlayer")?.src
);
/\/[0-9]+\//.exec(document.querySelector<HTMLVideoElement>("video")?.src);

if (!regexResult) {
if (!regexResult || !videoPlayerElem) {
presence.error("Could not obtain video itemId");
return;
}
Expand Down Expand Up @@ -880,7 +906,6 @@ async function updateData(): Promise<void> {
if (showPresence) {
if (!presenceData.largeImageKey)
presenceData.largeImageKey = PRESENCE_ART_ASSETS.logo;

if (!presenceData.details) presence.setActivity();
else presence.setActivity(presenceData);
}
Expand Down

0 comments on commit eff5971

Please sign in to comment.