api.violets-purgatory.dev/index.js

169 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-10-20 21:53:49 -05:00
const express = require('express'),
2023-11-16 19:29:22 -06:00
path = require('path'),
fs = require('fs'),
2024-01-27 12:45:39 -06:00
WebSocket = require('ws'),
2024-02-08 12:30:38 -06:00
minify = require('minify-html'),
pageUpdater = require('./pageUpdater.js')
2023-10-20 21:53:49 -05:00
var app = express()
const PORT = process.env.PORT || 8080
2023-11-16 15:16:22 -06:00
const staticpath = path.join(__dirname, 'static')
2024-01-25 23:29:28 -06:00
const resourcePath = path.join(__dirname, 'resources')
2023-11-16 15:16:22 -06:00
const mainpage = resourcePath + '/mainPage.html'
var lanyardData = undefined
2024-01-25 23:29:28 -06:00
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json')))
var thumborInstances = config.thumborInstances
2024-01-25 23:50:09 -06:00
var activityImages = config.activityImages
2024-01-27 12:02:59 -06:00
var highlight = config.highlightedWords
2024-01-27 12:11:36 -06:00
var uptime = Date.now()
var lastLanyardUpdate = Date.now()
2024-02-07 21:59:21 -06:00
var lastPong = Date.now()
2024-01-27 12:11:36 -06:00
var thumbCount = 0
function getThumbor() {
thumbCount += 1
return thumborInstances[thumbCount % thumborInstances.length] + "unsafe"
}
2023-10-20 21:53:49 -05:00
2023-11-16 15:16:22 -06:00
app.listen(PORT, () => {
console.log("Violet's Purgatory is now listening on port: " + PORT)
2023-11-03 13:01:13 -05:00
})
2024-02-04 14:41:15 -06:00
var cachedImages = {}
2024-01-27 14:19:49 -06:00
if (!fs.existsSync(path.join(staticpath, 'cached'))) {
fs.mkdirSync(path.join(staticpath, 'cached'))
}
2024-01-25 23:29:28 -06:00
var randomQuotes = config.quotes
2024-02-04 15:03:54 -06:00
var commitCount = "300+"
2024-02-04 14:41:15 -06:00
function get_img_url(activity, size = "large_image") {
2024-01-27 14:19:49 -06:00
if ("assets" in activity) {
2024-01-29 18:48:42 -06:00
var image = activity.assets[size]
2024-01-27 14:19:49 -06:00
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 {
2024-01-27 15:41:08 -06:00
return null
2024-01-27 14:19:49 -06:00
}
}
}
2024-01-25 23:03:05 -06:00
function timeFormatter(seconds) {
seconds = Math.ceil(seconds)
2024-01-25 23:08:44 -06:00
var minutes = Math.floor(seconds / 60)
2024-01-25 23:03:05 -06:00
2024-01-25 23:11:53 -06:00
if (seconds % 60 < 10) {
2024-01-25 23:11:19 -06:00
return `${minutes}:0${seconds % 60}`
2024-01-25 23:11:53 -06:00
} else {
return `${minutes}:${seconds % 60}`
2024-01-25 23:11:19 -06:00
}
2024-01-27 14:19:49 -06:00
2024-01-25 23:03:05 -06:00
}
2024-01-25 23:07:26 -06:00
function gameTimeFormatter(seconds) {
seconds = Math.ceil(seconds)
var minutes = Math.ceil(seconds / 60)
2024-01-25 23:08:44 -06:00
var hours = Math.floor(minutes / 60)
2024-01-26 11:20:50 -06:00
if (seconds <= 60) {
2024-02-02 09:06:39 -06:00
return 'about ' + seconds + ' seconds'
2024-01-25 23:07:26 -06:00
} else if (minutes < 60) {
return `${minutes} Minutes`
}
2024-01-27 14:19:49 -06:00
2024-01-25 23:11:19 -06:00
return `${hours} hours and ${minutes % 60} minutes`
2024-01-27 14:19:49 -06:00
2024-01-25 23:07:26 -06:00
}
2024-02-08 12:30:38 -06:00
app.use(pageUpdater.middleWare)
2024-01-27 14:19:49 -06:00
2023-11-16 15:16:22 -06:00
// Lanyard Stuffs
2024-02-07 21:59:21 -06:00
function socketeer() {
var lanyard = new WebSocket('https://api.violets-purgatory.dev')
function ping(dur) {
2023-11-16 15:16:22 -06:00
lanyard.send(JSON.stringify({
2024-02-07 21:59:21 -06:00
op: 3
2023-11-16 15:16:22 -06:00
}))
2024-02-07 21:59:21 -06:00
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
console.log("FUCK!")
lanyard.close()
socketeer()
}
}, dur);
}
2024-01-27 14:19:49 -06:00
2024-02-07 21:59:21 -06:00
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
if (data.op == 1) {
ping(30000)
lastPong = Date.now()
} else if (data.op == 3) {
lastPong = Date.now()
} 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];
2024-01-27 14:19:49 -06:00
2024-02-07 21:59:21 -06:00
if (get_img_url(activity)) {
var url = get_img_url(activity)
var fn = Math.ceil(Math.random() * 100_000_000_000).toString()
var fp = path.join(__dirname, 'static/cached', fn)
2024-02-04 14:41:15 -06:00
2024-02-07 21:59:21 -06:00
if (!cachedImages[url]) {
const response = await (await fetch(url)).arrayBuffer()
2024-02-04 14:41:15 -06:00
2024-02-07 21:59:21 -06:00
fs.writeFileSync(fp, Buffer.from(response))
cachedImages[url] = fn
}
2024-01-27 14:32:25 -06:00
}
2024-02-04 14:41:15 -06:00
2024-02-07 21:59:21 -06:00
if (get_img_url(activity, "small_image")) {
var url = get_img_url(activity, "small_image")
var fn = Math.ceil(Math.random() * 100_000_000_000).toString()
var fp = path.join(__dirname, 'static/cached', fn)
2024-02-04 14:41:15 -06:00
2024-02-07 21:59:21 -06:00
if (!cachedImages[url]) {
const response = await (await fetch(url)).arrayBuffer()
2024-02-04 14:41:15 -06:00
2024-02-07 21:59:21 -06:00
fs.writeFileSync(fp, Buffer.from(response))
cachedImages[url] = fn
}
2024-01-29 18:48:42 -06:00
}
}
2024-02-07 21:59:21 -06:00
2024-01-27 14:19:49 -06:00
}
2024-02-07 21:59:21 -06:00
})
}
2024-02-04 14:41:15 -06:00
2024-02-08 17:30:52 -06:00
socketeer()