From 44634482171f5d693f6881a925eb0f79dfa2c728 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Sat, 27 Jan 2024 14:19:49 -0600 Subject: [PATCH] Caching --- .gitignore | 3 +- index.js | 154 +++++++++++++++++++++++++++++----------------- package-lock.json | 39 ++++++++++++ package.json | 1 + 4 files changed, 139 insertions(+), 58 deletions(-) diff --git a/.gitignore b/.gitignore index e6fd96a..8aedd51 100644 --- a/.gitignore +++ b/.gitignore @@ -131,4 +131,5 @@ dist .pnp.* # Violet's Purgatory -static/index.html \ No newline at end of file +static/index.html +static/cached \ No newline at end of file diff --git a/index.js b/index.js index 28dbeae..4349d17 100644 --- a/index.js +++ b/index.js @@ -2,7 +2,8 @@ const express = require('express'), path = require('path'), fs = require('fs'), WebSocket = require('ws'), - minify = require('minify-html') + minify = require('minify-html'), + sha256 = require('sha256') var app = express() @@ -37,8 +38,42 @@ app.listen(PORT, () => { console.log("Violet's Purgatory is now listening on port: " + PORT) }) +if (!fs.existsSync(path.join(staticpath, 'cached'))) { + fs.mkdirSync(path.join(staticpath, 'cached')) +} + var randomQuotes = config.quotes +function get_img_url(activity) { + + if ("assets" in activity) { + var image = undefined + if ("large_image" in activity.assets) { + image = activity.assets.large_image + } else if ("small_image" in activity.assets) { + image = activity.assets.small_image + } + 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)) + } else { + return decodeURIComponent(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${image}.png`) + } + } + } + + if (!image) { + if (activity.name in activityImages) { + return decodeURIComponent(activityImages[activity.name]) + } else { + return decodeURIComponent(`https://cdn.discordapp.com/app-assets/680748054038577165/680775885317472448.png`) + // This was supposed to be temporary but it kinda stuck honestly lol (It's an ultrakill icon) + } + } +} + function timeFormatter(seconds) { seconds = Math.ceil(seconds) var minutes = Math.floor(seconds / 60) @@ -48,7 +83,7 @@ function timeFormatter(seconds) { } else { return `${minutes}:${seconds % 60}` } - + } function gameTimeFormatter(seconds) { @@ -60,15 +95,15 @@ function gameTimeFormatter(seconds) { } else if (minutes < 60) { return `${minutes} Minutes` } - + return `${hours} hours and ${minutes % 60} minutes` - + } -function pageUpdate() { +async function pageUpdate() { var genStart = Date.now() - + var statuses = { "online": { "text": "Online", @@ -118,7 +153,7 @@ function pageUpdate() { addedHTML += `"` // addedHTML += (status.state || "") var splitStatus = status.state.split(' ') - + for (let index = 0; index < splitStatus.length; index++) { const text = splitStatus[index]; if (highlight[text]) { @@ -157,40 +192,19 @@ function pageUpdate() { if (found) { continue } - + if (!debounce && activity.type != 4) { addedHTML += `


What I'm up to:

` debounce = true } + + function get_img() { + var fn = sha256(get_img_url(activity)) - if ("assets" in activity) { - var image = undefined - if ("large_image" in activity.assets) { - image = activity.assets.large_image - } else if ("small_image" in activity.assets) { - image = activity.assets.small_image - } - 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)) - } else { - return decodeURIComponent(`https://cdn.discordapp.com/app-assets/${activity.application_id}/${image}.png`) - } - } - } - - if (!image) { - if (activity.name in activityImages) { - return decodeURIComponent(activityImages[activity.name]) - } else { - return decodeURIComponent(`https://cdn.discordapp.com/app-assets/680748054038577165/680775885317472448.png`) - // This was supposed to be temporary but it kinda stuck honestly lol (It's an ultrakill icon) - } - } + return '/cached/' + fn } + function songStats() { var html = `` @@ -210,7 +224,7 @@ function pageUpdate() { var currentPercent = (Date.now() - activity.timestamps.start) / (activity.timestamps.end - activity.timestamps.start) * 100 addedHTML += `
- +

Listening to ${activity.name}
Song: ${activity.details || " "} @@ -240,17 +254,18 @@ function pageUpdate() { ` } else if (activity.type == 0) { - var time = activity.created_at - if (activity.timestamps) { - time = activity.timestamps.start - } - if (!activity.assets) { - activity.assets = {"large_text": " ", "small_text": " "} - } + var time = activity.created_at + if (activity.timestamps) { + time = activity.timestamps.start + } + if (!activity.assets) { + activity.assets = { "large_text": " ", "small_text": " " } + } - addedHTML += ` + + addedHTML += `

- +

Playing ${activity.name}
${activity.details || activity.assets.large_text || " "} @@ -262,16 +277,16 @@ function pageUpdate() { ` } else if (activity.type != 4) { var time = activity.created_at - if (activity.timestamps) { - time = activity.timestamps.start - } - if (!activity.assets) { - activity.assets = {"large_text": " ", "small_text": " "} - } + if (activity.timestamps) { + time = activity.timestamps.start + } + if (!activity.assets) { + activity.assets = { "large_text": " ", "small_text": " " } + } - addedHTML += ` + addedHTML += `

- +

${activity.name}
${activity.details || activity.assets.large_text || " "} @@ -320,7 +335,7 @@ function pageUpdate() { html = html.replace("{THUMBOR}", getThumbor()) var quote = randomQuotes[Math.floor(Math.random() * randomQuotes.length)] - + var splitQuote = quote.split(' ') var finalQuote = '' @@ -359,6 +374,7 @@ function pageUpdate() { return html } + // Lanyard Stuffs var lanyard = new WebSocket('wss://api.lanyard.rest/socket') @@ -385,17 +401,41 @@ lanyard.addEventListener("message", (res) => { } else if (data.op == 0) { lanyardData = data.d lastLanyardUpdate = Date.now() + + for (let index = 0; index < lanyardData.activities.length; index++) { + const activity = lanyardData.activities[index]; + + var fn = sha256(get_img_url(activity)) + var fp = path.join(__dirname, 'static/cached', fn) + + if (!fs.existsSync(fp)) { + var wrst = fs.createWriteStream(fp) + + fetch(`${getThumbor()}/256x256/${get_img_url(activity)}`) + .then((response) => response.body) + .then((body) => { + const stream = new WritableStream({ + write(chunk) { + wrst.write(chunk) + } + }) + + body.pipeTo(stream) + }) + } + } } }) -app.get('/', (req, res) => { - res.send(minify.minify(pageUpdate())) +app.get('/', async (req, res) => { + var html = await (pageUpdate()) + res.send(minify.minify(html)) }) -app.use((req, res, next) => { +app.use((req, res, next) => { res.status(404).send(`

404

Uh oh... I think your lost? There's nothing here :P

- `) + `) }) \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 34f798a..d2d9b4d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "express": "^4.18.2", "minify-html": "^0.0.2", + "sha256": "^0.2.0", "ws": "^8.14.2" } }, @@ -93,6 +94,16 @@ "node": ">= 0.6" } }, + "node_modules/convert-hex": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/convert-hex/-/convert-hex-0.1.0.tgz", + "integrity": "sha512-w20BOb1PiR/sEJdS6wNrUjF5CSfscZFUp7R9NSlXH8h2wynzXVEPFPJECAnkNylZ+cvf3p7TyRUHggDmrwXT9A==" + }, + "node_modules/convert-string": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/convert-string/-/convert-string-0.1.0.tgz", + "integrity": "sha512-1KX9ESmtl8xpT2LN2tFnKSbV4NiarbVi8DVb39ZriijvtTklyrT+4dT1wsGMHKD3CJUjXgvJzstm9qL9ICojGA==" + }, "node_modules/cookie": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", @@ -557,6 +568,15 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" }, + "node_modules/sha256": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/sha256/-/sha256-0.2.0.tgz", + "integrity": "sha512-kTWMJUaez5iiT9CcMv8jSq6kMhw3ST0uRdcIWl3D77s6AsLXNXRp3heeqqfu5+Dyfu4hwpQnMzhqHh8iNQxw0w==", + "dependencies": { + "convert-hex": "~0.1.0", + "convert-string": "~0.1.0" + } + }, "node_modules/side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", @@ -733,6 +753,16 @@ "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz", "integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==" }, + "convert-hex": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/convert-hex/-/convert-hex-0.1.0.tgz", + "integrity": "sha512-w20BOb1PiR/sEJdS6wNrUjF5CSfscZFUp7R9NSlXH8h2wynzXVEPFPJECAnkNylZ+cvf3p7TyRUHggDmrwXT9A==" + }, + "convert-string": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/convert-string/-/convert-string-0.1.0.tgz", + "integrity": "sha512-1KX9ESmtl8xpT2LN2tFnKSbV4NiarbVi8DVb39ZriijvtTklyrT+4dT1wsGMHKD3CJUjXgvJzstm9qL9ICojGA==" + }, "cookie": { "version": "0.5.0", "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", @@ -1078,6 +1108,15 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz", "integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==" }, + "sha256": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/sha256/-/sha256-0.2.0.tgz", + "integrity": "sha512-kTWMJUaez5iiT9CcMv8jSq6kMhw3ST0uRdcIWl3D77s6AsLXNXRp3heeqqfu5+Dyfu4hwpQnMzhqHh8iNQxw0w==", + "requires": { + "convert-hex": "~0.1.0", + "convert-string": "~0.1.0" + } + }, "side-channel": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz", diff --git a/package.json b/package.json index 402ddae..5efa10e 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "express": "^4.18.2", "minify-html": "^0.0.2", + "sha256": "^0.2.0", "ws": "^8.14.2" } }