aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorFuwn <[email protected]>2023-12-31 21:09:47 -0800
committerFuwn <[email protected]>2023-12-31 21:09:47 -0800
commit4b454dabd0ede98dcb75cc80cf88c0a06d678923 (patch)
treec41f862e311053184e02a94be092f4af6051142a /src/lib
parentfix(wrapped): overflow on mobile (diff)
downloaddue.moe-4b454dabd0ede98dcb75cc80cf88c0a06d678923.tar.xz
due.moe-4b454dabd0ede98dcb75cc80cf88c0a06d678923.zip
feat(wrapped): select year
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/AniList/activity.ts3
-rw-r--r--src/lib/AniList/wrapped.ts23
-rw-r--r--src/lib/Error/RateLimited.svelte12
-rw-r--r--src/lib/Tools/Wrapped.svelte33
4 files changed, 51 insertions, 20 deletions
diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts
index e1547084..6c8deca7 100644
--- a/src/lib/AniList/activity.ts
+++ b/src/lib/AniList/activity.ts
@@ -195,7 +195,8 @@ export const fullActivityHistory = async (
fullLocalActivityHistory = fullLocalActivityHistory.filter((a) => !isNaN(a.date));
- fullLocalActivityHistory.push(...(await activityHistory(userIdentity)));
+ if (new Date().getMonth() > 6)
+ fullLocalActivityHistory.push(...(await activityHistory(userIdentity)));
fullLocalActivityHistory = fullLocalActivityHistory.filter(
(activityHistoryEntry, index, self) =>
diff --git a/src/lib/AniList/wrapped.ts b/src/lib/AniList/wrapped.ts
index 4bee8bbb..00ae0acc 100644
--- a/src/lib/AniList/wrapped.ts
+++ b/src/lib/AniList/wrapped.ts
@@ -45,7 +45,11 @@ export interface Wrapped {
};
}
-const profileActivities = async (user: AniListAuthorisation, identity: UserIdentity) => {
+const profileActivities = async (
+ user: AniListAuthorisation,
+ identity: UserIdentity,
+ now = Date.now()
+) => {
const get = async (page: number) =>
await (
await fetch('https://graphql.anilist.co', {
@@ -79,6 +83,7 @@ const profileActivities = async (user: AniListAuthorisation, identity: UserIdent
const pages = [];
let page = 1;
let response = await get(page);
+ const yearInSeconds = 31556952;
pages.push(response['data']['Page']['activities']);
@@ -94,21 +99,25 @@ const profileActivities = async (user: AniListAuthorisation, identity: UserIdent
.flat()
.filter(
(activity) =>
- activity.type == 'TEXT' && activity.createdAt > Math.floor(Date.now() / 1000) - 31556952
+ activity.type == 'TEXT' &&
+ activity.createdAt > Math.floor(now / 1000) - yearInSeconds &&
+ activity.createdAt < Math.floor(now / 1000)
).length,
messageCount: pages
.flat()
.filter(
(activity) =>
activity.type == 'MESSAGE' &&
- activity.createdAt > Math.floor(Date.now() / 1000) - 31556952
+ activity.createdAt > Math.floor(now / 1000) - yearInSeconds &&
+ activity.createdAt < Math.floor(now / 1000)
).length
};
};
export const wrapped = async (
anilistAuthorisation: AniListAuthorisation,
- identity: UserIdentity
+ identity: UserIdentity,
+ year = new Date().getFullYear()
): Promise<Wrapped> => {
const wrappedResponse = await (
await fetch('https://graphql.anilist.co', {
@@ -139,7 +148,11 @@ export const wrapped = async (
})
})
).json();
- const { statusCount, messageCount } = await profileActivities(anilistAuthorisation, identity);
+ const { statusCount, messageCount } = await profileActivities(
+ anilistAuthorisation,
+ identity,
+ new Date(year, 11, 31).getTime()
+ );
return {
statistics: wrappedResponse['data']['User']['statistics'],
diff --git a/src/lib/Error/RateLimited.svelte b/src/lib/Error/RateLimited.svelte
index f00239dd..59de182d 100644
--- a/src/lib/Error/RateLimited.svelte
+++ b/src/lib/Error/RateLimited.svelte
@@ -1,6 +1,7 @@
<script lang="ts">
export let type = 'Media';
export let loginSessionError = true;
+ export let contact = true;
</script>
<ul>
@@ -15,9 +16,12 @@
few minutes.
</p>
{/if}
- <p>
- If the problem persists, please contact
- <a href="https://anilist.co/user/fuwn" target="_blank">@fuwn</a> on AniList.
- </p>
+ <slot />
+ {#if contact}
+ <p>
+ If the problem persists, please contact
+ <a href="https://anilist.co/user/fuwn" target="_blank">@fuwn</a> on AniList.
+ </p>
+ {/if}
</li>
</ul>
diff --git a/src/lib/Tools/Wrapped.svelte b/src/lib/Tools/Wrapped.svelte
index 312e5a01..87c97ba8 100644
--- a/src/lib/Tools/Wrapped.svelte
+++ b/src/lib/Tools/Wrapped.svelte
@@ -29,7 +29,7 @@
export let user: AniListAuthorisation;
- const currentYear = new Date(Date.now()).getFullYear();
+ let currentYear = new Date(Date.now()).getFullYear();
let currentUserIdentity = { name: '', id: -1 };
let episodes = 0;
let chapters = 0;
@@ -104,6 +104,7 @@
mediaSort = mediaSort;
includeMovies = includeMovies;
includeOVAs = includeOVAs;
+ currentYear = currentYear;
update().then(updateWidth).catch(updateWidth);
}
@@ -481,10 +482,10 @@
{#if currentUserIdentity.id === -2}
Please log in to view this page.
{:else if currentUserIdentity.id !== -1}
- {#await useFullActivityHistory ? fullActivityHistory(user, currentUserIdentity, currentYear) : getActivityHistory(currentUserIdentity)}
+ {#await currentYear !== new Date(Date.now()).getFullYear() || useFullActivityHistory ? fullActivityHistory(user, currentUserIdentity, currentYear) : getActivityHistory(currentUserIdentity)}
{@html nbsp(`Loading${useFullActivityHistory ? ' full-year' : ''} activity history ...`)}
{:then activities}
- {#await wrapped(user, currentUserIdentity)}
+ {#await wrapped(user, currentUserIdentity, currentYear)}
{@html nbsp('Loading user data ...')}
{:then wrapped}
<div id="list-container">
@@ -776,14 +777,18 @@
<summary>Calculation</summary>
<input type="checkbox" bind:checked={useFullActivityHistory} />
- Enable full-year activity<br />
+ Enable full-year activity
<SettingHint>
- If you have many activities, you may rate-limited and you may need multiple
- attempts to fully populate your local activity history database.<br />If you get
- rate-limited, wait one minute, then try again.
<a href={'#'} on:click={pruneFullYear}>Refresh data</a>
</SettingHint><br />
- <p />
+ <select bind:value={currentYear}>
+ {#each Array.from({ length: new Date(Date.now()).getFullYear() - 2012 }) as _, i}
+ <option value={new Date(Date.now()).getFullYear() - i}>
+ {new Date(Date.now()).getFullYear() - i}
+ </option>
+ {/each}
+ </select>
+ Calculate for year<br />
<select bind:value={mediaSort}>
<option value={SortOptions.SCORE}>Score</option>
<option value={SortOptions.MINUTES_WATCHED}>Minutes Watched/Read</option>
@@ -832,13 +837,21 @@
</div>
</div>
{:catch}
- <Error />
+ <Error type="User" />
{/await}
{:catch}
<Error
type={`${useFullActivityHistory ? 'Full-year activity' : 'Activity'} history`}
loginSessionError={!useFullActivityHistory}
- />
+ >
+ {#if useFullActivityHistory}
+ <p>
+ With <b>many</b> activities, it may take multiple attempts to obtain all of your activity history
+ from AniList. If this occurs, wait one minute and try again to continue populating your local
+ activity history database.
+ </p>
+ {/if}
+ </Error>
{/await}
{:else}
{@html nbsp('Loading user identity ...')}