aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-01-02 15:21:59 -0800
committerFuwn <[email protected]>2024-01-02 15:21:59 -0800
commitdb17b25a759bde3ebca2b282d1bc45cd51a78840 (patch)
tree38125c3839269afa52e618c01c6e71b9bc67dc1b /src/lib
parentchore: remote debug logs (diff)
downloaddue.moe-db17b25a759bde3ebca2b282d1bc45cd51a78840.tar.xz
due.moe-db17b25a759bde3ebca2b282d1bc45cd51a78840.zip
feat(tools): random follower finder
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/AniList/following.ts56
-rw-r--r--src/lib/Error/RateLimited.svelte59
-rw-r--r--src/lib/Tools/RandomFollower.svelte56
3 files changed, 152 insertions, 19 deletions
diff --git a/src/lib/AniList/following.ts b/src/lib/AniList/following.ts
new file mode 100644
index 00000000..1bba66b9
--- /dev/null
+++ b/src/lib/AniList/following.ts
@@ -0,0 +1,56 @@
+import { user, type User } from './user';
+
+export interface FollowingPage {
+ data: {
+ Page: {
+ pageInfo: {
+ hasNextPage: boolean;
+ };
+ following: Partial<User>[];
+ };
+ };
+}
+
+const followingPage = async (page: number, id: number): Promise<FollowingPage> =>
+ await (
+ await fetch('https://graphql.anilist.co', {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ },
+ body: JSON.stringify({
+ query: `{
+ Page(page: ${page}) {
+ pageInfo { hasNextPage }
+ following(userId: ${id}) { name id }
+ }
+ }`
+ })
+ })
+ ).json();
+
+export const followers = async (name: string): Promise<Partial<User>[]> => {
+ const activities = [];
+ let page = 1;
+ const id = (await user(name)).id;
+ let currentPage = await followingPage(page, id);
+
+ for (const activity of currentPage.data.Page.following) activities.push(activity);
+
+ while (currentPage['data']['Page']['pageInfo']['hasNextPage']) {
+ for (const activity of currentPage.data.Page.following) activities.push(activity);
+
+ page += 1;
+ currentPage = await followingPage(page, id);
+ }
+
+ for (const activity of currentPage.data.Page.following) activities.push(activity);
+
+ for (let i = activities.length - 1; i > 0; i--) {
+ const j = Math.floor(Math.random() * (i + 1));
+ [activities[i], activities[j]] = [activities[j], activities[i]];
+ }
+
+ return activities;
+};
diff --git a/src/lib/Error/RateLimited.svelte b/src/lib/Error/RateLimited.svelte
index 59de182d..ce5afdba 100644
--- a/src/lib/Error/RateLimited.svelte
+++ b/src/lib/Error/RateLimited.svelte
@@ -2,26 +2,47 @@
export let type = 'Media';
export let loginSessionError = true;
export let contact = true;
+ export let list = true;
</script>
-<ul>
- <li>
- <p>
- {type} could not be loaded. You might have been
- <a href="https://en.wikipedia.org/wiki/Rate_limiting" target="_blank">rate limited</a>.
- </p>
- {#if loginSessionError}
- <p>
- Your login session may have expired. Try logging out and logging back in, or try again in a
- few minutes.
- </p>
- {/if}
- <slot />
- {#if contact}
+{#if list}
+ <ul>
+ <li>
<p>
- If the problem persists, please contact
- <a href="https://anilist.co/user/fuwn" target="_blank">@fuwn</a> on AniList.
+ {type} could not be loaded. You might have been
+ <a href="https://en.wikipedia.org/wiki/Rate_limiting" target="_blank">rate limited</a>.
</p>
- {/if}
- </li>
-</ul>
+ {#if loginSessionError}
+ <p>
+ Your login session may have expired. Try logging out and logging back in, or try again in
+ a few minutes.
+ </p>
+ {/if}
+ <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>
+{:else}
+ <p>
+ {type} could not be loaded. You might have been
+ <a href="https://en.wikipedia.org/wiki/Rate_limiting" target="_blank">rate limited</a>.
+ </p>
+ {#if loginSessionError}
+ <p>
+ Your login session may have expired. Try logging out and logging back in, or try again in a
+ few minutes.
+ </p>
+ {/if}
+ <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}
+{/if}
diff --git a/src/lib/Tools/RandomFollower.svelte b/src/lib/Tools/RandomFollower.svelte
new file mode 100644
index 00000000..e52d4e86
--- /dev/null
+++ b/src/lib/Tools/RandomFollower.svelte
@@ -0,0 +1,56 @@
+<script lang="ts">
+ import { followers } from '$lib/AniList/following';
+ import RateLimited from '$lib/Error/RateLimited.svelte';
+ import { clearAllParameters } from '$lib/Utility/parameters';
+ import { onMount } from 'svelte';
+
+ let input = '';
+ let submit = '';
+ let randomSeed = 0;
+
+ onMount(clearAllParameters);
+</script>
+
+<p>
+ <!-- svelte-ignore missing-declaration -->
+ <input
+ type="text"
+ placeholder="Username"
+ bind:value={input}
+ on:keypress={(e) => {
+ if (e.key === 'Enter') {
+ submit = input;
+ randomSeed = Math.random();
+
+ // eslint-disable-next-line no-undef
+ umami.track('Random Follower');
+ }
+ }}
+ />
+ <a
+ href={`#`}
+ on:click={() => (submit = input) && (randomSeed = Math.random())}
+ title="Or click your Enter key"
+ data-umami-event="Random Follower"
+ >
+ Generate
+ </a>
+</p>
+
+{#if submit !== ''}
+ {#await followers(submit)}
+ Loading followers ... 50%
+ {:then users}
+ {@const user = users[Math.floor(randomSeed * users.length)]}
+
+ <p>
+ <a href={`https://anilist.co/user/${user.id}`} target="_blank">
+ {user.name}
+ </a>
+ </p>
+ {:catch}
+ <RateLimited type="Followers" list={false} />
+ {/await}
+{:else}
+ <p>Enter a username to search for to continue.</p>
+{/if}