2024-02-08 12:30:38 -06:00
|
|
|
const path = require('path'),
|
2024-02-12 08:46:49 -06:00
|
|
|
fs = require('fs'),
|
|
|
|
WebSocket = require('ws'),
|
2024-05-28 18:33:32 -05:00
|
|
|
minify = require('@node-minify/core'),
|
|
|
|
uglifyJs = require("@node-minify/uglify-js"),
|
|
|
|
htmlMinifier = require("minify-html"),
|
|
|
|
activityToHTML = require("./overcomplicatedStatuses.js"),
|
2024-05-31 01:05:50 -05:00
|
|
|
randomThemer = require("./randomThemer.js"),
|
|
|
|
himalaya = require("himalaya")
|
2024-02-08 12:30:38 -06:00
|
|
|
|
2024-05-20 10:20:19 -05:00
|
|
|
var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json')))
|
2024-02-08 17:52:26 -06:00
|
|
|
|
2024-05-20 10:20:19 -05:00
|
|
|
var highlightedWords = constants.highlightedWords
|
|
|
|
var quotes = constants.quotes
|
|
|
|
var titles = constants.titles
|
2024-02-08 12:30:38 -06:00
|
|
|
|
2024-04-21 13:01:21 -05:00
|
|
|
var globalSpins = 0
|
|
|
|
|
2024-05-14 15:22:30 -05:00
|
|
|
var commitCount = "500+"
|
2024-02-08 17:28:17 -06:00
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
var lanyardData = undefined
|
2024-02-08 16:03:40 -06:00
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
var uptime = Date.now()
|
2024-02-08 16:03:40 -06:00
|
|
|
|
2024-04-16 14:38:32 -05:00
|
|
|
function firstToUpper(str) {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1)
|
|
|
|
}
|
|
|
|
|
2024-04-23 09:08:04 -05:00
|
|
|
function timeFormatter(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-31 01:05:50 -05:00
|
|
|
function converter(html) {
|
2024-05-26 15:32:26 -05:00
|
|
|
var startTime = Date.now()
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-05-31 01:16:50 -05:00
|
|
|
html = html
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-05-20 10:20:19 -05:00
|
|
|
var config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/config.json')))
|
2024-02-18 08:01:58 -06:00
|
|
|
|
|
|
|
var statusText = ""
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
if (lanyardData) {
|
2024-05-20 10:20:19 -05:00
|
|
|
var statusData = constants.discStatuses[lanyardData.discord_status]
|
2024-02-18 08:01:58 -06:00
|
|
|
var username = lanyardData.discord_user.username
|
|
|
|
|
|
|
|
if (lanyardData.activities[0] && lanyardData.activities[0].type == 4) {
|
2024-05-31 01:05:50 -05:00
|
|
|
var statusText = `<hr/><p>${lanyardData.activities[0].state}</p>`
|
2024-02-18 08:01:58 -06:00
|
|
|
}
|
2024-02-12 08:46:49 -06:00
|
|
|
} else {
|
2024-05-20 10:20:19 -05:00
|
|
|
var statusData = constants.discStatuses.offline
|
2024-02-18 08:01:58 -06:00
|
|
|
var username = "bingus_violet"
|
2024-02-12 08:46:49 -06:00
|
|
|
}
|
2024-02-18 08:01:58 -06:00
|
|
|
|
2024-04-16 14:38:32 -05:00
|
|
|
var bnchName = "Beta"
|
|
|
|
var bnchSub = "beta."
|
|
|
|
|
|
|
|
if (process.env.BRANCH == "dev") {
|
|
|
|
bnchName = "Stable"
|
|
|
|
bnchSub = ""
|
|
|
|
}
|
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
var replacers = {
|
2024-05-15 22:31:17 -05:00
|
|
|
"ALL_KEYWORDS": undefined,
|
|
|
|
"ALL_HIGHLIGHTS": Object.keys(highlightedWords).join(", "),
|
2024-04-16 14:38:32 -05:00
|
|
|
"BRANCH_NAME": bnchName,
|
|
|
|
"BRANCH_SUB": bnchSub,
|
2024-02-12 08:46:49 -06:00
|
|
|
"RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)],
|
2024-05-26 15:38:47 -05:00
|
|
|
"COMMIT_COUNT": commitCount,
|
2024-02-12 08:46:49 -06:00
|
|
|
"QUOTE_COUNT": quotes.length,
|
|
|
|
"RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)],
|
2024-05-31 01:05:50 -05:00
|
|
|
"DISCORD_STATUS":
|
|
|
|
`<span style="color: ${statusData.color};" class="statusColor">${statusData.text}</span>` +
|
|
|
|
`<style>.pfp { border-color: ${statusData.color} }</style>`,
|
2024-02-13 11:12:46 -06:00
|
|
|
"UPTIME": uptime,
|
2024-02-18 08:01:58 -06:00
|
|
|
"TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`,
|
|
|
|
"DISCORD_USER": username,
|
2024-04-17 11:12:42 -05:00
|
|
|
"CUSTOM_STATUS": statusText,
|
2024-05-20 10:20:19 -05:00
|
|
|
"SELECTED_VIDEO": () => {
|
|
|
|
if (config.dailyVideoURL) {
|
2024-05-31 01:05:50 -05:00
|
|
|
return `<h2><hr/>Random video!</h2><p>I would call it random <em>daily</em> video but its not at all daily...</p>
|
|
|
|
<br/>
|
|
|
|
<video controls="true" src="${config.dailyVideoURL}"></video>`
|
2024-05-20 10:20:19 -05:00
|
|
|
}
|
|
|
|
return ``
|
|
|
|
},
|
2024-04-23 09:08:04 -05:00
|
|
|
"SPINCOUNT": globalSpins,
|
|
|
|
"UPTIME": timeFormatter((Date.now() - uptime) / 1000),
|
2024-05-28 18:33:32 -05:00
|
|
|
"WEATHER_MODIFIER": randomThemer.returnTheme(),
|
2024-05-20 10:45:24 -05:00
|
|
|
"WEATHER_TEXT": "",
|
2024-05-20 11:34:25 -05:00
|
|
|
"ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")),
|
2024-05-31 01:05:50 -05:00
|
|
|
"CACHED_IMAGES": fs.readdirSync(path.join(__dirname, "cached")).length.toString(),
|
|
|
|
"ACTIVITIES": activityToHTML.activitiesToHTML(lanyardData, cachedImages)
|
2024-02-12 08:46:49 -06:00
|
|
|
}
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-05-15 22:31:17 -05:00
|
|
|
replacers.ALL_KEYWORDS = "{" + Object.keys(replacers).join("}{") + "} "
|
2024-05-31 15:24:40 -05:00
|
|
|
|
|
|
|
while (html.includes("{PATH_")) {
|
|
|
|
var pagePath = html.substring(html.indexOf("{PATH_"))
|
|
|
|
pagePath = pagePath.substring(6, pagePath.indexOf('}'))
|
|
|
|
|
|
|
|
var stringIndex = `{PATH_${pagePath}}`
|
|
|
|
pagePath = pagePath.toLowerCase()
|
|
|
|
|
|
|
|
var pageHTML = fs.readFileSync(path.join(__dirname, 'static', pagePath, 'index.html')).toString()
|
|
|
|
pageHTML = pageHTML.substring(pageHTML.indexOf('<main>') + 6, pageHTML.indexOf('</main>'))
|
|
|
|
html = html.replaceAll(stringIndex, pageHTML)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rpTable = Object.keys(replacers)
|
|
|
|
|
|
|
|
for (let index = 0; index < rpTable.length; index++) {
|
|
|
|
const text = rpTable[index];
|
|
|
|
html = html.replaceAll(`{${text}}`, replacers[text])
|
|
|
|
}
|
2024-05-31 01:16:50 -05:00
|
|
|
|
2024-05-31 01:28:48 -05:00
|
|
|
if (html.includes("<body>")) {
|
|
|
|
var bodyHTML = htmlMinifier.minify(html.substring(html.indexOf("<body>") + 6, html.lastIndexOf("</body>")))
|
|
|
|
var parsedHTML = himalaya.parse(bodyHTML)
|
|
|
|
} else {
|
|
|
|
var parsedHTML = himalaya.parse(html)
|
|
|
|
}
|
2024-05-31 01:05:50 -05:00
|
|
|
|
|
|
|
function highlighter(json) {
|
|
|
|
for (var i = 0; i < json.length; i++) {
|
|
|
|
var element = json[i]
|
|
|
|
if (element.type == "element") {
|
|
|
|
if (element.children.length > 0) {
|
|
|
|
element.children = highlighter(element.children)
|
|
|
|
}
|
|
|
|
} else if (element.type == "text") {
|
|
|
|
var highTable = Object.keys(highlightedWords)
|
|
|
|
|
|
|
|
for (let index = 0; index < highTable.length; index++) {
|
|
|
|
var term = highTable[index];
|
|
|
|
var replacement = `<span style="color: ${highlightedWords[term]}">${term}</span>`
|
|
|
|
|
|
|
|
|
|
|
|
element.content = element.content.replaceAll(`{${term}}`, "TEMPORARY_REPLACE")
|
|
|
|
element.content = element.content.replaceAll(term, replacement)
|
|
|
|
element.content = element.content.replaceAll("TEMPORARY_REPLACE", `${term}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json
|
2024-02-13 08:49:28 -06:00
|
|
|
}
|
|
|
|
|
2024-05-31 01:05:50 -05:00
|
|
|
parsedHTML = highlighter(parsedHTML)
|
2024-02-28 19:02:30 -06:00
|
|
|
|
2024-05-31 01:16:50 -05:00
|
|
|
parsedHTML = himalaya.stringify(parsedHTML)
|
|
|
|
|
2024-05-31 01:28:48 -05:00
|
|
|
if (html.includes("<body>")) {
|
|
|
|
html = html.substring(0, html.indexOf("<body>")) + parsedHTML + html.substring(html.indexOf("</body>") + 7)
|
|
|
|
} else {
|
|
|
|
html = parsedHTML
|
|
|
|
}
|
2024-02-13 08:49:28 -06:00
|
|
|
|
2024-04-23 09:08:04 -05:00
|
|
|
html = html.replaceAll("{LOAD_TIME}", (Date.now() - startTime).toString() + "ms")
|
|
|
|
|
2024-02-08 12:30:38 -06:00
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:30:52 -06:00
|
|
|
module.exports = {
|
2024-03-08 10:49:47 -06:00
|
|
|
getActivities: function () {
|
2024-05-31 01:28:48 -05:00
|
|
|
return htmlMinifier.minify(converter(activityToHTML.activitiesToHTML(lanyardData, cachedImages)))
|
2024-03-08 10:49:47 -06:00
|
|
|
},
|
|
|
|
|
2024-05-28 18:33:32 -05:00
|
|
|
middleWare: async function (req, res, next) {
|
2024-02-08 17:30:52 -06:00
|
|
|
|
2024-02-12 11:12:15 -06:00
|
|
|
var filePath = (req.baseUrl + req.path).trim()
|
|
|
|
|
2024-04-18 21:39:08 -05:00
|
|
|
if (!filePath.includes(".")) {
|
2024-02-12 11:12:15 -06:00
|
|
|
if (filePath.charAt(filePath.length - 1) != '/') {
|
|
|
|
res.redirect(filePath + '/')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filePath = path.join(filePath, '/index.html')
|
2024-02-08 17:30:52 -06:00
|
|
|
}
|
2024-02-12 11:12:15 -06:00
|
|
|
|
2024-02-08 17:30:52 -06:00
|
|
|
filePath = path.join(__dirname, 'static', filePath || 'index.html')
|
|
|
|
if (fs.existsSync(filePath)) {
|
|
|
|
var data = fs.readFileSync(filePath).toString()
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-03-30 01:26:45 -05:00
|
|
|
res.contentType(path.basename(filePath))
|
|
|
|
|
|
|
|
if (filePath.includes(".html")) {
|
2024-03-03 23:57:30 -06:00
|
|
|
data = converter(data, req.query)
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-02-08 17:30:52 -06:00
|
|
|
}
|
2024-02-13 11:12:46 -06:00
|
|
|
|
2024-04-20 12:15:04 -05:00
|
|
|
if (!filePath.includes(".js")) {
|
2024-05-28 18:33:32 -05:00
|
|
|
data = htmlMinifier.minify(data)
|
|
|
|
} else {
|
|
|
|
data = await minify({
|
|
|
|
compressor: uglifyJs,
|
|
|
|
content: data
|
|
|
|
})
|
2024-04-20 12:15:04 -05:00
|
|
|
}
|
2024-04-20 06:50:20 -05:00
|
|
|
|
2024-03-30 01:26:45 -05:00
|
|
|
res.send(data)
|
2024-02-08 17:30:52 -06:00
|
|
|
} else {
|
|
|
|
res.status(404).send(`
|
2024-02-12 15:51:12 -06:00
|
|
|
<link rel="stylesheet" href="/style.css">
|
|
|
|
<h1>404</h1>
|
|
|
|
<p>Uh oh... I think your lost? There's nothing here :P</p>
|
2024-02-08 17:30:52 -06:00
|
|
|
`)
|
|
|
|
}
|
2024-02-08 12:30:38 -06:00
|
|
|
}
|
|
|
|
}
|
2024-02-08 17:28:17 -06:00
|
|
|
|
|
|
|
async function updateCommits() {
|
2024-04-21 19:13:18 -05:00
|
|
|
var siteResponse = await (await fetch(`https://git.violets-purgatory.dev/bingus_violet/violets-purgatory/src/branch/${process.env.BRANCH || "origin"}`)).text()
|
2024-04-21 13:32:01 -05:00
|
|
|
var commits = siteResponse.substring(0, siteResponse.indexOf("Commits"))
|
2024-04-21 13:33:24 -05:00
|
|
|
|
2024-04-21 19:13:18 -05:00
|
|
|
commits = commits.substring(commits.lastIndexOf("<b>") + 3, commits.lastIndexOf("</b>"))
|
2024-05-14 15:31:00 -05:00
|
|
|
// ^ this works for Forgejo (basically everything i use that isnt Github E.G. Codeberg)
|
2024-05-31 01:05:50 -05:00
|
|
|
|
2024-04-21 19:13:18 -05:00
|
|
|
// commits = commits.substring(commits.lastIndexOf(">") + 1)
|
2024-04-21 13:32:01 -05:00
|
|
|
// ^ This works for Github (fuck you Github)
|
|
|
|
|
2024-04-17 11:21:02 -05:00
|
|
|
commitCount = commits.toString()
|
|
|
|
if (process.env.BRANCH == "dev") {
|
2024-05-16 11:55:35 -05:00
|
|
|
commitCount += " | Beta site"
|
2024-04-17 11:21:02 -05:00
|
|
|
}
|
2024-02-08 17:28:17 -06:00
|
|
|
}
|
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
updateCommits()
|
|
|
|
|
|
|
|
// Lanyard Stuffs
|
|
|
|
|
|
|
|
var lastLanyardUpdate = Date.now()
|
2024-03-18 20:57:48 -05:00
|
|
|
var lastPong = 0
|
2024-02-12 08:46:49 -06:00
|
|
|
|
|
|
|
function socketeer() {
|
|
|
|
var lanyard = new WebSocket('https://api.violets-purgatory.dev')
|
2024-03-18 20:57:48 -05:00
|
|
|
|
2024-05-31 01:05:50 -05:00
|
|
|
lanyard.on("error", (error) => {
|
2024-03-18 20:57:48 -05:00
|
|
|
console.log(error)
|
|
|
|
})
|
|
|
|
|
|
|
|
lanyard.on("close", () => {
|
|
|
|
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
|
|
|
|
setTimeout(() => {
|
|
|
|
socketeer()
|
2024-04-20 16:46:12 -05:00
|
|
|
}, 30000);
|
2024-03-18 20:57:48 -05:00
|
|
|
})
|
|
|
|
|
2024-02-12 08:46:49 -06:00
|
|
|
function ping(dur) {
|
|
|
|
lanyard.send(JSON.stringify({
|
|
|
|
op: 3
|
|
|
|
}))
|
|
|
|
setTimeout(() => {
|
|
|
|
ping(dur)
|
|
|
|
if (Date.now() - lastPong > 120000) {
|
|
|
|
lanyard.close()
|
2024-03-18 20:57:48 -05:00
|
|
|
console.log("Max duration since last pong exceeded- Closing socket.")
|
2024-02-12 08:46:49 -06:00
|
|
|
}
|
|
|
|
}, dur);
|
|
|
|
}
|
|
|
|
|
|
|
|
lanyard.addEventListener("message", async (res) => {
|
|
|
|
var data = JSON.parse(res.data)
|
2024-03-18 21:22:32 -05:00
|
|
|
// console.log(data.op)
|
2024-02-12 08:46:49 -06:00
|
|
|
if (data.op == 1) {
|
2024-03-18 20:57:48 -05:00
|
|
|
console.log("Connected to Discord Websocket!")
|
2024-02-12 08:46:49 -06:00
|
|
|
ping(30000)
|
|
|
|
lastPong = Date.now()
|
2024-03-18 21:22:32 -05:00
|
|
|
} else if (data.op == 3) {
|
|
|
|
lastPong = Date.now()
|
2024-02-12 08:46:49 -06:00
|
|
|
} else if (data.op == 0) {
|
|
|
|
lanyardData = data.d
|
|
|
|
lastLanyardUpdate = Date.now()
|
2024-04-21 13:01:21 -05:00
|
|
|
} else if (data.op == 4) {
|
|
|
|
globalSpins = data.spins
|
2024-02-12 08:46:49 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
socketeer()
|