aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-10 02:19:28 -0800
committerFuwn <[email protected]>2023-12-10 02:34:52 -0800
commite8bf266e5f487f7c444baf16bbabf1f4e4c2bdad (patch)
tree7dff6a98a33392670e8b0085d7ea98904d1420a2 /src/lib
parentfeat(page): use video always (diff)
downloaddue.moe-e8bf266e5f487f7c444baf16bbabf1f4e4c2bdad.tar.xz
due.moe-e8bf266e5f487f7c444baf16bbabf1f4e4c2bdad.zip
feat(mangadex): add safe rate limits
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/List/Manga/CleanMangaList.svelte6
-rw-r--r--src/lib/List/Manga/MangaListTemplate.svelte14
-rw-r--r--src/lib/Media/manga.ts50
3 files changed, 48 insertions, 22 deletions
diff --git a/src/lib/List/Manga/CleanMangaList.svelte b/src/lib/List/Manga/CleanMangaList.svelte
index 8f36f244..40082809 100644
--- a/src/lib/List/Manga/CleanMangaList.svelte
+++ b/src/lib/List/Manga/CleanMangaList.svelte
@@ -1,5 +1,6 @@
<script lang="ts">
import type { Media } from '$lib/AniList/media';
+ import Error from '$lib/Error.svelte';
import { volumeCount } from '$lib/Media/manga';
import { outboundLink } from '$lib/Media/media';
import settings from '../../../stores/settings';
@@ -17,12 +18,17 @@
) => Promise<void>;
export let pendingUpdate: number | null;
export let due: boolean;
+ export let rateLimited: boolean;
</script>
<ListTitle count={media.length} time={endTime / 1000}>
<a href={'#'} title="Force a full refresh" on:click={cleanCache}>Refresh</a>
</ListTitle>
+{#if rateLimited}
+ <Error />
+{/if}
+
{#if media.length === 0}
<ul>
<li>No manga to display. <a href={'#'} on:click={cleanCache}>Force refresh</a></li>
diff --git a/src/lib/List/Manga/MangaListTemplate.svelte b/src/lib/List/Manga/MangaListTemplate.svelte
index 370cfd72..c6ea8b7d 100644
--- a/src/lib/List/Manga/MangaListTemplate.svelte
+++ b/src/lib/List/Manga/MangaListTemplate.svelte
@@ -23,6 +23,7 @@
let previousMangaList: Media[];
let pendingUpdate: number | null = null;
let progress = 0;
+ let rateLimited = false;
const keyCacher = setInterval(() => {
startTime = performance.now();
@@ -84,7 +85,15 @@
const chapterCounts: (number | null)[] = [];
for (let i = 0; i < chapterPromises.length; i++) {
- chapterCounts.push(await chapterPromises[i]);
+ const count = await chapterPromises[i];
+
+ if (count === -22) {
+ rateLimited = true;
+
+ break;
+ }
+
+ chapterCounts.push(count);
progress += progressStep;
}
@@ -168,6 +177,7 @@
{updateMedia}
{pendingUpdate}
{due}
+ {rateLimited}
/>
{:else}
<ListTitle />
@@ -185,6 +195,7 @@
{updateMedia}
{pendingUpdate}
{due}
+ {rateLimited}
/>
{:else}
<ListTitle />
@@ -200,6 +211,7 @@
{updateMedia}
{pendingUpdate}
{due}
+ {rateLimited}
/>
{:catch}
<ListTitle count={-1337} time={0} />
diff --git a/src/lib/Media/manga.ts b/src/lib/Media/manga.ts
index af08d4eb..b8afbc96 100644
--- a/src/lib/Media/manga.ts
+++ b/src/lib/Media/manga.ts
@@ -51,24 +51,28 @@ export const chapterCount = async (
return await tryRecentMediaActivities();
}
- const mangadexData = await (
- await fetch(
- `/api/mangadex/manga?english=${manga.title.english}&year=${manga.startDate.year}&romaji=${manga.title.romaji}&native=${manga.title.native}&status=${manga.status}`
- )
- ).json();
+ 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}`
+ );
- if (mangadexData['data'] === undefined || mangadexData['data'].length === 0) {
+ if ((await mangadexData.clone().text()) === 'rate-limited') return -22;
+
+ const mangadexDataJson = await mangadexData.json();
+
+ if (mangadexDataJson['data'] === undefined || mangadexDataJson['data'].length === 0)
return await tryRecentMediaActivities();
- }
- const mangadexId = mangadexData['data'][0]['id'];
- const lastChapterData = await (await fetch(`/api/mangadex/feed?id=${mangadexId}`)).json();
+ 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();
- if (lastChapterData['data'] === undefined || lastChapterData['data'].length === 0) {
+ if (lastChapterDataJson['data'] === undefined || lastChapterDataJson['data'].length === 0)
return await tryRecentMediaActivities();
- }
- let lastChapter = lastChapterData['data'][0]['attributes']['chapter'];
+ let lastChapter = lastChapterDataJson['data'][0]['attributes']['chapter'];
let completedVolumes = null;
if ((manga.mediaListEntry || { progress: 0 }).progress > lastChapter) {
@@ -80,19 +84,23 @@ export const chapterCount = async (
}
if (!settings.get().disableOutOfDateVolumeWarning) {
- const volumeOfChapterData = await (
- await fetch(
- `/api/mangadex/chapter?id=${mangadexId}&chapter=${manga.mediaListEntry?.progress}`
- )
- ).json();
- let lastAvailableVolume = lastChapterData['data'][0]['attributes']['volume'];
+ 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();
+ let lastAvailableVolume = lastChapterDataJson['data'][0]['attributes']['volume'];
if (lastAvailableVolume === null) {
let chapterIndex = 0;
- while (chapterIndex < lastChapterData['data'].length && lastAvailableVolume === null) {
- if (lastChapterData['data'][chapterIndex]['attributes']['volume'] !== null) {
- lastAvailableVolume = lastChapterData['data'][chapterIndex]['attributes']['volume'];
+ while (chapterIndex < lastChapterDataJson['data'].length && lastAvailableVolume === null) {
+ if (lastChapterDataJson['data'][chapterIndex]['attributes']['volume'] !== null) {
+ lastAvailableVolume = lastChapterDataJson['data'][chapterIndex]['attributes']['volume'];
}
chapterIndex += 1;