UNPKG

ddg-bulk-image-downloader

Version:

Lazy way to download images from Duck Duck Go search results in bulk

24 lines (23 loc) 974 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const node_fetch_1 = require("node-fetch"); const file_type_1 = require("file-type"); const fs_1 = require("fs"); const path_1 = require("path"); const downloadImage = async (url, filePath) => { var _a; const response = await node_fetch_1.default(url); const fileName = path_1.parse(filePath).name; if (response.status !== 200) { throw new Error(`Skipped : ${fileName}. ${response.status} - ${url}`); } const buffer = await response.buffer(); // TODO: and also content type ? If non image ? const { ext } = (_a = (await file_type_1.fromBuffer(buffer))) !== null && _a !== void 0 ? _a : {}; if (!ext) { throw new Error(`Failed to identify image format : ${fileName}. ${url}`); } const pathWithExtension = `${filePath}.${ext === "xml" ? "svg" : ext}`; fs_1.writeFileSync(pathWithExtension, buffer); }; exports.default = downloadImage;