borders/util/ingest.js

125 lines
3.1 KiB
JavaScript
Raw Normal View History

2022-04-17 01:14:14 +00:00
const fs = require("fs");
const path = require("path");
2022-04-11 17:33:16 +00:00
const Prisma = require("@prisma/client");
const prisma = new Prisma.PrismaClient();
console.log("processing new border images");
const cwd = process.cwd();
2022-04-17 01:14:14 +00:00
const folder = path.join(
cwd,
cwd.includes("util") ? ".." : "",
"public/images"
);
2022-04-17 04:24:59 +00:00
2022-04-11 17:33:16 +00:00
const list = fs.readdirSync(folder);
2022-04-17 04:24:59 +00:00
let catalogue = async () => {
2022-04-17 01:14:14 +00:00
let numAdded = 0;
for (let item of list) {
// console.log(item);
2022-04-11 17:33:16 +00:00
2022-04-17 01:14:14 +00:00
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,
2022-04-17 04:24:59 +00:00
borderName: "Default",
2022-04-24 02:58:54 +00:00
itemType: 54,
2022-04-17 01:14:14 +00:00
},
});
numAdded++;
console.log(added);
} else if (!result) {
const added = await prisma.borderImage.create({
data: {
imageName: item,
2022-04-20 14:01:43 +00:00
appName: "Manually added",
2022-04-17 01:14:14 +00:00
},
});
numAdded++;
console.log(added);
2022-04-11 17:33:16 +00:00
}
2022-04-17 01:14:14 +00:00
}
console.log(`Processed ${numAdded} new images.`);
2022-04-17 04:24:59 +00:00
};
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;
2022-04-20 14:01:43 +00:00
for (let key in json_data) {
let value = json_data[key];
2022-04-17 04:24:59 +00:00
if (!value) continue;
const filename_regex =
2022-04-20 14:01:43 +00:00
/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");
2022-04-17 04:24:59 +00:00
if (!list.includes(filename)) {
console.log("N", filename);
2022-04-20 14:01:43 +00:00
setTimeout(async () => {
let data = await fetch.default(value.borderURL);
data.body.pipe(fs.createWriteStream(path.join(folder, filename)));
}, key * 100);
2022-04-17 04:24:59 +00:00
}
const result = await prisma.borderImage.findFirst({
where: {
imageName: filename,
},
});
2022-04-20 14:01:43 +00:00
if (
result?.appId != value.appInfo.appid ||
result?.appName != value.appInfo.name ||
result?.borderName != value.name
) {
2022-04-17 04:24:59 +00:00
const added = await prisma.borderImage.upsert({
create: {
imageName: filename,
appId: value.appInfo.appid,
appName: value.appInfo.name,
borderName: value.name,
2022-04-24 02:58:54 +00:00
itemType: value.itemType,
2022-04-17 04:24:59 +00:00
},
update: {
imageName: filename,
appId: value.appInfo.appid,
appName: value.appInfo.name,
borderName: value.name,
2022-04-24 02:58:54 +00:00
itemType: value.itemType,
2022-04-17 04:24:59 +00:00
},
where: {
imageName: filename,
},
});
2022-04-20 14:01:43 +00:00
// console.log(added);
2022-04-17 04:24:59 +00:00
numAdded++;
}
}
console.log(`Upserted ${numAdded} values.`);
};
download().then(() => {
catalogue().then(() => {
console.log("done.");
2022-04-20 14:01:43 +00:00
console.log("waiting for image catch up to finish...");
2022-04-17 04:24:59 +00:00
});
});