diff options
Diffstat (limited to 'src/lib/Data/Manga/raw.ts')
| -rw-r--r-- | src/lib/Data/Manga/raw.ts | 109 |
1 files changed, 74 insertions, 35 deletions
diff --git a/src/lib/Data/Manga/raw.ts b/src/lib/Data/Manga/raw.ts index 4dfa6a8e..64ed3de4 100644 --- a/src/lib/Data/Manga/raw.ts +++ b/src/lib/Data/Manga/raw.ts @@ -1,47 +1,86 @@ -import proxy from '$lib/Utility/proxy'; +import proxy from "$lib/Utility/proxy"; interface Chapter { - href: string; - chapterNum: string; - chapterDate: string; + href: string; + chapterNum: string; + chapterDate: string; } -export const getChapterCount = async (nativeTitle: string): Promise<number | undefined> => { - const html = new DOMParser().parseFromString( - await (await fetch(proxy(`https://rawkuma.com/?s=${encodeURIComponent(nativeTitle)}`))).text(), - 'text/html' - ); - const listContent = html.querySelector('.listupd'); +const RAWKUMA_ORIGIN = "https://rawkuma.net"; - if (listContent && listContent.textContent && listContent.textContent.includes('Not Found')) { - return undefined; - } +const fetchDocument = async (url: string, init?: RequestInit) => + new DOMParser().parseFromString( + await (await fetch(proxy(url, true), init)).text(), + "text/html", + ); - const chapterCount = html.querySelector('.epxs'); +const parseChapterNumber = (text: string | null | undefined) => { + if (!text) return undefined; - if (chapterCount && chapterCount.textContent && chapterCount.textContent.includes('Chapter')) { - return Number.parseInt(chapterCount.textContent.replace('Chapter', '').trim(), 10); - } + const match = text.match(/Chapter\s+(\d+(?:\.\d+)?)/i); - return undefined; + return match ? Number.parseFloat(match[1]) : undefined; +}; + +export const getChapterCount = async ( + nativeTitle: string, +): Promise<number | undefined> => { + const nonceDocument = await fetchDocument( + `${RAWKUMA_ORIGIN}/wp-admin/admin-ajax.php?type=search_form&action=get_nonce`, + ); + const nonce = nonceDocument + .querySelector("input[name='search_nonce']") + ?.getAttribute("value"); + + if (!nonce) return undefined; + + const searchDocument = await fetchDocument( + `${RAWKUMA_ORIGIN}/wp-admin/admin-ajax.php?nonce=${encodeURIComponent( + nonce, + )}&action=search`, + { + method: "POST", + headers: { + "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", + }, + body: new URLSearchParams({ + query: nativeTitle, + }), + }, + ); + const mangaUrl = searchDocument + .querySelector("#searchResults a[href*='/manga/']") + ?.getAttribute("href"); + + if (!mangaUrl) return undefined; + + const mangaDocument = await fetchDocument(mangaUrl); + const chapters = [...mangaDocument.querySelectorAll("a[href*='/chapter-']")] + .map((anchor) => parseChapterNumber(anchor.textContent)) + .filter((value): value is number => value !== undefined) + .sort((left, right) => right - left); + + return chapters[0]; }; export const getChaptersFromText = (text: string) => { - const dom = new DOMParser().parseFromString(text, 'text/html').querySelectorAll('.eph-num'); - const chapters: Chapter[] = []; - - dom.forEach((chapter) => { - const href = chapter.querySelector('a')?.getAttribute('href'); - const chapterNum = chapter.querySelector('.chapternum')?.textContent; - const chapterDate = chapter.querySelector('.chapterdate')?.textContent; - - if (href && chapterNum && chapterDate) - chapters.push({ - href, - chapterNum, - chapterDate - }); - }); - - return chapters; + const dom = new DOMParser() + .parseFromString(text, "text/html") + .querySelectorAll(".eph-num"); + const chapters: Chapter[] = []; + + dom.forEach((chapter) => { + const href = chapter.querySelector("a")?.getAttribute("href"); + const chapterNum = chapter.querySelector(".chapternum")?.textContent; + const chapterDate = chapter.querySelector(".chapterdate")?.textContent; + + if (href && chapterNum && chapterDate) + chapters.push({ + href, + chapterNum, + chapterDate, + }); + }); + + return chapters; }; |