aboutsummaryrefslogtreecommitdiff
path: root/src/lib/List
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-08-26 22:29:03 -0700
committerFuwn <[email protected]>2023-08-26 22:29:03 -0700
commitb89d0e7dada186e31be37e62a7a75efc2dbe9c99 (patch)
tree8c9f6b5d7aa0f709c06d5eb45fbf763883b21c89 /src/lib/List
downloaddue.moe-b89d0e7dada186e31be37e62a7a75efc2dbe9c99.tar.xz
due.moe-b89d0e7dada186e31be37e62a7a75efc2dbe9c99.zip
feat: initial build
Diffstat (limited to 'src/lib/List')
-rw-r--r--src/lib/List/Due/AnimeList.svelte135
-rw-r--r--src/lib/List/Due/MangaList.svelte114
2 files changed, 249 insertions, 0 deletions
diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte
new file mode 100644
index 00000000..dd19bada
--- /dev/null
+++ b/src/lib/List/Due/AnimeList.svelte
@@ -0,0 +1,135 @@
+<script lang="ts">
+ import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media';
+ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity';
+ import { onMount } from 'svelte';
+
+ export let user: AniListAuthorisation;
+ export let identity: UserIdentity;
+ export let displayUnresolved: boolean;
+
+ let animeLists: any;
+ let startTime: number;
+ let endTime: number;
+
+ onMount(async () => {
+ animeLists = mediaListCollection(user, identity, Type.Anime);
+ });
+
+ const cleanMedia = (media: object[][], displayUnresolved: boolean) => {
+ startTime = performance.now();
+
+ const flattenedLists = flattenLists(media);
+ const releasingMedia = flattenedLists.filter((media: Media) => media['status'] == 'RELEASING');
+ const outdatedMedia = releasingMedia.filter((media: Media) => {
+ return (
+ (media['nextAiringEpisode'] || { episode: 0 })['episode'] - 1 !=
+ (media['mediaListEntry'] || { progress: 0 })['progress']
+ );
+ });
+ let finalMedia = outdatedMedia.map((media: Media) => {
+ if ((media['nextAiringEpisode'] || { episode: 0 })['episode'] - 1 <= 0) {
+ media['nextAiringEpisode'] = { episode: -1 };
+ }
+
+ return media;
+ });
+
+ if (!displayUnresolved) {
+ finalMedia = finalMedia.filter((media: Media) => media.nextAiringEpisode?.episode !== -1);
+ }
+
+ finalMedia.sort((a: Media, b: Media) => {
+ const difference = (anime: Media) => {
+ return (
+ (anime.nextAiringEpisode?.episode === -1
+ ? 99999
+ : anime.nextAiringEpisode?.episode || -1) -
+ (anime['mediaListEntry'] || { progress: 0 })['progress']
+ );
+ };
+
+ return difference(a) - difference(b);
+ });
+
+ finalMedia = finalMedia.filter((item, index, array) => {
+ return (
+ array.findIndex((i) => {
+ return i.id === item.id;
+ }) === index
+ );
+ });
+ endTime = performance.now() - startTime;
+
+ return finalMedia;
+ };
+
+ const airingTime = (anime: Media) => {
+ const untilAiring = anime.nextAiringEpisode?.timeUntilAiring;
+ let timeFrame;
+
+ if (untilAiring !== undefined) {
+ let hours = untilAiring / 3600;
+
+ if (hours >= 24) {
+ let weeks = Math.floor(Math.floor(hours / 24) / 7);
+
+ if (weeks >= 1) {
+ weeks = Math.round(weeks);
+
+ timeFrame = `${weeks} week${weeks === 1 ? '' : 's'}`;
+ } else {
+ const days = Math.round(Math.floor(hours / 24));
+
+ timeFrame = `${days} day${days === 1 ? '' : 's'}`;
+ }
+ } else {
+ hours = Math.round(hours);
+
+ timeFrame = `${hours} hour${hours === 1 ? '' : 's'}`;
+ }
+
+ return `<span style="opacity: 50%">${anime.nextAiringEpisode?.episode} in ${timeFrame}</span>`;
+ }
+
+ return '';
+ };
+
+ const totalEpisodes = (anime: Media) => {
+ return anime['episodes'] === null
+ ? ''
+ : `<span style="opacity: 50%">/${anime['episodes']}</span>`;
+ };
+
+ const updateMedia = async (id: number, progress: number | undefined) => {
+ animeLists = mediaListCollection(user, identity, Type.Anime);
+
+ await fetch(`/anilist/increment?id=${id}&progress=${progress! + 1}`);
+ };
+</script>
+
+{#await animeLists}
+ <summary>Anime</summary>
+
+ <ul><li>Loading ...</li></ul>
+{:then media}
+ <summary
+ >Anime [{cleanMedia(media, displayUnresolved).length}]
+ <small style="opacity: 50%">{endTime / 1000}s</small></summary
+ >
+
+ <ul>
+ {#each cleanMedia(media, displayUnresolved) as anime}
+ <li>
+ <a href={`https://anilist.co/anime/${anime['id']}`} target="_blank">
+ {anime['title']['english'] || anime['title']['romaji']}
+ </a>
+ {(anime['mediaListEntry'] || { progress: 0 })['progress']}{@html totalEpisodes(anime)}
+ <a href="#" on:click={() => updateMedia(anime.id, anime.mediaListEntry?.progress)}>+</a>
+ [{anime.nextAiringEpisode?.episode === -1
+ ? '?'
+ : (anime.nextAiringEpisode?.episode || 1) - 1}]
+ {@html airingTime(anime)}
+ </li>
+ {/each}
+ </ul>
+{/await}
diff --git a/src/lib/List/Due/MangaList.svelte b/src/lib/List/Due/MangaList.svelte
new file mode 100644
index 00000000..3016ed57
--- /dev/null
+++ b/src/lib/List/Due/MangaList.svelte
@@ -0,0 +1,114 @@
+<script lang="ts">
+ import { mediaListCollection, Type, flattenLists, type Media } from '$lib/AniList/media';
+ import type { UserIdentity, AniListAuthorisation } from '$lib/AniList/identity';
+ import { onMount } from 'svelte';
+ import { chapterCount } from '$lib/mangadex';
+ import lastPruneAt from '../../../stores/lastPruneAt';
+ import cacheMangaMinutes from '../../../stores/cacheMangaMinutes';
+
+ export let user: AniListAuthorisation;
+ export let identity: UserIdentity;
+ export let displayUnresolved: boolean;
+
+ let mangaLists: any;
+ let startTime: number;
+ let endTime: number;
+
+ onMount(async () => {
+ mangaLists = mediaListCollection(user, identity, Type.Manga);
+ });
+
+ const cleanMedia = async (media: object[][], displayUnresolved: boolean) => {
+ startTime = performance.now();
+
+ if ($lastPruneAt == '') {
+ lastPruneAt.set(new Date().getTime().toString());
+ } else {
+ if ((new Date().getTime() - Number($lastPruneAt)) / 1000 / 60 > Number($cacheMangaMinutes)) {
+ lastPruneAt.set(new Date().getTime().toString());
+ }
+ }
+
+ const flattenedLists = flattenLists(media);
+ const releasingMedia = flattenedLists.filter(
+ (media: Media) =>
+ media['status'] == 'RELEASING' &&
+ media['format'] != 'NOVEL' &&
+ (media['mediaListEntry'] || { progress: 0 })['progress'] >= 1
+ );
+ let finalMedia = releasingMedia;
+
+ const chapterPromises = finalMedia.map((m: Media) => chapterCount(m));
+ const chapterCounts = await Promise.all(chapterPromises);
+
+ finalMedia.forEach((m: Media, i) => {
+ m.episodes = chapterCounts[i];
+ });
+
+ if (!displayUnresolved) {
+ finalMedia = finalMedia.filter((m: Media) => m.episodes !== null);
+ }
+
+ finalMedia.sort((a: Media, b: Media) => {
+ return (
+ (a.episodes || 9999) -
+ a.mediaListEntry!.progress -
+ ((b.episodes || 9999) - b.mediaListEntry!.progress)
+ );
+ });
+
+ finalMedia = finalMedia.filter((item, index, array) => {
+ return (
+ array.findIndex((i) => {
+ return i.id === item.id;
+ }) === index &&
+ (item.episodes === null && displayUnresolved
+ ? true
+ : (item.mediaListEntry?.progress || 0) < item.episodes)
+ );
+ });
+ endTime = performance.now() - startTime;
+
+ return finalMedia;
+ };
+
+ const updateMedia = async (id: number, progress: number | undefined) => {
+ mangaLists = mediaListCollection(user, identity, Type.Manga);
+
+ await fetch(`/anilist/increment?id=${id}&progress=${progress! + 1}`);
+ };
+</script>
+
+{#await mangaLists}
+ <summary>Manga</summary>
+
+ <ul><li>Loading ...</li></ul>
+{:then media}
+ <summary>
+ Manga {#await cleanMedia(media, displayUnresolved) then count}[{count.length}]{/await}
+ <small style="opacity: 50%">{endTime / 1000}s</small>
+ </summary>
+
+ <ul>
+ {#await cleanMedia(media, displayUnresolved)}
+ <li>Loading ...</li>
+ {:then cleanedMedia}
+ {#each cleanedMedia as manga}
+ <li>
+ <a href={`https://anilist.co/manga/${manga['id']}`} target="_blank">
+ {manga['title']['english'] || manga['title']['romaji'] || manga['title']['native']}
+ </a>
+ {(manga['mediaListEntry'] || { progress: 0 })['progress']}
+ <a href="#" on:click={() => updateMedia(manga.id, manga.mediaListEntry?.progress)}>+</a>
+ [{manga['episodes'] || '?'}]
+ </li>
+ {/each}
+ {:catch}
+ <li>
+ Media could not be loaded. You might have been <a
+ href="https://en.wikipedia.org/wiki/Rate_limiting">rate limited</a
+ >.
+ </li>
+ {/await}
+ </ul>
+{/await}