Skip to content

Commit

Permalink
feat(Yandex Music): rewrite presence (#9015)
Browse files Browse the repository at this point in the history
* feat(Yandex Music): rewrite presence

* chore: change username

* feat(Yandex Music): hide when paused

* refactor: remove un-needed function
  • Loading branch information
Slowlife01 authored Dec 29, 2024
1 parent 47f4323 commit 137eddc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 76 deletions.
13 changes: 11 additions & 2 deletions websites/Y/Yandex Music/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@
"name": "D3rise",
"id": "253913831896645632"
},
"contributors": [
{
"name": "slowlife.",
"id": "374905512661221377"
}
],
"service": "Yandex Music",
"description": {
"en": "Yandex streaming service that allows you to listen to music tracks, albums, and collections of music tracks and get personal recommendations.",
"ru": "Стриминговый сервис компании «Яндекс», позволяющий прослушивать музыкальные композиции, альбомы, подборки музыкальных треков и получать персональные рекомендации.",
"ga_IE": "Seirbhís sruthú Yandex a ligeann duit éisteacht le rianta ceoil, albam, agus bailiúcháin rianta ceoil agus moltaí pearsanta a fháil.",
"nl": "Yandex-streamingdienst waarmee u naar muziektracks, albums en verzamelingen muziektracks kunt luisteren en persoonlijke aanbevelingen kunt krijgen."
},
"url": "music.yandex.ru",
"version": "1.1.4",
"url": [
"music.yandex.ru",
"music.yandex.com"
],
"version": "2.0.0",
"logo": "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/logo.png",
"thumbnail": "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/thumbnail.png",
"color": "#FDCD01",
Expand Down
109 changes: 35 additions & 74 deletions websites/Y/Yandex Music/presence.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,38 @@
const presence = new Presence({
clientId: "745261937092198532",
}),
strings = presence.getStrings({
playing: "general.playing",
pause: "general.paused",
});

let presenceData: PresenceData;

function getMillisecondsFromString(timeString: string): number {
const parsedText = timeString.split(":");
return (Number(parsedText[0]) * 60 + Number(parsedText[1])) * 1000;
}

function isPodcast(): boolean {
return !!document.querySelectorAll(".track__podcast")[0];
}

setInterval(async () => {
const startedAt =
Date.now() -
getMillisecondsFromString(
document.querySelectorAll<HTMLElement>(".progress__left")[0].textContent
),
playing =
document.querySelectorAll(".player-controls__btn_pause").length === 2;

let artists;
if (isPodcast()) {
artists =
document.querySelectorAll<HTMLElement>(".track__podcast")[0].textContent;
} else {
artists =
document.querySelectorAll<HTMLElement>(".track__artists")[0].textContent;
}

const coverImageSizes = document
.querySelector(".track")
.querySelector<HTMLImageElement>(".entity-cover__image")
.srcset // get all images of all sizes
.split(", "),
coverImage = coverImageSizes
.at(-1) // get the last one (the best one)
.split(" ")
.at(0),
largeImageKey = coverImage
? `https:${coverImage}`
: "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/logo.png";

presenceData = {
largeImageKey,
smallImageKey: playing ? Assets.Play : Assets.Pause,
smallImageText: playing ? (await strings).playing : (await strings).pause,
details:
document.querySelectorAll<HTMLElement>(".track__title")[0].textContent,
state: artists,
startTimestamp: startedAt,
endTimestamp:
startedAt +
getMillisecondsFromString(
document.querySelectorAll<HTMLElement>(".progress__right")[0]
.textContent
),
};

if (!playing) {
delete presenceData.startTimestamp;
delete presenceData.endTimestamp;
}
}, 1000);
clientId: "745261937092198532",
});

presence.on("UpdateData", () => {
if (document.querySelectorAll(".track__title").length !== 0)
presence.setActivity(presenceData);
else presence.setActivity();
presence.on("UpdateData", async () => {
if (
document.querySelectorAll(".player-controls__btn_pause").length !== 2 ||
!navigator.mediaSession.metadata
)
return presence.clearActivity();

const largeImageKey = navigator.mediaSession.metadata.artwork
? navigator.mediaSession.metadata.artwork.at(-1).src
: "https://cdn.rcd.gg/PreMiD/websites/Y/Yandex%20Music/assets/logo.png",
timePassed = document.querySelector(".progress__left").textContent,
[currentTime, duration] = [
presence.timestampFromFormat(timePassed),
presence.timestampFromFormat(
document.querySelector(".progress__right").textContent
) + presence.timestampFromFormat(timePassed),
],
[startTimestamp, endTimestamp] = presence.getTimestamps(
currentTime,
duration
),
presenceData = {
type: ActivityType.Listening,
largeImageKey,
details: navigator.mediaSession.metadata.title,
state:
navigator.mediaSession.metadata.artist ||
navigator.mediaSession.metadata.album,
startTimestamp,
endTimestamp,
};

presence.setActivity(presenceData);
});

0 comments on commit 137eddc

Please sign in to comment.