seperate API & pageUpdater files

This commit is contained in:
bingus_violet 2024-06-25 01:02:28 -05:00
parent 6057503e06
commit 74f2911244
3 changed files with 171 additions and 75 deletions

69
api.js Normal file
View file

@ -0,0 +1,69 @@
const path = require("path"),
fs = require("fs"),
WebSocket = require('ws'),
EventEmitter = require("events").EventEmitter
const events = new EventEmitter();
const constants = JSON.parse(fs.readFileSync(path.join(__dirname, "constants.json")))
var lastPong = 0
module.exports = {
"lanyard": constants.fallbackLanyard,
"connected": false,
"lastLanyardUpdate": Date.now(),
"events": events,
"spins": 0,
}
function socketeer() {
var lanyard = new WebSocket('https://api.violets-purgatory.dev')
lanyard.on("error", (error) => {
console.log(error)
})
lanyard.on("close", () => {
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
module.exports.lanyard = constants.fallbackLanyard
setTimeout(() => {
socketeer()
}, 30000);
})
function ping(dur) {
lanyard.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
lanyard.close()
console.log("Max duration since last pong exceeded- Closing socket.")
}
}, dur);
}
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
// console.log(data.op)
if (data.op == 1) {
console.log("Connected to Discord Websocket!")
ping(30000)
lastPong = Date.now()
} else if (data.op == 3) {
lastPong = Date.now()
} else if (data.op == 0) {
module.exports.lanyard = data.d
module.exports.lastLanyardUpdate = Date.now()
events.emit("lanyardUpdate")
} else if (data.op == 4) {
module.exports.spins = data.spins
}
})
}
socketeer()

View file

@ -21,7 +21,6 @@
"World's Worst Developer", "World's Worst Developer",
"The worst git user to exist", "The worst git user to exist",
"These birds are Pissing me off... I'm the original        Starwalker", "These birds are Pissing me off... I'm the original        Starwalker",
"Make sure to check out <a href='https://univerter.dev'>Univerter!</a>", "Make sure to check out <a href='https://univerter.dev'>Univerter!</a>",
"Check out <a href='https://asahixp.pages.gay'>Asahi's website! &lt;3</a>", "Check out <a href='https://asahixp.pages.gay'>Asahi's website! &lt;3</a>",
"Check out <a href='https://sylvie.loveh.art/'>Sylvie's website! &lt;3</a>" "Check out <a href='https://sylvie.loveh.art/'>Sylvie's website! &lt;3</a>"
@ -36,6 +35,84 @@
"https://unusual-back-production.up.railway.app/", "https://unusual-back-production.up.railway.app/",
"https://axiomatic-hair-production.up.railway.app/" "https://axiomatic-hair-production.up.railway.app/"
], ],
"fallbackLanyard": {
"kv": {},
"spotify": null,
"discord_user": {
"id": "534132311781015564",
"username": "bingus_violet",
"avatar": "f3fe0d6a1dd2fe301f437e02333e602d",
"discriminator": "0",
"bot": false,
"clan": null,
"global_name": "Violet",
"avatar_decoration_data": null,
"display_name": "Violet",
"public_flags": 0
},
"activities": [],
"discord_status": "offline",
"active_on_discord_web": false,
"active_on_discord_desktop": false,
"active_on_discord_mobile": false,
"listening_to_spotify": false,
"socials": {
"Social Media": {
"Fedi": {
"name": "bingus_violet@floofy.city",
"url": "https://floofy.city/@bingus_violet"
},
"Youtube": {
"name": "Violet's Fiasco",
"url": "https://www.youtube.com/channel/UChcrBJNJLZucy3TPyGyAY2g"
},
"Liberapay": {
"name": "bingus_violet",
"url": "https://en.liberapay.com/bingus_violet/"
}
},
"Messaging": {
"Matrix": {
"name": "@bingus_violet:matrix.violets-purgatory.dev",
"url": "https://matrix.to/#/@bingus_violet:matrix.violets-purgatory.dev"
},
"Discord": {
"name": "bingus_violet"
},
"Revolt": {
"name": "BingusViolet#5573"
}
},
"Coding": {
"Forgejo": {
"name": "bingus_violet@git.violets-purgatory.dev",
"url": "https://git.violets-purgatory.dev/bingus_violet/"
},
"Codeberg": {
"name": "bingus_violet",
"url": "https://codeberg.org/Bingus_violet"
},
"Docker": {
"name": "bingusviolet",
"url": "https://hub.docker.com/u/bingusviolet"
},
"Github": {
"name": "violets-puragtory",
"url": "https://github.com/violets-puragtory"
}
},
"Games": {
"Steam": {
"name": "Violet",
"url": "https://steamcommunity.com/id/violet-The-Thigh-high-obtainer/"
},
"Roblox": {
"name": "@bingus_violet",
"url": "https://www.roblox.com/users/28347789/profile"
}
}
}
},
"highlightedWords": { "highlightedWords": {
"birds": { "birds": {
"color": "yellow" "color": "yellow"

View file

@ -1,13 +1,13 @@
const path = require('path'), const path = require('path'),
fs = require('fs'), fs = require('fs'),
WebSocket = require('ws'),
minify = require('@node-minify/core'), minify = require('@node-minify/core'),
uglifyJs = require("@node-minify/uglify-js"), uglifyJs = require("@node-minify/uglify-js"),
htmlMinifier = require("minify-html"), htmlMinifier = require("minify-html"),
activityToHTML = require("./overcomplicatedStatuses.js"), activityToHTML = require("./overcomplicatedStatuses.js"),
randomThemer = require("./randomThemer.js"), randomThemer = require("./randomThemer.js"),
himalaya = require("himalaya"), himalaya = require("himalaya"),
glob = require("glob") glob = require("glob"),
api = require("./api.js")
var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json'))) var constants = JSON.parse(fs.readFileSync(path.join(__dirname, 'constants.json')))
@ -19,8 +19,6 @@ var globalSpins = 0
var commitCount = "500+" var commitCount = "500+"
var lanyardData = undefined
var uptime = Date.now() var uptime = Date.now()
var pregenFiles = [] var pregenFiles = []
@ -168,17 +166,15 @@ function converter(html, dynamic = true) {
"COMMIT_COUNT": commitCount, "COMMIT_COUNT": commitCount,
"QUOTE_COUNT": quotes.length, "QUOTE_COUNT": quotes.length,
"DISCORD_STATUS": () => { "DISCORD_STATUS": () => {
if (lanyardData) { return `<span style="color: ${constants.discStatuses[api.lanyard.discord_status].color};" class="statusColor">${constants.discStatuses[api.lanyard.discord_status].text}</span>` +
return `<span style="color: ${constants.discStatuses[lanyardData.discord_status].color};" class="statusColor">${constants.discStatuses[lanyardData.discord_status].text}</span>` + `<style>.pfp { border-color: ${constants.discStatuses[api.lanyard.discord_status].color} }</style>`;
`<style>.pfp { border-color: ${constants.discStatuses[lanyardData.discord_status].color} }</style>`;
}
return ""; return "";
}, },
"TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`, "TOPBAR": `<div id="topbar"><h3><a href="/socials">Socials</a></h3></div>`,
"CUSTOM_STATUS": () => { "CUSTOM_STATUS": () => {
if (lanyardData && lanyardData.activities[0] && lanyardData.activities[0].type == 4) { if (api.lanyard.activities[0] && api.lanyard.activities[0].type == 4) {
var status = lanyardData.activities[0] var status = api.lanyard.activities[0]
var addedHTML = "<hr/><p>" var addedHTML = "<hr/><p>"
if (status.emoji) { if (status.emoji) {
if (status.emoji.id) { if (status.emoji.id) {
@ -205,8 +201,8 @@ function converter(html, dynamic = true) {
"WEATHER_TEXT": "", "WEATHER_TEXT": "",
"ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")), "ANNOUNCEMENT": fs.readFileSync(path.join(__dirname, "config/announcement.html")),
"SOCIALS": () => { "SOCIALS": () => {
if (lanyardData && lanyardData.socials) { if (api.lanyard.socials) {
var socials = lanyardData.socials var socials = api.lanyard.socials
var html = "" var html = ""
var socialsTable = Object.keys(socials) var socialsTable = Object.keys(socials)
for (var i = 0; i < socialsTable.length; i++) { for (var i = 0; i < socialsTable.length; i++) {
@ -229,10 +225,10 @@ function converter(html, dynamic = true) {
} }
var realtimeReplacers = { var realtimeReplacers = {
"ACTIVITIES": activityToHTML.activitiesToHTML(lanyardData), "ACTIVITIES": activityToHTML.activitiesToHTML(api.lanyard),
"SPINCOUNT": globalSpins, "SPINCOUNT": globalSpins,
"UPTIME": timeFormatter((Date.now() - uptime) / 1000), "UPTIME": timeFormatter((Date.now() - uptime) / 1000),
"LAST_LANYARD": timeFormatter((Date.now() - lastLanyardUpdate) / 1000), "LAST_LANYARD": timeFormatter((Date.now() - api.lastLanyardUpdate) / 1000),
"RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)], "RANDOM_TITLE": titles[Math.floor(Math.random() * titles.length)],
"RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)], "RANDOM_QUOTE": quotes[Math.floor(Math.random() * quotes.length)],
} }
@ -283,7 +279,7 @@ function converter(html, dynamic = true) {
module.exports = { module.exports = {
getActivities: function () { getActivities: function () {
return htmlMinifier.minify(converter(activityToHTML.activitiesToHTML(lanyardData))) return htmlMinifier.minify(converter(activityToHTML.activitiesToHTML(api.lanyard)))
}, },
middleWare: async function (req, res, next) { middleWare: async function (req, res, next) {
@ -355,8 +351,6 @@ updateCommits()
// Lanyard Stuffs // Lanyard Stuffs
var lastLanyardUpdate = Date.now()
var lastPong = 0
function pregenerate() { function pregenerate() {
for (var i = 0; i < pregenFiles.length; i++) { for (var i = 0; i < pregenFiles.length; i++) {
@ -368,49 +362,11 @@ function pregenerate() {
pregenerate() pregenerate()
function socketeer() { api.events.on("lanyardUpdate", async () => {
var lanyard = new WebSocket('https://api.violets-purgatory.dev')
lanyard.on("error", (error) => {
console.log(error)
})
lanyard.on("close", () => {
console.log("Connection Closed. Attempting Reconnect in 30 seconds.")
setTimeout(() => {
socketeer()
}, 30000);
})
function ping(dur) {
lanyard.send(JSON.stringify({
op: 3
}))
setTimeout(() => {
ping(dur)
if (Date.now() - lastPong > 120000) {
lanyard.close()
console.log("Max duration since last pong exceeded- Closing socket.")
}
}, dur);
}
lanyard.addEventListener("message", async (res) => {
var data = JSON.parse(res.data)
// console.log(data.op)
if (data.op == 1) {
console.log("Connected to Discord Websocket!")
ping(30000)
lastPong = Date.now()
} else if (data.op == 3) {
lastPong = Date.now()
} else if (data.op == 0) {
lanyardData = data.d
lastLanyardUpdate = Date.now()
pregenerate() pregenerate()
for (var i = 0; i < lanyardData.activities.length; i++) {
var activity = lanyardData.activities[i] for (var i = 0; i < api.lanyard.activities.length; i++) {
var activity = api.lanyard.activities[i]
if (activity.type == 4 && activity.emoji) { if (activity.type == 4 && activity.emoji) {
if (activity.emoji.id) { if (activity.emoji.id) {
@ -423,10 +379,4 @@ function socketeer() {
} }
} }
} }
} else if (data.op == 4) {
globalSpins = data.spins
}
}) })
}
socketeer()