UNPKG

plugin-books-pro

Version:

[![npm version](https://badge.fury.io/js/plugin-books-pro.svg)](https://badge.fury.io/js/plugin-books-pro)

125 lines (124 loc) 5.8 kB
"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 TruyenQQ extends base_1.BaseBook { constructor() { super(...arguments); this.SELECTORS = { BOOK_ITEMS: "#main_homepage .list_grid_out li", IMAGE: ".book_avatar img", NAME: ".book_info .book_name a", VIEW: ".book_info .text_detail span:nth-child(1)", FOLLOW: ".book_info .text_detail span:nth-child(2)", LAST_CHAPTER: ".book_info .last_chapter a", TAGS: ".book_info .more-info .list-tags .blue", Link: ".book_avatar a" }; this.ROUTES = { TOP: "/top-ngay", NEW: "/truyen-tranh-moi", FAVORITE: "/truyen-yeu-thich" }; } extractBookData(book, index) { return __awaiter(this, void 0, void 0, function* () { const imageElement = yield book.$(this.SELECTORS.IMAGE); const nameElement = yield book.$(this.SELECTORS.NAME); const viewElement = yield book.$(this.SELECTORS.VIEW); const followElement = yield book.$(this.SELECTORS.FOLLOW); const lastChapterElement = yield book.$(this.SELECTORS.LAST_CHAPTER); const tagElements = yield book.$$(this.SELECTORS.TAGS); const linkElement = yield book.$(this.SELECTORS.Link); const imageUrlThumbnail = (yield (imageElement === null || imageElement === void 0 ? void 0 : imageElement.getAttribute("src"))) || ""; const name = (yield (nameElement === null || nameElement === void 0 ? void 0 : nameElement.getAttribute("title"))) || ""; const rawView = (yield (viewElement === null || viewElement === void 0 ? void 0 : viewElement.textContent())) || ""; const rawFollow = (yield (followElement === null || followElement === void 0 ? void 0 : followElement.textContent())) || ""; const rawLastChapter = (yield (lastChapterElement === null || lastChapterElement === void 0 ? void 0 : lastChapterElement.textContent())) || ""; const rawTags = yield Promise.all(tagElements.map((tag) => tag.textContent())); const link = (yield (linkElement === null || linkElement === void 0 ? void 0 : linkElement.getAttribute("href"))) || ""; return { rank: index + 1, identifier: this.getIdentifier(String(name)), name: String(name), link: String(link), imageUrlThumbnail: String(imageUrlThumbnail), view: this.justNumber(String(rawView)), follow: this.justNumber(String(rawFollow)), lastChapter: this.justNumber(String(rawLastChapter)), tags: rawTags.filter(Boolean).map(tag => tag.trim()).join(','), }; }); } subGetBook(page, dataType) { return __awaiter(this, void 0, void 0, function* () { const result = { dataType, data: [], status: "SUCCESS" }; try { yield page.waitForLoadState("domcontentloaded"); const bookItems = yield page.$$(this.SELECTORS.BOOK_ITEMS); for (const [index, book] of bookItems.entries()) { if (index === this.LIMIT_ITEMS) break; const bookData = yield this.extractBookData(book, index); result.data.push(bookData); } } catch (error) { console.error(`Error in subGetBook for ${dataType}:`, error); result.status = "ERROR"; } return result; }); } navigateAndFetch(page, route, dataType) { return __awaiter(this, void 0, void 0, function* () { yield page.goto(`${this.baseUrl}${route}`); return this.subGetBook(page, dataType); }); } getTop(page) { return __awaiter(this, void 0, void 0, function* () { return this.navigateAndFetch(page, this.ROUTES.TOP, "Top"); }); } getNew(page) { return __awaiter(this, void 0, void 0, function* () { return this.navigateAndFetch(page, this.ROUTES.NEW, "New"); }); } getFavorite(page) { return __awaiter(this, void 0, void 0, function* () { return this.navigateAndFetch(page, this.ROUTES.FAVORITE, "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 = TruyenQQ;