Caching
This commit is contained in:
parent
c0ba44c88b
commit
4463448217
4 changed files with 139 additions and 58 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -132,3 +132,4 @@ dist
|
||||||
|
|
||||||
# Violet's Purgatory
|
# Violet's Purgatory
|
||||||
static/index.html
|
static/index.html
|
||||||
|
static/cached
|
136
index.js
136
index.js
|
@ -2,7 +2,8 @@ const express = require('express'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
fs = require('fs'),
|
fs = require('fs'),
|
||||||
WebSocket = require('ws'),
|
WebSocket = require('ws'),
|
||||||
minify = require('minify-html')
|
minify = require('minify-html'),
|
||||||
|
sha256 = require('sha256')
|
||||||
|
|
||||||
var app = express()
|
var app = express()
|
||||||
|
|
||||||
|
@ -37,8 +38,42 @@ app.listen(PORT, () => {
|
||||||
console.log("Violet's Purgatory is now listening on port: " + 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
|
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) {
|
function timeFormatter(seconds) {
|
||||||
seconds = Math.ceil(seconds)
|
seconds = Math.ceil(seconds)
|
||||||
var minutes = Math.floor(seconds / 60)
|
var minutes = Math.floor(seconds / 60)
|
||||||
|
@ -65,7 +100,7 @@ function gameTimeFormatter(seconds) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function pageUpdate() {
|
async function pageUpdate() {
|
||||||
|
|
||||||
var genStart = Date.now()
|
var genStart = Date.now()
|
||||||
|
|
||||||
|
@ -162,35 +197,14 @@ function pageUpdate() {
|
||||||
addedHTML += `<h2><hr>What I'm up to:</h2><div class="container-fluid row" style="margin: 0; padding: 0; display: flex;">`
|
addedHTML += `<h2><hr>What I'm up to:</h2><div class="container-fluid row" style="margin: 0; padding: 0; display: flex;">`
|
||||||
debounce = true
|
debounce = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function get_img() {
|
function get_img() {
|
||||||
|
var fn = sha256(get_img_url(activity))
|
||||||
|
|
||||||
if ("assets" in activity) {
|
return '/cached/' + fn
|
||||||
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 songStats() {
|
function songStats() {
|
||||||
var html = ``
|
var html = ``
|
||||||
|
|
||||||
|
@ -210,7 +224,7 @@ function pageUpdate() {
|
||||||
var currentPercent = (Date.now() - activity.timestamps.start) / (activity.timestamps.end - activity.timestamps.start) * 100
|
var currentPercent = (Date.now() - activity.timestamps.start) / (activity.timestamps.end - activity.timestamps.start) * 100
|
||||||
addedHTML += `
|
addedHTML += `
|
||||||
<div class="chip activity col-md-6 testing">
|
<div class="chip activity col-md-6 testing">
|
||||||
<img src="${getThumbor()}/256x256/${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
<img src="${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
||||||
<p>
|
<p>
|
||||||
Listening to <span style="color: limegreen;">${activity.name}</span>
|
Listening to <span style="color: limegreen;">${activity.name}</span>
|
||||||
<br> Song: ${activity.details || " "}
|
<br> Song: ${activity.details || " "}
|
||||||
|
@ -240,17 +254,18 @@ function pageUpdate() {
|
||||||
</style>
|
</style>
|
||||||
`
|
`
|
||||||
} else if (activity.type == 0) {
|
} else if (activity.type == 0) {
|
||||||
var time = activity.created_at
|
var time = activity.created_at
|
||||||
if (activity.timestamps) {
|
if (activity.timestamps) {
|
||||||
time = activity.timestamps.start
|
time = activity.timestamps.start
|
||||||
}
|
}
|
||||||
if (!activity.assets) {
|
if (!activity.assets) {
|
||||||
activity.assets = {"large_text": " ", "small_text": " "}
|
activity.assets = { "large_text": " ", "small_text": " " }
|
||||||
}
|
}
|
||||||
|
|
||||||
addedHTML += `
|
|
||||||
|
addedHTML += `
|
||||||
<div class="chip activity col-md-6 testing">
|
<div class="chip activity col-md-6 testing">
|
||||||
<img src="${getThumbor()}/${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
<img src="${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
||||||
<p>
|
<p>
|
||||||
Playing <span style="color: rgb(255, 100, 150);">${activity.name}</span>
|
Playing <span style="color: rgb(255, 100, 150);">${activity.name}</span>
|
||||||
<br> ${activity.details || activity.assets.large_text || " "}
|
<br> ${activity.details || activity.assets.large_text || " "}
|
||||||
|
@ -262,16 +277,16 @@ function pageUpdate() {
|
||||||
`
|
`
|
||||||
} else if (activity.type != 4) {
|
} else if (activity.type != 4) {
|
||||||
var time = activity.created_at
|
var time = activity.created_at
|
||||||
if (activity.timestamps) {
|
if (activity.timestamps) {
|
||||||
time = activity.timestamps.start
|
time = activity.timestamps.start
|
||||||
}
|
}
|
||||||
if (!activity.assets) {
|
if (!activity.assets) {
|
||||||
activity.assets = {"large_text": " ", "small_text": " "}
|
activity.assets = { "large_text": " ", "small_text": " " }
|
||||||
}
|
}
|
||||||
|
|
||||||
addedHTML += `
|
addedHTML += `
|
||||||
<div class="chip activity col-md-6 testing">
|
<div class="chip activity col-md-6 testing">
|
||||||
<img src="${getThumbor()}/${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
<img src="${get_img()}" title="${activity.assets.large_text || activity.assets.small_text}">
|
||||||
<p>
|
<p>
|
||||||
<span style="color: rgb(225, 200, 255);">${activity.name}</span>
|
<span style="color: rgb(225, 200, 255);">${activity.name}</span>
|
||||||
<br> ${activity.details || activity.assets.large_text || " "}
|
<br> ${activity.details || activity.assets.large_text || " "}
|
||||||
|
@ -359,6 +374,7 @@ function pageUpdate() {
|
||||||
return html
|
return html
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Lanyard Stuffs
|
// Lanyard Stuffs
|
||||||
|
|
||||||
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
|
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
|
||||||
|
@ -385,11 +401,35 @@ lanyard.addEventListener("message", (res) => {
|
||||||
} else if (data.op == 0) {
|
} else if (data.op == 0) {
|
||||||
lanyardData = data.d
|
lanyardData = data.d
|
||||||
lastLanyardUpdate = Date.now()
|
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) => {
|
app.get('/', async (req, res) => {
|
||||||
res.send(minify.minify(pageUpdate()))
|
var html = await (pageUpdate())
|
||||||
|
res.send(minify.minify(html))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|
39
package-lock.json
generated
39
package-lock.json
generated
|
@ -11,6 +11,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"minify-html": "^0.0.2",
|
"minify-html": "^0.0.2",
|
||||||
|
"sha256": "^0.2.0",
|
||||||
"ws": "^8.14.2"
|
"ws": "^8.14.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -93,6 +94,16 @@
|
||||||
"node": ">= 0.6"
|
"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": {
|
"node_modules/cookie": {
|
||||||
"version": "0.5.0",
|
"version": "0.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
|
"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",
|
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
|
||||||
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
|
"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": {
|
"node_modules/side-channel": {
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
"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",
|
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
|
||||||
"integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA=="
|
"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": {
|
"cookie": {
|
||||||
"version": "0.5.0",
|
"version": "0.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz",
|
"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",
|
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
|
||||||
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw=="
|
"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": {
|
"side-channel": {
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"minify-html": "^0.0.2",
|
"minify-html": "^0.0.2",
|
||||||
|
"sha256": "^0.2.0",
|
||||||
"ws": "^8.14.2"
|
"ws": "^8.14.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue