borders/util/ingest.js

122 lines
3.0 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const Prisma = require("@prisma/client");
const prisma = new Prisma.PrismaClient();
console.log("processing new border images");
const cwd = process.cwd();
const folder = path.join(
cwd,
cwd.includes("util") ? ".." : "",
"public/images"
);
const list = fs.readdirSync(folder);
let catalogue = async () => {
let numAdded = 0;
for (let item of list) {
// console.log(item);
const result = await prisma.borderImage.findFirst({
where: {
imageName: item,
},
});
if (!result && item == "default.png") {
const added = await prisma.borderImage.create({
data: {
id: 0,
imageName: item,
borderName: "Default",
},
});
numAdded++;
console.log(added);
} else if (!result) {
const added = await prisma.borderImage.create({
data: {
imageName: item,
appName: "Manually added",
},
});
numAdded++;
console.log(added);
}
}
console.log(`Processed ${numAdded} new images.`);
};
let download = async () => {
const fetch = await import("node-fetch");
const filePath = path.join(
cwd,
cwd.includes("util") ? ".." : "",
"util/border_data.json"
);
const json_data = JSON.parse(fs.readFileSync(filePath));
let numAdded = 0;
for (let key in json_data) {
let value = json_data[key];
if (!value) continue;
const filename_regex =
/https:\/\/cdn.akamai.steamstatic.com\/steamcommunity\/public\/images\/items\/\d+\/\/?(.+\.(a?png|gif))/gi;
const file_match = filename_regex.exec(value.borderURL);
if (!file_match || file_match == null) {
console.error("MATCH ERROR", value.borderURL);
}
const filename = file_match[1].replace("apng", "png");
if (!list.includes(filename)) {
console.log("N", filename);
setTimeout(async () => {
let data = await fetch.default(value.borderURL);
data.body.pipe(fs.createWriteStream(path.join(folder, filename)));
}, key * 100);
}
const result = await prisma.borderImage.findFirst({
where: {
imageName: filename,
},
});
if (
result?.appId != value.appInfo.appid ||
result?.appName != value.appInfo.name ||
result?.borderName != value.name
) {
const added = await prisma.borderImage.upsert({
create: {
imageName: filename,
appId: value.appInfo.appid,
appName: value.appInfo.name,
borderName: value.name,
},
update: {
imageName: filename,
appId: value.appInfo.appid,
appName: value.appInfo.name,
borderName: value.name,
},
where: {
imageName: filename,
},
});
// console.log(added);
numAdded++;
}
}
console.log(`Upserted ${numAdded} values.`);
};
download().then(() => {
catalogue().then(() => {
console.log("done.");
console.log("waiting for image catch up to finish...");
});
});