mangakonekuto
Version:
Your CLI for reading manga from the terminal
78 lines (77 loc) • 3.9 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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"));
function getImagesAndCreatePDF(chapterUrl, mangaTitle, chapterTitle) {
return __awaiter(this, void 0, void 0, function* () {
const browser = yield puppeteer_1.default.launch({
executablePath: process.env.CHROME_PATH,
headless: true
});
const page = yield browser.newPage();
yield page.goto(chapterUrl, { waitUntil: 'networkidle0' });
// Scrape image URLs
const imageUrls = yield 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);
});
yield browser.close();
if (imageUrls.length === 0) {
throw new Error("No images found to build the PDF.");
}
// Get download folder from Enmap
const baseFolder = SettingsConfig_1.default.get("Settings.DownloadFolder") || "Images";
const imagesDir = path_1.default.join(baseFolder, `${mangaTitle}_${chapterTitle}`);
yield 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 = yield axios_1.default.get(imageUrl, { responseType: 'arraybuffer' });
yield fs_extra_1.default.writeFile(localPath, response.data);
imagePaths.push(localPath);
}
// Build PDF
const pdfDoc = yield pdf_lib_1.PDFDocument.create();
for (const imgPath of imagePaths) {
const imageBytes = yield fs_extra_1.default.readFile(imgPath);
let image;
if (imgPath.endsWith('.png')) {
image = yield pdfDoc.embedPng(imageBytes);
}
else {
image = yield 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 = yield pdfDoc.save();
const pdfPath = path_1.default.join(imagesDir, `${chapterTitle}.pdf`);
yield fs_extra_1.default.writeFile(pdfPath, pdfBytes);
return pdfPath;
});
}