diff options
| author | Fuwn <[email protected]> | 2024-09-10 00:06:05 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-09-10 00:06:05 -0700 |
| commit | fcbc26f544a5ad7ec841561dec695aaceb63d709 (patch) | |
| tree | c94210ebbdec428cac3cd7148ae2fb5929474d12 /src | |
| parent | refactor(reader): chapter list component (diff) | |
| download | due.moe-fcbc26f544a5ad7ec841561dec695aaceb63d709.tar.xz due.moe-fcbc26f544a5ad7ec841561dec695aaceb63d709.zip | |
feat(reader): add rawkuma chapter support
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/Data/Manga/raw.ts | 26 | ||||
| -rw-r--r-- | src/lib/Reader/Chapters/MangaDex.svelte (renamed from src/lib/Reader/Chapters.svelte) | 2 | ||||
| -rw-r--r-- | src/lib/Reader/Chapters/Rawkuma.svelte | 22 | ||||
| -rw-r--r-- | src/lib/Reader/resource.ts | 40 | ||||
| -rw-r--r-- | src/routes/reader/+page.svelte | 28 |
5 files changed, 104 insertions, 14 deletions
diff --git a/src/lib/Data/Manga/raw.ts b/src/lib/Data/Manga/raw.ts index c570474d..0d7e928c 100644 --- a/src/lib/Data/Manga/raw.ts +++ b/src/lib/Data/Manga/raw.ts @@ -1,5 +1,11 @@ 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(), @@ -19,3 +25,23 @@ export const getChapterCount = async (nativeTitle: string): Promise<number | und 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; +}; diff --git a/src/lib/Reader/Chapters.svelte b/src/lib/Reader/Chapters/MangaDex.svelte index 9953036e..12eca713 100644 --- a/src/lib/Reader/Chapters.svelte +++ b/src/lib/Reader/Chapters/MangaDex.svelte @@ -1,5 +1,5 @@ <script lang="ts"> - export let data; + export let data: any; </script> <ul> diff --git a/src/lib/Reader/Chapters/Rawkuma.svelte b/src/lib/Reader/Chapters/Rawkuma.svelte new file mode 100644 index 00000000..5a7e57ad --- /dev/null +++ b/src/lib/Reader/Chapters/Rawkuma.svelte @@ -0,0 +1,22 @@ +<script lang="ts"> + import { getChaptersFromText } from '$lib/Data/Manga/raw'; + import { onMount } from 'svelte'; + + export let data: string; + + onMount(() => { + console.log(); + }); +</script> + +<ul> + {#each getChaptersFromText(data) as chapter} + <li> + {chapter.chapterDate} + <span class="opaque">|</span> + <a href={chapter.href} target="_blank" rel="noopener noreferrer"> + {chapter.chapterNum} + </a> + </li> + {/each} +</ul> diff --git a/src/lib/Reader/resource.ts b/src/lib/Reader/resource.ts new file mode 100644 index 00000000..b96a890f --- /dev/null +++ b/src/lib/Reader/resource.ts @@ -0,0 +1,40 @@ +export enum Resource { + MangaDex = 'MangaDex', + Rawkuma = 'Rawkuma' +} + +export const identify = (url: string): Resource | undefined => { + if (url.match(/mangadex\.org\/title\/([a-f0-9-]+)\/?/)?.[1]) { + return Resource.MangaDex; + } else if (url.match(/rawkuma\.com\/manga\/([a-z0-9-]+)\/?/)?.[1]) { + return Resource.Rawkuma; + } + + return undefined; +}; + +export const fetchResource = async (url: string) => { + const resource = identify(url); + + if (resource === Resource.MangaDex) { + return await fetch( + `https://api.mangadex.org/manga/${ + url.match(/mangadex\.org\/title\/([a-f0-9-]+)\/?/)?.[1] + }/feed?order[chapter]=desc&translatedLanguage[]=en` + ); + } else if (resource === Resource.Rawkuma) { + return await fetch(url); + } + + return fetch(url); +}; + +export const decodeResource = async (response: Response, url: string) => { + const resource = identify(url); + + if (resource === Resource.MangaDex) { + return await response.json(); + } else { + return await response.text(); + } +}; diff --git a/src/routes/reader/+page.svelte b/src/routes/reader/+page.svelte index 05e47274..98c3ae1c 100644 --- a/src/routes/reader/+page.svelte +++ b/src/routes/reader/+page.svelte @@ -1,34 +1,36 @@ <script> import Notice from '$lib/Error/Notice.svelte'; import Message from '$lib/Loading/Message.svelte'; - import Chapters from '$lib/Reader/Chapters.svelte'; + import MangaDexChapters from '$lib/Reader/Chapters/MangaDex.svelte'; + import RawkumaChapters from '$lib/Reader/Chapters/Rawkuma.svelte'; + import { decodeResource, fetchResource, identify, Resource } from '$lib/Reader/resource'; import InputTemplate from '$lib/Tools/InputTemplate.svelte'; let submission = ''; - $: mangaDexID = submission.match(/mangadex\.org\/title\/([a-f0-9-]+)\/?/)?.[1]; + $: resourceIdentity = identify(submission); </script> -<InputTemplate field="MangaDex URL" bind:submission submitText="Read" preserveCase> - {#if mangaDexID} - {#await fetch(`https://api.mangadex.org/manga/${mangaDexID}/feed?order[chapter]=desc&translatedLanguage[]=en`)} +<InputTemplate field="Manga URL" bind:submission submitText="Read" preserveCase> + {#if resourceIdentity} + {#await fetchResource(submission)} <Message message="Loading chapters ..." /> {:then response} {#if response.ok} - {#await response.json() then data} - {#if data.data} - <Chapters {data} /> - {:else} - <p class="opaque">(⌣_⌣”)</p> + {#await decodeResource(response, submission) then data} + {#if resourceIdentity === Resource.MangaDex} + <MangaDexChapters {data} /> + {:else if resourceIdentity === Resource.Rawkuma} + <RawkumaChapters {data} /> {/if} {:catch error} <Notice>{error}</Notice> {/await} {:else} - <Notice>Failed to fetch data from MangaDex</Notice> + <Notice>Failed to fetch data</Notice> {/if} - {:catch error} - {error} + {:catch} + <Notice>An unknown error has occurred.</Notice> {/await} {:else} <Notice>Invalid URL</Notice> |