UNPKG

comic-book-dl

Version:
175 lines (174 loc) 6.52 kB
import got from '../../../../node_modules/got/dist/source/index.js'; import { load } from 'cheerio'; import pLimit from 'p-limit'; import { Base } from '../../../lib/parse/base/index.js'; import { fixPathName, sleep } from '../../../utils/index.js'; import { UA } from '../../../utils/index.js'; export class Godamanga extends Base { constructor() { super(...arguments); this.type = 'Godamanga'; } async parseBookInfo() { const url = this.bookUrl; const rawUrl = url; let response; try { response = await got.get(url, this.genReqOptions()); } catch (e) { return false; } if (!response || response.statusCode !== 200) { return false; } let $ = load(response.body); let name = $('#info .gap-unit-xs .text-xl').text().trim(); const _name = $('#info .gap-unit-xs .text-xl .text-xs').text().trim(); name = name.replace(_name, '').trim(); const desc = $('#info .block .text-medium').text().trim(); const author = $('#info .block div:nth-child(2) a span').text().trim(); const coverUrl = $('#MangaCard > div > div:nth-child(1) img').attr('src')?.trim() ?? ''; const chaptersAllUrl = $('.my-unit-sm a').attr('href'); let chapters = []; const { origin } = new URL(url); if (chaptersAllUrl) { const chaptersAllHref = new URL(chaptersAllUrl, origin).href; const res = await got.get(chaptersAllHref, this.genReqOptions()).catch(() => ({ body: '' })); $ = load(res.body); } const chaptersElSelector = chaptersAllUrl ? '#allchapters' : '.peer-checked\\:block #chapterlists .chapteritem'; const chaptersEl = $(chaptersElSelector); const mid = chaptersEl.data('mid'); if (!mid) return false; let chaptersList = []; let chaptersHrefPrefix = ''; try { const chaptersAPI = `https://api-get-v2.mgsearcher.com/api/manga/get?mid=${mid}&mode=all`; const response = await fetch(chaptersAPI, { headers: this.genReqOptions().headers, method: 'GET' }); const bodyText = await response.text(); const data = JSON.parse(bodyText); if (data.status && Array.isArray(data?.data?.chapters)) { chaptersList = data?.data?.chapters; chaptersHrefPrefix = `/manga/${data?.data?.slug}`; } } catch (e) { return false; } chaptersList.forEach((data, index) => { const name = data?.attributes?.title?.trim() || ''; const slug = data?.attributes?.slug?.trim() || ''; const href = `${chaptersHrefPrefix}/${slug}`; chapters.push({ name: `${index}_${fixPathName(name)}`, rawName: name, href: `${origin}${href}`, imageList: [], imageListPath: [], index }); }); if (!name || chapters.length === 0) { return false; } chapters = chapters.map((item, index) => { const newItem = { ...item }; if (index !== 0) { newItem.preChapter = { name: chapters[index - 1].name, rawName: chapters[index - 1].rawName, href: chapters[index - 1].href, index: chapters[index - 1].index }; } if (index !== chapters.length - 1) { newItem.nextChapter = { name: chapters[index + 1].name, rawName: chapters[index + 1].rawName, href: chapters[index + 1].href, index: chapters[index + 1].index }; } return newItem; }); return { name, pathName: fixPathName(name), author, desc, coverUrl, coverPath: '', chapters, url, language: '简体', rawUrl }; } async getImgList(chapterUrl) { const response = await got(chapterUrl, this.genReqOptions()); const $ = load(response.body); const domInfo = $('#chapterContent'); const mid = domInfo.data('ms'); const cid = domInfo.data('cs'); let imgList = []; try { const chaptersAPI = `https://api-get-v2.mgsearcher.com/api/chapter/getinfo?m=${mid}&c=${cid}`; const response = await fetch(chaptersAPI, { headers: this.genReqOptions().headers, method: 'GET' }); const bodyText = await response.text(); const data = JSON.parse(bodyText); if (data?.status && Array.isArray(data?.data?.info?.images?.images)) { imgList = data?.data?.info?.images?.images.map((item) => { const imgHost = data?.data?.info?.images?.line === 2 ? 'https://f40-1-4.g-mh.online' : 'https://t40-1-4.g-mh.online'; return `${imgHost}${item?.url}` || ''; }); } } catch (e) { console.log(e); return []; } return [...new Set(imgList)]; } async saveImgList(path, imgList, saveImgCallback) { const limit = pLimit(6); const promiseList = imgList.map((imgUrl, index) => limit(async () => { let isSuccess = true; let imgFileName = ''; try { imgFileName = await this.saveImg(path, imgUrl, String(index + 1), 'jpg'); } catch (err) { isSuccess = false; } if (typeof saveImgCallback === 'function') saveImgCallback(imgUrl, isSuccess); return imgFileName; })); return await Promise.all(promiseList); } async saveImg(path, imgUrl, fixFileName, fixSuffix) { await sleep(600); const res = await super.saveImg(path, imgUrl, fixFileName, fixSuffix); await sleep(600); return res; } genReqOptions() { return { headers: { 'user-agent': UA, referer: 'https://m.baozimh.one/' }, http2: true }; } }