From 4792d6c5fd628e9a455ef461d89b268a7ef114b3 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Tue, 26 Dec 2023 23:44:41 -0800 Subject: feat(manga): distributed proxy --- src/lib/List/Manga/MangaListTemplate.svelte | 139 +++++++++++++--------------- src/lib/Media/Manga/chapters.ts | 99 +++++++++++++++++--- src/lib/settings.json | 3 - 3 files changed, 150 insertions(+), 91 deletions(-) delete mode 100644 src/lib/settings.json (limited to 'src/lib') diff --git a/src/lib/List/Manga/MangaListTemplate.svelte b/src/lib/List/Manga/MangaListTemplate.svelte index 256a4900..4e92acd8 100644 --- a/src/lib/List/Manga/MangaListTemplate.svelte +++ b/src/lib/List/Manga/MangaListTemplate.svelte @@ -12,7 +12,6 @@ import Error from '$lib/Error/RateLimited.svelte'; import CleanMangaList from './CleanMangaList.svelte'; import authorisedJson from '$lib/authorised.json'; - import serviceSettings from '$lib/settings.json'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -168,14 +167,42 @@ }; -{#if serviceSettings.disabled} - +{#await mangaLists} + {#if !authorised} + + { + cleanCache(); + + forceFlag = true; + }}>Refresh + + {/if} + + {#if previousMangaList} + + {:else} + {#if authorised} + + {/if} -
    -
  • Manga is currently unavailable due to API abuse. Check back later.
  • -
-{:else} - {#await mangaLists} +
  • Loading {progress.toFixed(0)}% ...
+ {/if} +{:then media} + {#await cleanMedia(media, displayUnresolved, forceFlag)} {#if !authorised}
  • Loading {progress.toFixed(0)}% ...
  • {/if} - {:then media} - {#await cleanMedia(media, displayUnresolved, forceFlag)} - {#if !authorised} - -
    { - cleanCache(); - - forceFlag = true; - }}>Refresh -
    - {/if} + {:then cleanedMedia} + {#if !authorised} + + { + cleanCache(); - {#if previousMangaList} - - {:else} - {#if authorised} - - {/if} - -
    • Loading {progress.toFixed(0)}% ...
    - {/if} - {:then cleanedMedia} - {#if !authorised} - -
    { - cleanCache(); - - forceFlag = true; - }}>Refresh -
    - {/if} + forceFlag = true; + }}>Refresh +
    + {/if} - - {:catch} - {#if authorised} - - {/if} + + {:catch} + {#if authorised} + + {/if} - - {/await} + {/await} -{/if} +{/await} diff --git a/src/lib/Media/Manga/chapters.ts b/src/lib/Media/Manga/chapters.ts index 2e3c6678..36942ce4 100644 --- a/src/lib/Media/Manga/chapters.ts +++ b/src/lib/Media/Manga/chapters.ts @@ -3,6 +3,75 @@ import settings from '../../../stores/settings'; import type { UserIdentity } from '../../AniList/identity'; import { database } from '../../Database/chapters'; +const getManga = async ( + statusIn: string, + year: number, + native: string | null, + english: string | null, + romaji: string | null +) => { + let status = ''; + let error = false; + + switch (statusIn) { + case 'FINISHED': + { + status = 'completed'; + } + break; + case 'RELEASING': + { + status = 'ongoing'; + } + break; + case 'HIATUS': + { + status = 'hiatus'; + } + break; + case 'CANCELLED': + { + status = 'cancelled'; + } + break; + } + + const nullIfNullString = (s: string | null) => (s == 'null' ? null : s); + const get = async (title: string) => { + try { + return await ( + await fetch( + `https://due-proxy.fuwn.workers.dev/?q=https://api.mangadex.org/manga?title=${encodeURIComponent( + title + )}&year=${year}&status[]=${status}` + ) + ).json(); + } catch { + error = true; + } + }; + + let mangadexData = await get( + nullIfNullString(native) || nullIfNullString(english) || nullIfNullString(romaji) || '' + ); + + if (error) return new Response('rate-limited'); + + if (mangadexData['data'] === undefined || mangadexData['data'].length === 0) { + mangadexData = await get(nullIfNullString(english) || ''); + + if (mangadexData['data'] === undefined || mangadexData['data'].length === 0) { + mangadexData = await get(nullIfNullString(romaji) || ''); + } + } + + return Response.json(mangadexData, { + headers: { + 'Cache-Control': 'max-age=300' + } + }); +}; + export const chapterCount = async ( identity: UserIdentity, manga: Media, @@ -37,8 +106,12 @@ export const chapterCount = async ( if (manga.format === 'NOVEL') return await tryRecentMediaActivities(); - const mangadexData = await fetch( - `/api/mangadex/manga?english=${manga.title.english}&year=${manga.startDate.year}&romaji=${manga.title.romaji}&native=${manga.title.native}&status=${manga.status}` + const mangadexData = await getManga( + manga.status, + manga.startDate.year, + manga.title.native, + manga.title.english, + manga.title.romaji ); if ((await mangadexData.clone().text()) === 'rate-limited') return -22; @@ -49,11 +122,11 @@ export const chapterCount = async ( return await tryRecentMediaActivities(); const mangadexId = mangadexDataJson['data'][0]['id']; - const lastChapterData = await fetch(`/api/mangadex/feed?id=${mangadexId}`); - - if ((await lastChapterData.clone().text()) === 'rate-limited') return -22; - - const lastChapterDataJson = await lastChapterData.json(); + const lastChapterDataJson = await ( + await fetch( + `https://due-proxy.fuwn.workers.dev/?q=https://api.mangadex.org/manga/${mangadexId}/feed?order[chapter]=desc&translatedLanguage[]=en&limit=1&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic` + ) + ).json(); if (lastChapterDataJson['data'] === undefined || lastChapterDataJson['data'].length === 0) return await tryRecentMediaActivities(); @@ -72,13 +145,11 @@ export const chapterCount = async ( } if (!settings.get().calculateDisableOutOfDateVolumeWarning) { - const volumeOfChapterDataResponse = await fetch( - `/api/mangadex/chapter?id=${mangadexId}&chapter=${manga.mediaListEntry?.progress}` - ); - - if ((await volumeOfChapterDataResponse.clone().text()) === 'rate-limited') return -22; - - const volumeOfChapterData = await volumeOfChapterDataResponse.json(); + const volumeOfChapterData = await ( + await fetch( + `https://due-proxy.fuwn.workers.dev/?q=https://api.mangadex.org/chapter?manga=${mangadexId}&chapter=${manga.mediaListEntry?.progress}&contentRating[]=safe&contentRating[]=suggestive&contentRating[]=erotica&contentRating[]=pornographic&limit=1` + ) + ).json(); let lastAvailableVolume = lastChapterDataJson['data'][0]['attributes']['volume']; if (lastAvailableVolume === null) { diff --git a/src/lib/settings.json b/src/lib/settings.json deleted file mode 100644 index 5d318736..00000000 --- a/src/lib/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "disabled": true -} -- cgit v1.2.3