2024-02-18 08:21:08 -06:00
|
|
|
const path = require("path"),
|
|
|
|
fs = require("fs")
|
|
|
|
|
2024-05-20 10:20:19 -05:00
|
|
|
var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json')))
|
2024-02-18 08:21:08 -06:00
|
|
|
|
2024-05-20 10:20:19 -05:00
|
|
|
var activityImages = constants.activityImages
|
2024-02-18 08:01:58 -06:00
|
|
|
|
|
|
|
function get_img_url(activity, size = "large_image") {
|
|
|
|
if ("assets" in activity) {
|
|
|
|
var image = activity.assets[size]
|
|
|
|
|
|
|
|
if (image) {
|
|
|
|
if (image.includes("https/")) {
|
|
|
|
return decodeURIComponent('https://' + image.substr(image.indexOf('https/') + 6, image.length))
|
|
|
|
} else if (image.includes("spotify")) {
|
|
|
|
return decodeURIComponent('https://i.scdn.co/image/' + image.substr(image.indexOf('spotify:') + 8, image.length))
|
2024-03-24 16:54:09 -05:00
|
|
|
} else {
|
|
|
|
return decodeURIComponent('https://cdn.discordapp.com/app-assets/' + activity.application_id + "/" + image + ".png")
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
2024-03-24 16:55:00 -05:00
|
|
|
}
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
|
|
|
|
2024-03-23 12:37:22 -05:00
|
|
|
if (!image && size == "large_image") {
|
2024-02-18 08:01:58 -06:00
|
|
|
if (activity.name in activityImages) {
|
|
|
|
return decodeURIComponent(activityImages[activity.name])
|
|
|
|
}
|
|
|
|
}
|
2024-03-23 12:37:22 -05:00
|
|
|
return null
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function timeFormatter(seconds) {
|
|
|
|
seconds = Math.ceil(seconds)
|
|
|
|
var minutes = Math.floor(seconds / 60)
|
|
|
|
|
|
|
|
if (seconds % 60 < 10) {
|
|
|
|
return `${minutes}:0${seconds % 60}`
|
|
|
|
} else {
|
|
|
|
return `${minutes}:${seconds % 60}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gameTimeFormatter(seconds) {
|
|
|
|
seconds = Math.ceil(seconds)
|
|
|
|
var minutes = Math.ceil(seconds / 60)
|
|
|
|
var hours = Math.floor(minutes / 60)
|
|
|
|
if (seconds <= 60) {
|
|
|
|
return 'about ' + seconds + ' seconds'
|
|
|
|
} else if (minutes < 60) {
|
|
|
|
return `${minutes} Minutes`
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${hours} hours and ${minutes % 60} minutes`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-30 21:42:30 -05:00
|
|
|
function onlyIfExists(string, check) {
|
|
|
|
if (check) {
|
|
|
|
return string
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 08:01:58 -06:00
|
|
|
module.exports = {
|
|
|
|
activitiesToHTML: (lanyardData, cachedImages) => {
|
|
|
|
var debounce = false
|
|
|
|
var addedHTML = ""
|
|
|
|
|
|
|
|
if (lanyardData && lanyardData.activities.length > 0) {
|
|
|
|
for (let index = 0; index < lanyardData.activities.length; index++) {
|
|
|
|
const activity = lanyardData.activities[index];
|
|
|
|
|
|
|
|
var found = false
|
|
|
|
for (let index = 0; index < lanyardData.activities.length; index++) {
|
|
|
|
const act = lanyardData.activities[index]
|
|
|
|
if (act.name == activity.name) {
|
|
|
|
if (Object.keys(act).length > Object.keys(activity).length) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!debounce && activity.type != 4) {
|
2024-03-08 15:11:25 -06:00
|
|
|
addedHTML += `<h2><hr>What I'm up to:</h2><div class="activity-container">`
|
2024-02-18 08:01:58 -06:00
|
|
|
debounce = true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get_img(activity, size = "large_image") {
|
|
|
|
if (cachedImages[get_img_url(activity, size)]) {
|
|
|
|
var fn = cachedImages[get_img_url(activity, size)]
|
2024-04-18 21:39:08 -05:00
|
|
|
var fp = path.join(__dirname, 'cached', fn)
|
2024-02-18 08:01:58 -06:00
|
|
|
} else {
|
2024-04-17 11:12:42 -05:00
|
|
|
return '/imgs/notFound.png'
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
|
|
|
|
2024-03-08 10:49:47 -06:00
|
|
|
return '/cached/' + fn
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function songStats() {
|
|
|
|
var html = ``
|
|
|
|
|
2024-05-30 21:50:08 -05:00
|
|
|
if (activity.assets && activity.assets.large_text != activity.details && activity.state.length + activity.assets.large_text.length < 100) {
|
2024-02-18 08:01:58 -06:00
|
|
|
html += `
|
|
|
|
<br> Album: ${activity.assets.large_text || " "}
|
|
|
|
<br> Artist: ${activity.state || " "}
|
|
|
|
`
|
|
|
|
} else {
|
2024-05-30 21:50:08 -05:00
|
|
|
html += "<br>"
|
|
|
|
if (activity.state.includes(";"))
|
|
|
|
{ html += "Artists: " }
|
|
|
|
else { html += "Artist: " }
|
|
|
|
html += `${activity.state || " "}`
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
if (activity.type == 2) {
|
|
|
|
var timeLeft = Math.round((activity.timestamps.end - Date.now()) / 1000)
|
|
|
|
var currentPercent = (Date.now() - activity.timestamps.start) / (activity.timestamps.end - activity.timestamps.start) * 100
|
|
|
|
addedHTML += `
|
|
|
|
<div class="chip activity grid-child">
|
|
|
|
<img src="${get_img(activity)}" title="${activity.assets.large_text || activity.assets.small_text || activity.state || ""}">
|
|
|
|
<p>
|
2024-05-30 22:32:30 -05:00
|
|
|
Listening to ${activity.name}
|
2024-02-18 08:01:58 -06:00
|
|
|
<br> Song: ${activity.details || " "}
|
|
|
|
${songStats()}
|
|
|
|
<br>
|
|
|
|
<span class="lengthBar lengthBar${index}"><span></span></span>
|
|
|
|
${timeFormatter((activity.timestamps.end - activity.timestamps.start) / 1000)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<style>
|
|
|
|
|
|
|
|
.lengthBar${index} > span {
|
|
|
|
animation-name: songSlider${index};
|
|
|
|
animation-duration: ${timeLeft}s;
|
|
|
|
animation-timing-function: linear;
|
|
|
|
}
|
|
|
|
|
|
|
|
@keyframes songSlider${index} {
|
|
|
|
0% {
|
|
|
|
width: ${currentPercent}%;
|
|
|
|
}
|
|
|
|
100% {
|
|
|
|
width: 100%;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`
|
2024-05-30 21:42:30 -05:00
|
|
|
} else if (activity.type == 0 && activity.assets) {
|
2024-02-18 08:01:58 -06:00
|
|
|
var time = activity.created_at
|
|
|
|
if (activity.timestamps) {
|
|
|
|
time = activity.timestamps.start
|
|
|
|
}
|
|
|
|
if (!activity.assets) {
|
|
|
|
activity.assets = { "large_text": " ", "small_text": " " }
|
|
|
|
}
|
|
|
|
|
|
|
|
function smch() {
|
|
|
|
if (get_img_url(activity, "small_image")) {
|
|
|
|
return `<img class="smallimg" src="${get_img(activity, "small_image")}" title="${activity.assets.small_text}">`
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
addedHTML += `
|
|
|
|
<div class="chip activity grid-child">
|
|
|
|
<img src="${get_img(activity)}" title="${activity.assets.large_text}">
|
|
|
|
${smch()}
|
|
|
|
<p>
|
2024-05-31 01:22:43 -05:00
|
|
|
Playing ${activity.name}
|
2024-05-30 21:42:30 -05:00
|
|
|
${onlyIfExists("<br>" + (activity.details || activity.assets.large_text), activity.details || activity.assets.large_text)}
|
|
|
|
${onlyIfExists("<br>" + (activity.state || activity.assets.small_text), activity.state || activity.assets.small_text)}
|
2024-02-18 08:01:58 -06:00
|
|
|
<br> ${gameTimeFormatter((Date.now() - time) / 1000)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
`
|
2024-05-30 21:42:30 -05:00
|
|
|
} else if (activity.type != 4 && activity.assets) {
|
2024-02-18 08:01:58 -06:00
|
|
|
var time = activity.created_at
|
|
|
|
if (activity.timestamps) {
|
|
|
|
time = activity.timestamps.start
|
|
|
|
}
|
|
|
|
if (!activity.assets) {
|
|
|
|
activity.assets = { "large_text": " ", "small_text": " " }
|
|
|
|
}
|
|
|
|
|
|
|
|
addedHTML += `
|
|
|
|
<div class="chip activity grid-child">
|
|
|
|
<img src="${get_img(activity)}" title="${activity.assets.large_text || activity.assets.small_text}">
|
|
|
|
<p>
|
|
|
|
<span style="color: rgb(225, 200, 255);">${activity.name}</span>
|
|
|
|
<br> ${(activity.details || activity.assets.large_text || " ")}
|
|
|
|
<br> ${(activity.state || activity.assets.small_text || " ")}
|
|
|
|
<br> ${gameTimeFormatter((Date.now() - time) / 1000)}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return addedHTML + "</div>"
|
|
|
|
}
|
|
|
|
}
|