Restore images if broken
This commit is contained in:
parent
1b6663c716
commit
5af82414ea
1 changed files with 64 additions and 50 deletions
114
index.js
114
index.js
|
@ -1,24 +1,24 @@
|
|||
const express = require("express"),
|
||||
fs = require("fs"),
|
||||
path = require("path"),
|
||||
WebSocket = require('ws'),
|
||||
{ Station } = require("@fridgefm/radio-core")
|
||||
fs = require("fs"),
|
||||
path = require("path"),
|
||||
WebSocket = require('ws'),
|
||||
{ Station } = require("@fridgefm/radio-core")
|
||||
|
||||
const station = new Station({})
|
||||
|
||||
const PORT = process.env.PORT || 8080
|
||||
|
||||
const cachePath = path.join(__dirname, "cache"),
|
||||
cacheFile = path.join(__dirname, "cache.json"),
|
||||
configFile = path.join(__dirname, "config.json")
|
||||
cacheFile = path.join(__dirname, "cache.json"),
|
||||
configFile = path.join(__dirname, "config.json")
|
||||
|
||||
var lanyardData
|
||||
|
||||
if (!fs.existsSync(cachePath)) { fs.mkdirSync(cachePath) }
|
||||
if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
|
||||
fs.writeFileSync(cacheFile, fs.readFileSync(path.join(__dirname, "defaults/cache.json")))
|
||||
if (!fs.existsSync(cacheFile) || !JSON.parse(fs.readFileSync(cacheFile)).songs) {
|
||||
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"]
|
||||
for (var i = 0; i < cacheDirs.length; i++) {
|
||||
|
@ -36,7 +36,7 @@ const spotifydl = new Spotify({
|
|||
})
|
||||
|
||||
const songCache = path.join(cachePath, "songs"),
|
||||
imgCache = path.join(cachePath, "imgs")
|
||||
imgCache = path.join(cachePath, "imgs")
|
||||
|
||||
var app = express()
|
||||
|
||||
|
@ -54,29 +54,33 @@ app.get("/cached/*", (req, res) => {
|
|||
var imgURL = req.originalUrl
|
||||
imgURL = imgURL.substring(imgURL.indexOf("/", 2) + 1)
|
||||
var imgData = cachedImages.imgs[imgURL]
|
||||
|
||||
|
||||
var imgWait = 0
|
||||
function waitForImage() {
|
||||
if (imgWait < imgWaitMax) {
|
||||
imgWait += 0.1
|
||||
setTimeout(() => {
|
||||
imgData = cachedImages.imgs[imgURL]
|
||||
console.log(imgData)
|
||||
if (imgData) {
|
||||
fs.createReadStream(path.join(imgCache, imgData.file)).pipe(res)
|
||||
} else {
|
||||
waitForImage()
|
||||
imgData = cachedImages.imgs[imgURL]
|
||||
|
||||
if (imgData) {
|
||||
var imgPath = path.join(imgCache, imgData.file)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}, 100);
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
waitForImage()
|
||||
}, 100);
|
||||
}
|
||||
|
||||
} else {
|
||||
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, () => {
|
||||
|
@ -168,46 +172,56 @@ function socketeer() {
|
|||
lastSong = spotify.track_id
|
||||
station.addFolder(songPath)
|
||||
const removeSongs = (list) => list
|
||||
.filter(track => track.fsStats.fullPath !== songPath);
|
||||
|
||||
.filter(track => track.fsStats.fullPath !== songPath);
|
||||
|
||||
station.reorderPlaylist(removeSongs)
|
||||
|
||||
station.start()
|
||||
station.next()
|
||||
}
|
||||
} else {
|
||||
console.log(spotify)
|
||||
// console.log(spotify)
|
||||
}
|
||||
|
||||
for (let index = 0; index < lanyardData.activities.length; index++) {
|
||||
const activity = lanyardData.activities[index];
|
||||
|
||||
var imgType = undefined
|
||||
var imgRes = "512x512/"
|
||||
for (var i = 0; i < 2; i++) {
|
||||
if (get_img_url(activity, imgType)) {
|
||||
var url = get_img_url(activity, imgType)
|
||||
var fn = Object.keys(cachedImages.imgs).length + "." + imgExtension
|
||||
var fp = path.join(imgCache, fn)
|
||||
|
||||
if (!cachedImages.imgs[url]) {
|
||||
const response = await (await fetch(thumborURL + imgRes + thumborArgs + url)).arrayBuffer()
|
||||
|
||||
fs.writeFileSync(fp, Buffer.from(response))
|
||||
cachedImages.imgs[url] = {
|
||||
"file": fn,
|
||||
"lastUpdated": Date.now()
|
||||
}
|
||||
fs.writeFileSync(cacheFile, JSON.stringify(cachedImages))
|
||||
}
|
||||
imgType = "small_image"
|
||||
imgRes = "128x128/"
|
||||
}
|
||||
}
|
||||
downloadFromActivity(activity)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
socketeer()
|
||||
socketeer()
|
||||
|
||||
async function downloadFromActivity(activity) {
|
||||
var imgType = undefined
|
||||
var imgRes = "512x512/"
|
||||
for (var i = 0; i < 2; i++) {
|
||||
if (get_img_url(activity, imgType)) {
|
||||
var url = get_img_url(activity, imgType)
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
fs.writeFileSync(fp, Buffer.from(response))
|
||||
cachedImages.imgs[url] = {
|
||||
"file": fn,
|
||||
"lastUpdated": Date.now()
|
||||
}
|
||||
fs.writeFileSync(cacheFile, JSON.stringify(cachedImages))
|
||||
}
|
||||
imgType = "small_image"
|
||||
imgRes = "128x128/"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue