class wattpad { constructor(fetchPath, cheerioPath, browser) { this.browser = browser; this.fetch = require(fetchPath); this.cheerio = require(cheerioPath); this.baseUrl = "https://wattpad.com"; this.type = "book-board"; } async fetchSearchResult(query = "", page = 1) { if (!query || query.trim() === "") { const res = await this.fetch("https://www.wattpad.com/"); const html = await res.text(); const $ = this.cheerio.load(html); const results = []; $("li.splide__slide").each((_, el) => { const li = $(el); const link = li.find("a[data-testid='coverLink']").attr("href") || ""; const img = li.find("img[data-testid='image']").attr("src") || ""; const title = li.find("img[data-testid='image']").attr("alt") || ""; if (link && img) { const id = link.split("/story/")[1]?.split(/[^0-9]/)[0] || null; if (id) { results.push({ id, title, image: img, sampleImageUrl: img, tags: [], type: "book" }); } } }); return { results, hasNextPage: false, page: 1 }; } const limit = 15; const offset = (page - 1) * limit; const url = `${this.baseUrl}/v4/search/stories?query=${query}&fields=stories%28id%2Ctitle%2CvoteCount%2CreadCount%2CcommentCount%2Cdescription%2Ccompleted%2Cmature%2Ccover%2Curl%2CisPaywalled%2CpaidModel%2Clength%2Clanguage%28id%29%2Cuser%28name%29%2CnumParts%2ClastPublishedPart%28createDate%29%2Cpromoted%2Csponsor%28name%2Cavatar%29%2Ctags%2Ctracking%28clickUrl%2CimpressionUrl%2CthirdParty%28impressionUrls%2CclickUrls%29%29%2Ccontest%28endDate%2CctaLabel%2CctaURL%29%29%2Ctotal%2Ctags%2Cnexturl&limit=${limit}&mature=false&offset=${offset}`; const res = await this.fetch(url); const json = await res.json(); const results = json.stories.map(n => ({ id: n.id, title: n.title, image: n.cover, sampleImageUrl: n.cover, tags: n.tags, type: "book" })); const totalPages = Math.ceil(json.total / limit); const hasNextPage = page < totalPages; return { results, hasNextPage, page }; } async findChapters(bookId) { const res = await this.fetch(`${this.baseUrl}/story/${bookId}`); const html = await res.text(); const $ = this.cheerio.load(html); const chapters = []; $('div.Y26Ib ul[aria-label="story-parts"] li a').each((i, el) => { const href = $(el).attr("href") || ""; const match = href.match(/wattpad\.com\/(\d+)/); const id = match ? match[1] : null; const titleText = $(el).find('div.wpYp-').text().trim(); let chapterNumber = i + 1; let title = titleText; const match2 = titleText.match(/^(\d+)\s*-\s*(.*)$/); if (match2) { chapterNumber = parseInt(match[1], 10); title = match2[2].trim(); } chapters.push({ id: id, title, chapter: chapterNumber, language: "en" }); }); return { chapters }; } async findChapterPages(chapterId) { const ampUrl = `https://www.wattpad.com/amp/${chapterId}`; const res = await this.fetch(ampUrl); const html = await res.text(); const $ = this.cheerio.load(html); const title = $("#amp-reading h2").first().text().trim(); const titleHtml = title ? `
${h}
`); } return; } const ampImg = p.find("amp-img").first(); if (ampImg.length) { const src = ampImg.attr("src") || ""; const width = ampImg.attr("width") || ""; const height = ampImg.attr("height") || ""; if (src) { paragraphsHtml.push( `