2024-01-20 22:14:18 -06:00
|
|
|
const express = require('express'),
|
2024-02-07 21:49:28 -06:00
|
|
|
path = require("path"),
|
|
|
|
fs = require("fs"),
|
2024-08-19 06:36:15 -05:00
|
|
|
WebSocket = require("ws"),
|
2024-09-29 16:44:22 -05:00
|
|
|
chokidar = require("chokidar"),
|
|
|
|
sharp = require("sharp")
|
2024-01-19 18:39:24 -06:00
|
|
|
|
|
|
|
const PORT = process.env.PORT || 8080
|
|
|
|
|
|
|
|
var app = express()
|
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
var lanyardData = "Nothing Fetched yet!"
|
|
|
|
|
|
|
|
var sockets = []
|
|
|
|
|
2024-03-07 10:54:28 -06:00
|
|
|
var pfpSeason = "default"
|
|
|
|
|
2024-04-20 15:54:05 -05:00
|
|
|
var violetSpins = 0
|
|
|
|
|
2024-06-25 01:22:17 -05:00
|
|
|
var lastActivity
|
|
|
|
|
2024-06-19 12:55:18 -05:00
|
|
|
var configPath = path.join(__dirname, "config")
|
|
|
|
if (!fs.existsSync(configPath)) {
|
|
|
|
fs.mkdirSync(configPath)
|
|
|
|
}
|
|
|
|
|
2024-08-19 01:54:41 -05:00
|
|
|
var dataPath = path.join(__dirname, "data")
|
|
|
|
if (!fs.existsSync(dataPath)) {
|
|
|
|
fs.mkdirSync(dataPath)
|
|
|
|
} else if (fs.existsSync(path.join(dataPath, "spins"))) {
|
|
|
|
violetSpins = Number(fs.readFileSync(path.join(dataPath, "spins")))
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-08-19 06:36:15 -05:00
|
|
|
var configsC = ["socials.json"]
|
|
|
|
var configsB = ["videos.json"]
|
2024-06-19 12:55:18 -05:00
|
|
|
|
2024-08-19 06:36:15 -05:00
|
|
|
for (var i = 0; i < configsC.length; i++) {
|
|
|
|
var file = configsC[i]
|
2024-06-19 12:55:18 -05:00
|
|
|
var filePath = path.join(configPath, file)
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
fs.writeFileSync(filePath, "{}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 06:36:15 -05:00
|
|
|
for (var i = 0; i < configsB.length; i++) {
|
|
|
|
var file = configsB[i]
|
|
|
|
var filePath = path.join(configPath, file)
|
|
|
|
if (!fs.existsSync(filePath)) {
|
|
|
|
fs.writeFileSync(filePath, "[]")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 22:14:18 -06:00
|
|
|
function randomFileInDir(directory) {
|
|
|
|
var dir = fs.readdirSync(directory)
|
|
|
|
return path.join(directory, dir[Math.floor(Math.random() * dir.length)])
|
|
|
|
}
|
|
|
|
|
2024-01-19 18:44:51 -06:00
|
|
|
app.get("/", (req, res) => {
|
2024-02-07 21:49:28 -06:00
|
|
|
var docs = fs.readFileSync(path.join(__dirname, "basicDocs.json"))
|
|
|
|
docs = JSON.parse(docs)
|
|
|
|
res.send(docs)
|
2024-01-19 18:44:51 -06:00
|
|
|
})
|
2024-01-19 18:39:24 -06:00
|
|
|
|
2024-09-29 16:44:22 -05:00
|
|
|
app.get("/v1/pfp", async (req, res) => {
|
|
|
|
res.setHeader("Content-Type", "image/png")
|
|
|
|
|
2024-03-07 10:54:28 -06:00
|
|
|
var time = new Date(Date.now())
|
|
|
|
if ([11, 12, 1].includes(time.getMonth() + 1)) {
|
|
|
|
pfpSeason = "christmas"
|
|
|
|
}
|
|
|
|
|
|
|
|
var pfpConfig = JSON.parse(fs.readFileSync(path.join(__dirname, "PFPs.json")))[pfpSeason]
|
2024-06-14 00:37:17 -05:00
|
|
|
var characterNumb = (time.getDay()) % pfpConfig.length
|
2024-03-07 10:54:28 -06:00
|
|
|
var char = pfpConfig[characterNumb]
|
|
|
|
var pfps = char.pfps
|
|
|
|
|
2024-09-29 16:44:22 -05:00
|
|
|
var pfpPool = pfps
|
|
|
|
var rand = Math.floor(Math.random() * pfpPool.length)
|
|
|
|
var pfp = path.join(__dirname, char.pfpRoot, pfpPool[rand])
|
|
|
|
|
|
|
|
var pfpRaw = fs.readFileSync(pfp)
|
|
|
|
|
|
|
|
var pfpWidth = Number(req.query.width) || 256
|
|
|
|
var pfpHeight = Number(req.query.height) || 256
|
|
|
|
|
|
|
|
const converted = await sharp(pfpRaw)
|
|
|
|
.resize(pfpWidth, pfpHeight)
|
|
|
|
.png({ progressive: true })
|
|
|
|
.toBuffer()
|
|
|
|
|
|
|
|
res.send(converted)
|
2024-01-20 22:14:18 -06:00
|
|
|
})
|
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
app.get("/v1/lanyard", (req, res) => {
|
|
|
|
res.send(lanyardData)
|
|
|
|
})
|
|
|
|
|
2024-04-20 16:03:51 -05:00
|
|
|
app.get("/v1/spins", (req, res) => {
|
2024-04-21 13:12:50 -05:00
|
|
|
res.send(violetSpins.toString())
|
2024-04-20 16:03:51 -05:00
|
|
|
})
|
|
|
|
|
2024-06-19 12:55:18 -05:00
|
|
|
app.get("/v1/socials", (req, res) => {
|
|
|
|
res.sendFile(path.join(configPath, "socials.json"))
|
|
|
|
})
|
|
|
|
|
2024-08-19 06:36:15 -05:00
|
|
|
function getVideo() {
|
|
|
|
var videoQueue = JSON.parse(fs.readFileSync(path.join(dataPath, "videos.json")))
|
|
|
|
|
|
|
|
var newQueue = []
|
|
|
|
for (var i = 0; i < videoQueue.length; i++) {
|
|
|
|
var video = videoQueue[i]
|
|
|
|
if (video.ends > Date.now()) {
|
|
|
|
newQueue.push(video)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
videoQueue = newQueue
|
|
|
|
fs.writeFileSync(path.join(dataPath, "videos.json"), JSON.stringify(newQueue))
|
|
|
|
|
|
|
|
var oldest = videoQueue[0]
|
|
|
|
|
|
|
|
for (var i = 0; i < videoQueue.length; i++) {
|
|
|
|
var video = videoQueue[i]
|
|
|
|
if (video.ends < (oldest.ends)) {
|
|
|
|
oldest = video
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return oldest
|
|
|
|
}
|
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
// app.listen(PORT, () => {
|
|
|
|
// console.log("API is now listening on port 8080!")
|
|
|
|
// })
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
function socketeer() {
|
|
|
|
var lanyard = new WebSocket('wss://api.lanyard.rest/socket')
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
function beat(dur) {
|
2024-02-07 21:49:28 -06:00
|
|
|
lanyard.send(JSON.stringify({
|
2024-02-19 10:57:50 -06:00
|
|
|
op: 3
|
2024-02-07 21:49:28 -06:00
|
|
|
}))
|
2024-02-19 10:57:50 -06:00
|
|
|
setTimeout(() => {
|
|
|
|
beat(dur)
|
|
|
|
}, dur);
|
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
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
|
2024-06-19 13:13:05 -05:00
|
|
|
lanyardData.d.socials = JSON.parse(fs.readFileSync(path.join(configPath, "socials.json")))
|
2024-08-19 06:36:15 -05:00
|
|
|
lanyardData.d.video = getVideo()
|
2024-06-26 23:56:47 -05:00
|
|
|
var newArr = []
|
|
|
|
for (var i = 0; i < lanyardData.d.activities.length; i++) {
|
|
|
|
var activity = lanyardData.d.activities[i]
|
|
|
|
|
2024-06-29 16:06:16 -05:00
|
|
|
if (activity.type == 2) {
|
|
|
|
// if (activity.name == "Spotify") {
|
2024-06-29 16:06:54 -05:00
|
|
|
if (activity.assets) {
|
2024-06-29 16:45:30 -05:00
|
|
|
activity.song = activity.details
|
|
|
|
activity.album = activity.assets.large_text
|
|
|
|
activity.artist = activity.state
|
2024-06-29 16:06:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-26 23:56:47 -05:00
|
|
|
if (activity.type != 4) {
|
|
|
|
newArr.push(activity)
|
|
|
|
} else if (activity.type == 4) {
|
|
|
|
lanyardData.d.custom_status = {
|
|
|
|
"text": activity.state
|
|
|
|
}
|
|
|
|
if (activity.emoji) {
|
|
|
|
lanyardData.d.custom_status.emoji = activity.emoji
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lanyardData.d.activities = newArr
|
|
|
|
|
2024-06-25 13:04:39 -05:00
|
|
|
if (lanyardData.d.discord_status == "offline") {
|
|
|
|
lanyardData.d.activityChanged = false
|
|
|
|
} else {
|
|
|
|
lanyardData.d.activityChanged = lastActivity != JSON.stringify(lanyardData.d.activities)
|
|
|
|
}
|
2024-06-25 01:22:17 -05:00
|
|
|
lastActivity = JSON.stringify(lanyardData.d.activities)
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
for (let index = 0; index < sockets.length; index++) {
|
|
|
|
var socketData = sockets[index]
|
|
|
|
var socket = socketData.socket;
|
|
|
|
socket.send(JSON.stringify(lanyardData))
|
|
|
|
|
|
|
|
if ((Date.now() - socketData.lastPing) > 120000) {
|
|
|
|
sockets.splice(index, 1)
|
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
}
|
|
|
|
}
|
2024-02-19 10:57:50 -06:00
|
|
|
})
|
2024-02-07 21:49:28 -06:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
lanyard.on('close', () => {
|
|
|
|
console.log("Disconnected")
|
|
|
|
setTimeout(() => {
|
|
|
|
socketeer()
|
|
|
|
}, 60000)
|
|
|
|
})
|
|
|
|
}
|
2024-02-18 06:34:43 -06:00
|
|
|
|
2024-02-19 11:01:40 -06:00
|
|
|
socketeer()
|
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
// wsServer = new WebSocket.WebSocketServer({ port: 8090 })
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:58:04 -06:00
|
|
|
wsServer = WebSocket.Server;
|
|
|
|
let server = require('http').createServer()
|
|
|
|
wsServer = new wsServer({
|
|
|
|
server: server,
|
|
|
|
perMessageDeflate: false
|
|
|
|
})
|
|
|
|
server.on('request', app)
|
2024-02-07 21:49:28 -06:00
|
|
|
|
|
|
|
wsServer.on("connection", function connection(socket) {
|
|
|
|
socket.on('message', function message(data) {
|
|
|
|
data = JSON.parse(data)
|
|
|
|
if (data.op == 3) {
|
|
|
|
for (let index = 0; index < sockets.length; index++) {
|
|
|
|
const socketData = sockets[index];
|
|
|
|
if (socketData.socket == socket) {
|
|
|
|
sockets[index].lastPing = Date.now()
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
socket.send(`{"op": 3}`)
|
2024-04-20 16:03:51 -05:00
|
|
|
} else if (data.op == 4) {
|
|
|
|
violetSpins += 1
|
2024-08-19 01:54:41 -05:00
|
|
|
if (violetSpins % 10 == 0) {
|
|
|
|
fs.writeFileSync(path.join(dataPath, "spins"), violetSpins.toString())
|
|
|
|
}
|
2024-04-20 16:10:27 -05:00
|
|
|
for (var i = 0; i < sockets.length; i++) {
|
2024-04-20 16:14:23 -05:00
|
|
|
var soc = sockets[i].socket
|
|
|
|
soc.send(`{"op": 4, "spins": ${violetSpins}}`)
|
2024-04-20 16:10:27 -05:00
|
|
|
|
|
|
|
}
|
2024-04-20 15:54:05 -05:00
|
|
|
} else {
|
|
|
|
console.log(data)
|
2024-02-19 10:57:50 -06:00
|
|
|
}
|
2024-02-07 21:49:28 -06:00
|
|
|
})
|
2024-02-19 10:57:50 -06:00
|
|
|
|
2024-02-07 21:49:28 -06:00
|
|
|
socket.send(JSON.stringify(lanyardData))
|
|
|
|
socket.send(`{ "op": 1 }`)
|
2024-04-21 13:11:57 -05:00
|
|
|
socket.send(`{"op": 4, "spins": ${violetSpins}}`)
|
2024-06-25 01:22:17 -05:00
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
sockets.push({ socket, lastPing: Date.now() })
|
2024-02-07 21:58:04 -06:00
|
|
|
})
|
|
|
|
|
2024-02-19 10:57:50 -06:00
|
|
|
server.listen(PORT, function () {
|
2024-02-07 21:58:04 -06:00
|
|
|
console.log("API & Websocket now on port: " + PORT)
|
2024-06-20 23:14:26 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
process.on('uncaughtException', (err, origin) => {
|
|
|
|
fs.writeSync(
|
|
|
|
process.stderr.fd,
|
|
|
|
`Caught exception: ${err}\n` +
|
|
|
|
`Exception origin: ${origin}`,
|
|
|
|
);
|
2024-08-19 06:36:15 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
chokidar.watch(path.join(configPath, "videos.json")).on("change", () => {
|
|
|
|
var videos = JSON.parse(fs.readFileSync(path.join(configPath, "videos.json")))
|
|
|
|
|
|
|
|
if (!fs.existsSync(path.join(dataPath, "videos.json"))) {
|
|
|
|
fs.writeFileSync(path.join(dataPath, "videos.json"), "[]")
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoQueue = JSON.parse(fs.readFileSync(path.join(dataPath, "videos.json")))
|
|
|
|
|
|
|
|
var latestTime = Date.now()
|
|
|
|
for (var i = 0; i < videoQueue.length; i++) {
|
|
|
|
var vid = videoQueue[i];
|
|
|
|
if (Number(vid.ends) > latestTime) {
|
|
|
|
latestTime = Number(vid.ends)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var newVideos = []
|
|
|
|
|
|
|
|
for (var i = 0; i < videos.length; i++) {
|
|
|
|
latestTime += 604800000
|
|
|
|
var url = videos[i]
|
|
|
|
newVideos.push({
|
|
|
|
"url": url,
|
|
|
|
"ends": latestTime
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
videoQueue = videoQueue.concat(newVideos)
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(dataPath, "videos.json"), JSON.stringify(videoQueue))
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(configPath, "videos.json"), "[]")
|
|
|
|
})
|