news/utils/images.js

56 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-02-13 14:22:58 +01:00
import { join } from "https://deno.land/std@0.208.0/path/mod.ts";
2024-05-07 01:05:47 +02:00
import { ensureDir } from "https://deno.land/std@0.224.0/fs/ensure_dir.ts";
import { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
2024-02-13 14:22:58 +01:00
2024-05-07 01:05:47 +02:00
const DATA_DIR = "./data";
2024-02-13 14:22:58 +01:00
2024-05-07 01:05:47 +02:00
const imgDir = "./web/public/img";
await ensureDir(imgDir);
2024-02-13 14:22:58 +01:00
// get images
2024-05-07 01:05:47 +02:00
for (const { name: year } of Deno.readDirSync(DATA_DIR)) {
for (const { name: fn } of Deno.readDirSync(join(DATA_DIR, year))) {
const week = fn.match(/^week(\d+)/)?.[1];
if (!week) continue;
const yearWeek = `${year}-${week}`;
const imgn = `https://news.web3privacy.info/image/${yearWeek}?${new Date().valueOf()}`;
const outputFn = join(imgDir, `${yearWeek}.png`);
if (existsSync(outputFn)) continue;
console.log('Processing:', imgn);
await genImage(imgn, outputFn);
}
2024-02-13 14:22:58 +01:00
}
// make cover
2024-05-07 01:05:47 +02:00
const coverFn = join(imgDir, "cover.png");
if (!existsSync(coverFn)) {
await genImage(`https://news.web3privacy.info/cover`, coverFn);
}
2024-02-13 14:22:58 +01:00
console.log("Done");
2024-05-07 01:05:47 +02:00
// ---------
2024-02-13 14:22:58 +01:00
async function genImage(url, fn) {
const imgResp = await fetch("https://html2svg.gwei.cz", {
method: "POST",
body: JSON.stringify({
url,
format: "png",
width: 1920,
height: 960,
}),
2024-05-07 01:05:47 +02:00
headers: {
"content-type": "application/json",
}
2024-02-13 14:22:58 +01:00
});
if (imgResp.body) {
const file = await Deno.open(fn, { write: true, create: true });
await imgResp.body.pipeTo(file.writable);
console.log(`Image written: ${fn}`);
}
}