hgberdtzhgij

This commit is contained in:
Lea 2024-06-16 17:28:24 +02:00
parent 0d2ff988fd
commit 6a0152611d
Signed by: Lea
GPG key ID: 1BAFFE8347019C42

View file

@ -1,5 +1,5 @@
import path from "path"; import path from "path";
import { fileURLToPath } from "url"; import { fileURLToPath, URL } from "url";
import fs from "fs/promises"; import fs from "fs/promises";
import axios from "axios"; import axios from "axios";
import { createWriteStream } from "fs"; import { createWriteStream } from "fs";
@ -38,6 +38,8 @@ type CommonImages = {
} }
} }
const fsExists = async (path: string) => (await fs.stat(path).catch((e) => false)) !== false;
async function downloadToPath(url: string, path: string) { async function downloadToPath(url: string, path: string) {
const stream = createWriteStream(path); const stream = createWriteStream(path);
const response = await axios.get(url, { responseType: "stream" }); const response = await axios.get(url, { responseType: "stream" });
@ -50,15 +52,15 @@ async function downloadToPath(url: string, path: string) {
* unless it is already cached. * unless it is already cached.
*/ */
async function fetchScript(makerId: string) { async function fetchScript(makerId: string) {
const cacheDir = path.join(dirname, "..", "cache", makerId); const cacheDir = path.join(dirname, "..", "cache", "picrew", makerId);
const htmlPath = path.join(cacheDir, "index.html"); const htmlPath = path.join(cacheDir, "index.html");
const scriptPath = path.join(cacheDir, "script.js"); const scriptPath = path.join(cacheDir, "script.js");
await fs.mkdir(cacheDir, { recursive: true }) await fs.mkdir(cacheDir, { recursive: true })
.catch((e) => { if (e?.code != "EEXIST") throw e; }); // Ignore "already exists" error .catch((e) => { if (e?.code != "EEXIST") throw e; }); // Ignore "already exists" error
const htmlExists = (await fs.stat(htmlPath).catch((e) => false)) !== false; const htmlExists = await fsExists(htmlPath);
const scriptExists = (await fs.stat(scriptPath).catch((e) => false)) !== false; const scriptExists = await fsExists(scriptPath);
if (!htmlExists) { if (!htmlExists) {
await downloadToPath(`https://picrew.me/en/image_maker/${encodeURIComponent(makerId)}`, htmlPath); await downloadToPath(`https://picrew.me/en/image_maker/${encodeURIComponent(makerId)}`, htmlPath);
@ -83,7 +85,7 @@ async function fetchScript(makerId: string) {
} }
async function extractDataFromScript(makerId: string) { async function extractDataFromScript(makerId: string) {
const code = await fs.readFile(path.join(dirname, "..", "cache", makerId, "script.js"), { encoding: "utf8" }); const code = await fs.readFile(path.join(dirname, "..", "cache", "picrew", makerId, "script.js"), { encoding: "utf8" });
const isolate = new ivm.Isolate(); const isolate = new ivm.Isolate();
const context = await isolate.createContext(); const context = await isolate.createContext();
const script = await isolate.compileScript(code); const script = await isolate.compileScript(code);
@ -97,6 +99,27 @@ async function extractDataFromScript(makerId: string) {
}; };
} }
/**
* Download an asset if it's not cached already, then
* return it as buffer.
*/
async function getAssetBuffer(url: string) {
// https://cdn.picrew.me/app/image_maker/{makerId}/{pId}/{random_string}.png
const [ makerId, cId, filename ] = new URL(url).pathname.split("/").slice(-3);
const dir = path.join(dirname, "..", "cache", "assets", makerId, cId);
const filePath = path.join(dir, filename);
await fs.mkdir(dir, { recursive: true })
.catch((e) => { if (e?.code != "EEXIST") throw e; }); // Ignore "already exists" error
if (!await fsExists(filePath)) {
await downloadToPath(url, filePath);
}
return await fs.readFile(filePath);
}
await fetchScript("1904634"); await fetchScript("1904634");
const { config, commonImages, cdnRoot } = await extractDataFromScript("1904634"); const { config, commonImages, cdnRoot } = await extractDataFromScript("1904634");
@ -138,5 +161,3 @@ for (const c in config.pList) {
items.push({ itemId: id.toString(), layers: layers }); items.push({ itemId: id.toString(), layers: layers });
} }
} }
console.log(layerColors);