Restore images if broken

This commit is contained in:
bingus_violet 2024-06-10 19:24:46 -05:00
parent 1b6663c716
commit 5af82414ea

View file

@ -1,16 +1,16 @@
const express = require("express"), const express = require("express"),
fs = require("fs"), fs = require("fs"),
path = require("path"), path = require("path"),
WebSocket = require('ws'), WebSocket = require('ws'),
{ Station } = require("@fridgefm/radio-core") { Station } = require("@fridgefm/radio-core")
const station = new Station({}) const station = new Station({})
const PORT = process.env.PORT || 8080 const PORT = process.env.PORT || 8080
const cachePath = path.join(__dirname, "cache"), const cachePath = path.join(__dirname, "cache"),
cacheFile = path.join(__dirname, "cache.json"), cacheFile = path.join(__dirname, "cache.json"),
configFile = path.join(__dirname, "config.json") configFile = path.join(__dirname, "config.json")
var lanyardData var lanyardData
@ -18,7 +18,7 @@ if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) }
if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) { if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json"))) fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json")))
} }
if (!fs.existsSync(configFile)) { fs.writeFileSync(configFile, fs.readFileSync(path.join(__dirname, "defaults/config.json")))} if (!fs.existsSync(configFile)) { fs.writeFileSync(configFile, fs.readFileSync(path.join(__dirname, "defaults/config.json"))) }
var cacheDirs = ["songs", "imgs"] var cacheDirs = ["songs", "imgs"]
for (var i = 0; i < cacheDirs.length; i++) { for (var i = 0; i < cacheDirs.length; i++) {
@ -36,7 +36,7 @@ const spotifydl = new Spotify({
}) })
const songCache = path.join(cachePath, "songs"), const songCache = path.join(cachePath, "songs"),
imgCache = path.join(cachePath, "imgs") imgCache = path.join(cachePath, "imgs")
var app = express() var app = express()
@ -59,24 +59,28 @@ app.get("/cached/*", (req, res) => {
function waitForImage() { function waitForImage() {
if (imgWait < imgWaitMax) { if (imgWait < imgWaitMax) {
imgWait += 0.1 imgWait += 0.1
setTimeout(() => {
imgData = cachedImages.imgs[imgURL] imgData = cachedImages.imgs[imgURL]
console.log(imgData)
if (imgData) { if (imgData) {
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res) var imgPath = path.join(imgCache, imgData.file)
} else {
waitForImage() if (fs.existsSync(imgPath)) {
var img = fs.readFileSync(imgPath)
if (fs.existsSync(imgPath) && (img.toString().includes("<html") || fs.statSync(imgPath).size < 1000)) {
fs.rmSync(imgPath)
} }
}
} else {
setTimeout(() => {
waitForImage()
}, 100); }, 100);
}
} else { } else {
fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res) fs.createReadStream(path.join(__dirname, "/imgs/notFound.png")).pipe(res)
} }
} }
if (imgData) {
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
} else {
waitForImage() waitForImage()
}
}) })
app.listen(PORT, () => { app.listen(PORT, () => {
@ -176,21 +180,37 @@ function socketeer() {
station.next() station.next()
} }
} else { } else {
console.log(spotify) // console.log(spotify)
} }
for (let index = 0; index < lanyardData.activities.length; index++) { for (let index = 0; index < lanyardData.activities.length; index++) {
const activity = lanyardData.activities[index]; const activity = lanyardData.activities[index];
downloadFromActivity(activity)
}
}
})
}
socketeer()
async function downloadFromActivity(activity) {
var imgType = undefined var imgType = undefined
var imgRes = "512x512/" 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.imgs).length + "." + imgExtension var fn = Object.keys(cachedImages.imgs).length + "." + imgExtension
if (cachedImages.imgs[url] && !fs.existsSync(path.join(imgCache, cachedImages.imgs[url].file))) {
fn = Object.keys(cachedImages.imgs).indexOf(url) + "." + imgExtension
}
var fp = path.join(imgCache, fn) var fp = path.join(imgCache, fn)
if (!cachedImages.imgs[url]) { if (!cachedImages.imgs[url] || !fs.existsSync(path.join(imgCache, cachedImages.imgs[url].file))) {
console.log(fn)
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))
@ -204,10 +224,4 @@ function socketeer() {
imgRes = "128x128/" imgRes = "128x128/"
} }
} }
}
}
})
} }
socketeer()