From 980fdd58998c2c20eb91b6b9a1935e0026014966 Mon Sep 17 00:00:00 2001 From: Bingus_Violet Date: Sat, 17 Feb 2024 11:38:12 -0600 Subject: [PATCH] Use api --- index.js | 119 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index 6f0cdcf..83ad69b 100644 --- a/index.js +++ b/index.js @@ -381,65 +381,72 @@ async function pageUpdate() { // Lanyard Stuffs -var lanyard = new WebSocket('wss://api.lanyard.rest/socket') +function socketeer() { + var lanyard = new WebSocket('https://api.violets-purgatory.dev') + function ping(dur) { + lanyard.send(JSON.stringify({ + op: 3 + })) + setTimeout(() => { + ping(dur) + if (Date.now() - lastPong > 120000) { + lanyard.close() + socketeer() + } + }, dur); + } -function beat(dur) { - lanyard.send(JSON.stringify({ - op: 3 - })) - setTimeout(() => { - beat(dur) - }, dur); + lanyard.addEventListener("message", async (res) => { + var data = JSON.parse(res.data) + if (data.op == 1) { + ping(30000) + lastPong = Date.now() + console.log("ping") + } else if (data.op == 3) { + lastPong = Date.now() + console.log("pong") + } else if (data.op == 0) { + console.log(data.d) + lanyardData = data.d + lastLanyardUpdate = Date.now() + + for (let index = 0; index < lanyardData.activities.length; index++) { + const activity = lanyardData.activities[index]; + + 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) + + if (!cachedImages[url]) { + const response = await (await fetch(url)).arrayBuffer() + + fs.writeFileSync(fp, Buffer.from(response)) + + cachedImages[url] = fn + } + } + + 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) + + if (!cachedImages[url]) { + const response = await (await fetch(url)).arrayBuffer() + + fs.writeFileSync(fp, Buffer.from(response)) + + cachedImages[url] = fn + } + } + } + + } + }) } -lanyard.addEventListener("message", async (res) => { - var data = JSON.parse(res.data) - if (data.op == 1) { - beat(data.d.heartbeat_interval) - lanyard.send(JSON.stringify({ - op: 2, - d: { - subscribe_to_id: "534132311781015564" - } - })) - } 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]; - - 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) - - if (!cachedImages[url]) { - const response = await (await fetch(url)).arrayBuffer() - - fs.writeFileSync(fp, Buffer.from(response)) - - cachedImages[url] = fn - } - } - - 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) - - if (!cachedImages[url]) { - const response = await (await fetch(url)).arrayBuffer() - - fs.writeFileSync(fp, Buffer.from(response)) - - cachedImages[url] = fn - } - } - } - - } -}) +socketeer() app.get('/', async (req, res) => { var html = await (pageUpdate())