Modular Code

This commit is contained in:
bingus_violet 2024-06-08 16:52:20 -05:00
parent e64c9feea2
commit 9ae8ebfe98

View file

@ -9,7 +9,19 @@ const cachePath = path.join(__dirname, "cache"),
cacheFile = path.join(__dirname, "cache.json") cacheFile = path.join(__dirname, "cache.json")
if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) } if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) }
if (!fs.existsSync(cacheFile)) { fs.writeFileSync(cacheFile, "{}") } if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json")))
}
var cacheDirs = ["songs", "imgs"]
for (var i = 0; i < cacheDirs.length; i++) {
if (!fs.existsSync(path.join(cachePath, cacheDirs[i]))) {
fs.mkdirSync(path.join(cachePath, cacheDirs[i]))
}
}
const songCache = path.join(cachePath, "songs"),
imgCache = path.join(cachePath, "imgs")
var app = express() var app = express()
@ -18,16 +30,16 @@ const imgWaitMax = 2
app.get("/cached/*", (req, res) => { app.get("/cached/*", (req, res) => {
var imgURL = req.originalUrl var imgURL = req.originalUrl
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1) imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
var imgData = cachedImages[imgURL] var imgData = cachedImages.imgs[imgURL]
var imgWait = 0 var imgWait = 0
function waitForImage() { function waitForImage() {
if (imgWait < imgWaitMax) { if (imgWait < imgWaitMax) {
imgWait += 0.1 imgWait += 0.1
setTimeout(() => { setTimeout(() => {
imgData = cachedImages[imgURL] imgData = cachedImages.imgs[imgURL]
if (imgData) { if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res) fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
} else { } else {
waitForImage() waitForImage()
} }
@ -37,7 +49,7 @@ app.get("/cached/*", (req, res) => {
} }
} }
if (imgData) { if (imgData) {
fs.createReadStream(path.join(cachePath, imgData.file)).pipe(res) fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
} else { } else {
waitForImage() waitForImage()
} }
@ -122,25 +134,25 @@ function socketeer() {
const activity = lanyardData.activities[index]; const activity = lanyardData.activities[index];
var imgType = undefined var imgType = undefined
var imgRes = "256x256/" var imgRes = "512x512/"
for (var i = 0; i < 2; i++) { for (var i = 0; i < 2; i++) {
if (get_img_url(activity, imgType)) { if (get_img_url(activity, imgType)) {
var url = get_img_url(activity, imgType) var url = get_img_url(activity, imgType)
var fn = Object.keys(cachedImages).length + "." + imgExtension var fn = Object.keys(cachedImages.imgs).length + "." + imgExtension
var fp = path.join(cachePath, fn) var fp = path.join(imgCache, fn)
if (!cachedImages[url]) { if (!cachedImages.imgs[url]) {
const response = await (await fetch(thumborURL + imgRes + thumborArgs + url)).arrayBuffer() const response = await (await fetch(thumborURL + imgRes + thumborArgs + url)).arrayBuffer()
fs.writeFileSync(fp, Buffer.from(response)) fs.writeFileSync(fp, Buffer.from(response))
cachedImages[url] = { cachedImages.imgs[url] = {
"file": fn, "file": fn,
"lastUpdated": Date.now() "lastUpdated": Date.now()
} }
fs.writeFileSync(cacheFile, JSON.stringify(cachedImages)) fs.writeFileSync(cacheFile, JSON.stringify(cachedImages))
} }
imgType = "small_image" imgType = "small_image"
imgRes = "64x64/" imgRes = "128x128/"
} }
} }
} }