Skip to content

Commit

Permalink
feat(YTPLR): add presence (#8759)
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterStrick authored Sep 30, 2024
1 parent 97a94a8 commit 6dc5b62
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
14 changes: 14 additions & 0 deletions websites/Y/YTPLR/iframe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const iframe = new iFrame();
iframe.on("UpdateData", async () => {
const videoElement: HTMLVideoElement =
document.querySelector(".video-stream");
if (!videoElement) return;

iframe.send({
title: document.querySelector<HTMLAnchorElement>("div.ytp-title-text > a")
.textContent,
duration: videoElement.duration,
currentTime: videoElement.currentTime,
paused: videoElement.paused,
});
});
33 changes: 33 additions & 0 deletions websites/Y/YTPLR/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://schemas.premid.app/metadata/1.11",
"apiVersion": 1,
"author": {
"name": "uwu_peter",
"id": "762219738570555412"
},
"service": "YTPLR",
"altnames": ["YouTube Playlist Randomizer"],
"description": {
"en": "Shuffles a YouTube Playlist in random order"
},
"url": "youtube-playlist-randomizer.bitbucket.io",
"version": "1.0.0",
"logo": "https://i.imgur.com/hsDOqgw.png",
"thumbnail": "https://i.imgur.com/VhkOWl2.png",
"color": "#ff6d70",
"tags": [
"video",
"media"
],
"settings": [
{
"id": "buttons",
"title": "Show Buttons",
"icon": "fas fa-compress-arrows-alt",
"value": true
}
],
"category": "videos",
"iFrameRegExp": "youtube[-]playlist[-]randomizer[.]bitbucket[.]io",
"iframe": true
}
80 changes: 80 additions & 0 deletions websites/Y/YTPLR/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const presence = new Presence({
clientId: "1284161421957136486",
}),
strings = presence.getStrings({
play: "general.watchingVid",
pause: "general.paused",
}),
browsingTimestamp = Math.floor(Date.now() / 1000);

let videoTitle: string,
videoCurrentTime: number,
videoDuration: number,
videoPaused: boolean,
videoPlaylistTotal: number,
videoPlaylistID: string,
videoPlaylistTitle: string;

interface DataInterface {
title: string;
currentTime: number;
duration: number;
paused: boolean;
}

const enum Assets {
Logo = "https://i.imgur.com/hsDOqgw.png",
}

presence.on("iFrameData", (data: DataInterface) => {
videoTitle = data.title;
videoCurrentTime = data.currentTime;
videoDuration = data.duration;
videoPaused = data.paused;
videoPlaylistTotal = parseInt(document.querySelector("#all").textContent);
videoPlaylistID = document.querySelector<HTMLInputElement>("#pid").value;
videoPlaylistTitle = document.querySelector("#title").textContent;
});

presence.on("UpdateData", async () => {
const presenceData: PresenceData = {
largeImageKey: Assets.Logo,
},
[startTimestamp, endTimestamp] = presence.getTimestamps(
Math.floor(videoCurrentTime),
Math.floor(videoDuration)
),
showButtons = await presence.getSetting<boolean>("buttons");

if (videoTitle) {
presenceData.type = ActivityType.Watching;
presenceData.details = `Shuffling ${videoPlaylistTotal} Videos`;
presenceData.state = videoTitle;
presenceData.smallImageKey = videoPaused ? Assets.Pause : Assets.Play;
presenceData.smallImageText = videoPaused
? (await strings).pause
: (await strings).play;

if (!videoPaused) {
presenceData.startTimestamp = startTimestamp;
presenceData.endTimestamp = endTimestamp;
} else {
delete presenceData.startTimestamp;
delete presenceData.endTimestamp;
}

if (videoPlaylistID && showButtons) {
presenceData.buttons = [
{
label: `Shuffle ${videoPlaylistTitle}`,
url: `https://youtube-playlist-randomizer.bitbucket.io/?pid=${videoPlaylistID}&autostart`,
},
];
} else delete presenceData.buttons;
} else presenceData.startTimestamp = browsingTimestamp;

presenceData.state = `"${presenceData.state}"`;

if (presenceData.details) presence.setActivity(presenceData);
else presence.setActivity();
});

0 comments on commit 6dc5b62

Please sign in to comment.