gyazoimg
gyazoids.txtの例
code:gyazoids.txt
fc7db29078803743cdc9ed9a45299b18
fd7b16f707d6c192c11a37a3efe7d384
code:mod.ts
import { fileTypeFromBuffer } from "npm:file-type";
async function downloadImage(url: string, folderPath: string): Promise<void> {
const response = await fetch(url);
const buffer = await response.arrayBuffer();
const fileType = await fileTypeFromBuffer(buffer);
const ext = fileType?.ext || "jpg";
const toks = url.split("/");
toks.pop(); // remove "raw"
const fileName = toks.pop() || "image";
const filePath = ${folderPath}/${fileName}.${ext};
await Deno.writeFile(filePath, new Uint8Array(buffer));
}
async function main() {
if (Deno.args.length < 1) {
console.error(
"Usage: deno run --allow-net --allow-read --allow-write download_gyazo_images.ts <gyazo-ids-file-path>"
);
Deno.exit(1);
}
const filePath = Deno.args0; const fileContent = await Deno.readTextFile(filePath);
const gyazoIds = fileContent.split("\n");
const urls = gyazoIds
.filter((x) => !!x)
.map((id) => https://gyazo.com/${id}/raw);
const now = Math.floor(new Date().getTime() / 1000);
const folderPath =
"gyazo_" + new Date().toISOString().split("T")0 + _${now}; console.log(">", folderPath);
await Deno.mkdir(folderPath, { recursive: true });
for (const idx, url of urls.entries()) { if (idx % 10 === 0) {
console.log(Progress: ${idx + 1} / ${urls.length});
}
if (url.trim() !== "") {
try {
await downloadImage(url, folderPath);
console.log(\tDownloaded: ${url});
} catch (error) {
console.error(Failed to download ${url}: ${error});
}
}
}
}
main();
9割くらいChatGPTが書いてくれた