Phú Nguyễn

26 bundles
1 file3 months ago
1

Bato.to chapter crawler

index.js

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879"use strict";

const fs = require("fs");
const puppeteer = require("puppeteer");
const cheerio = require("cheerio");
const request = require("request-promise");
const req = require("request");
const url = require("url");

async function connect() {
    const link = "https://bato.to/chapter/989761";

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto(`${link}`);

    const viewer = await page.$("#viewer");
    let content = [];
    content = await viewer.evaluate(async () => {
        let images = document.querySelectorAll("img");
        images = Array.from(images);
        return images.map(img => img.src);
    });

    content = Array.from(content);

    const downloadedPath = "./downloaded";
    const mangaPath = "./downloaded/Twisted";

    if (content) {
        if (!fs.existsSync(downloadedPath)) {
            fs.mkdirSync(downloadedPath);
        }

        if (!fs.existsSync(mangaPath)) {
            fs.mkdirSync(mangaPath);
        }
    }

    content.splice(
        content.indexOf(
            "https://static.bato.to/img/manga/batoto-logo-2.png?v1"
        ),
        1
    );

    console.log(content);

    content &&
        content.forEach((image, index) => {
            const parsedLink = url.parse(`${image}`);
            const pathName = parsedLink.pathname;
            const originalName = /[^/]*$/.exec(pathName)[0];

            let fileType = originalName.substr(originalName.indexOf("."));
            let fileName = "";

            if (index >= 0 && index <= 9) {
                fileName = `00${index}`;
            } else if (index >= 10 && index <= 99) {
                fileName = `0${index}`;
            } else {
                fileName = index;
            }

            // Request to get page.
            req.get(`${image}`)
                .on("error", err => {
                    console.log(err);
                })
                .pipe(
                    fs.createWriteStream(`${mangaPath}/${fileName}.${fileType}`)
                );
        });

    await browser.close();
}

connect();