UNPKG

koneko-cli

Version:

Your CLI for reading manga from the terminal

72 lines (71 loc) 3.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getImagesAndCreatePDF = getImagesAndCreatePDF; const puppeteer_1 = __importDefault(require("puppeteer")); const axios_1 = __importDefault(require("axios")); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const pdf_lib_1 = require("pdf-lib"); const SettingsConfig_1 = __importDefault(require("../../utils/SettingsConfig")); async function getImagesAndCreatePDF(chapterUrl, mangaTitle, chapterTitle) { const browser = await puppeteer_1.default.launch({ executablePath: process.env.CHROME_PATH, headless: true, }); const page = await browser.newPage(); await page.goto(chapterUrl, { waitUntil: 'networkidle0' }); // Scrape image URLs const imageUrls = await page.evaluate(() => { const imgs = Array.from(document.querySelectorAll('div.page-break img')); return imgs.map(img => { var _a; return ((_a = img.getAttribute('src')) === null || _a === void 0 ? void 0 : _a.trim()) || ''; }).filter(Boolean); }); await browser.close(); if (imageUrls.length === 0) { throw new Error("No images found to build the PDF."); } // Await SettingsConfig.get because it returns a Promise const baseFolderSetting = await SettingsConfig_1.default.get("Settings.DownloadFolder"); const baseFolder = baseFolderSetting || "Images"; // Resolve to absolute path - for example relative to current working directory const absoluteBaseFolder = path_1.default.isAbsolute(baseFolder) ? baseFolder : path_1.default.resolve(process.cwd(), baseFolder); const imagesDir = path_1.default.join(absoluteBaseFolder, `${mangaTitle}_${chapterTitle}`); await fs_extra_1.default.ensureDir(imagesDir); // Download images const imagePaths = []; for (let i = 0; i < imageUrls.length; i++) { const imageUrl = imageUrls[i]; const ext = path_1.default.extname(new URL(imageUrl).pathname) || '.jpg'; const localPath = path_1.default.join(imagesDir, `page-${i + 1}${ext}`); const response = await axios_1.default.get(imageUrl, { responseType: 'arraybuffer' }); await fs_extra_1.default.writeFile(localPath, response.data); imagePaths.push(localPath); } // Build PDF const pdfDoc = await pdf_lib_1.PDFDocument.create(); for (const imgPath of imagePaths) { const imageBytes = await fs_extra_1.default.readFile(imgPath); let image; if (imgPath.endsWith('.png')) { image = await pdfDoc.embedPng(imageBytes); } else { image = await pdfDoc.embedJpg(imageBytes); } const page = pdfDoc.addPage([image.width, image.height]); page.drawImage(image, { x: 0, y: 0, width: image.width, height: image.height, }); } const pdfBytes = await pdfDoc.save(); const pdfPath = path_1.default.join(imagesDir, `${chapterTitle}.pdf`); await fs_extra_1.default.writeFile(pdfPath, pdfBytes); return pdfPath; }