plugin-books-pro
Version:
[](https://badge.fury.io/js/plugin-books-pro)
159 lines (158 loc) • 6.78 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const t2_browser_worker_1 = require("t2-browser-worker");
const base_1 = require("../models/base");
class Manhuavn extends base_1.BaseBook {
constructor() {
super(...arguments);
this.path = {
NEW: "/danhsach/P1/index.html?status=0&sort=0",
TOP: "/danhsach/P1/index.html?status=0&sort=1",
FAVORITE: "/danhsach/P1/index.html?status=0&sort=2"
};
this.selectors = {
bookList: ".lst_story .story_title",
title: ".wrap-info-title-part .title",
image: ".wrap-content-image img",
view: ".info-row:has(i.fa-eye) view.colorblue",
follow: ".info-row:has(i.fa-user-plus) strong#numbertheodoi",
lastChapter: ".info-row:has(i.fa-bolt) a.colorblue",
tags: ".clearfix:has(i.fa-tags) a",
rating: "span[itemprop='ratingValue']",
comment: ".header-content-part .colorblue",
description: ".clearfix p"
};
}
getListBookLink(page, url) {
return __awaiter(this, void 0, void 0, function* () {
yield page.goto(url, { waitUntil: "domcontentloaded" });
yield page.waitForSelector(this.selectors.bookList);
const bookItems = yield page.$$(this.selectors.bookList);
return Promise.all(bookItems
.slice(0, this.LIMIT_ITEMS)
.map(book => book.getAttribute("href"))).then(links => links.filter(Boolean));
});
}
extractBookData(page, index, link) {
return __awaiter(this, void 0, void 0, function* () {
yield page.goto(`${this.baseUrl}${link}`, { waitUntil: "domcontentloaded" });
const [name, imageUrlThumbnail, viewText, followText, lastChapterText, tagTexts, ratingText, commentText, description] = yield Promise.all([
this.getElementText(page, this.selectors.title),
this.getElementAttribute(page, this.selectors.image, "src"),
this.getElementText(page, this.selectors.view),
this.getElementText(page, this.selectors.follow),
this.getElementText(page, this.selectors.lastChapter),
this.getElementsText(page, this.selectors.tags),
this.getElementText(page, this.selectors.rating),
this.getElementText(page, this.selectors.comment),
this.getElementText(page, this.selectors.description)
]);
return {
rank: index + 1,
identifier: this.getIdentifier(name),
name,
imageUrlThumbnail,
link,
view: this.parseViewCount(viewText),
follow: this.parseViewCount(followText),
lastChapter: this.parseNumber(lastChapterText),
tags: this.formatTags(tagTexts),
rating: parseFloat(ratingText) || 0,
comment: this.parseNumber(commentText),
description
};
});
}
getElementText(page, selector) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield page.$(selector);
return element ? ((yield element.textContent()) || "") : "";
});
}
getElementAttribute(page, selector, attr) {
return __awaiter(this, void 0, void 0, function* () {
const element = yield page.$(selector);
return element ? ((yield element.getAttribute(attr)) || "") : "";
});
}
getElementsText(page, selector) {
return __awaiter(this, void 0, void 0, function* () {
const elements = yield page.$$(selector);
return Promise.all(elements.map(el => el.textContent()))
.then(texts => texts.filter(Boolean));
});
}
formatTags(tags) {
return tags.map(tag => tag.trim()).join(',');
}
parseNumber(text) {
return parseInt(text.replace(/[^0-9]/g, '')) || 0;
}
parseViewCount(viewText) {
const cleanText = viewText.toLowerCase().trim();
const number = parseFloat(cleanText.replace(/[^0-9.]/g, '')) || 0;
if (cleanText.includes('k')) {
return Math.round(number * 1000);
}
if (cleanText.includes('m')) {
return Math.round(number * 1000000);
}
return Math.round(number);
}
navigateAndFetch(page, path, dataType) {
return __awaiter(this, void 0, void 0, function* () {
const result = {
dataType,
data: [],
status: "SUCCESS"
};
const bookLinks = yield this.getListBookLink(page, `${this.baseUrl}${path}`);
for (const [index, link] of bookLinks.entries()) {
const bookData = yield this.extractBookData(page, index, link);
result.data.push(bookData);
}
return result;
});
}
getNew(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.navigateAndFetch(page, this.path.NEW, "New");
});
}
getFavorite(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.navigateAndFetch(page, this.path.FAVORITE, "Favorite");
});
}
getTop(page) {
return __awaiter(this, void 0, void 0, function* () {
return this.navigateAndFetch(page, this.path.TOP, "Favorite");
});
}
crawl(page) {
const _super = Object.create(null, {
crawl: { get: () => super.crawl }
});
return __awaiter(this, void 0, void 0, function* () {
(0, t2_browser_worker_1.useBlockResource)(page, [
"image",
"media",
"font",
"script",
"stylesheet",
"xhr"
]);
return _super.crawl.call(this, page);
});
}
}
exports.default = Manhuavn;