aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Data/Manga/raw.ts
blob: 4dfa6a8e2295e516d7c77e76349af30667e4bdb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import proxy from '$lib/Utility/proxy';

interface Chapter {
  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');

  if (listContent && listContent.textContent && listContent.textContent.includes('Not Found')) {
    return undefined;
  }

  const chapterCount = html.querySelector('.epxs');

  if (chapterCount && chapterCount.textContent && chapterCount.textContent.includes('Chapter')) {
    return Number.parseInt(chapterCount.textContent.replace('Chapter', '').trim(), 10);
  }

  return undefined;
};

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;
};