diff options
| author | Fuwn <[email protected]> | 2023-12-13 18:52:33 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-12-13 18:52:33 -0800 |
| commit | 9d762096e17bb5ac125dc5980adcae0dac8a17ad (patch) | |
| tree | a460e76091195eeb3dfc7e798a49451794632a95 /src/lib | |
| parent | fix(manga): long-standing prune filter error (diff) | |
| download | due.moe-9d762096e17bb5ac125dc5980adcae0dac8a17ad.tar.xz due.moe-9d762096e17bb5ac125dc5980adcae0dac8a17ad.zip | |
style(AniList): simplify
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/AniList/activity.ts | 6 | ||||
| -rw-r--r-- | src/lib/AniList/character.ts | 6 | ||||
| -rw-r--r-- | src/lib/AniList/forum.ts | 8 | ||||
| -rw-r--r-- | src/lib/AniList/identity.ts | 2 | ||||
| -rw-r--r-- | src/lib/AniList/media.ts | 76 | ||||
| -rw-r--r-- | src/lib/AniList/prequels.ts | 14 | ||||
| -rw-r--r-- | src/lib/AniList/user.ts | 7 | ||||
| -rw-r--r-- | src/lib/AniList/wrapped.ts | 1 |
8 files changed, 42 insertions, 78 deletions
diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts index df150c96..2c008582 100644 --- a/src/lib/AniList/activity.ts +++ b/src/lib/AniList/activity.ts @@ -21,20 +21,18 @@ export const fillMissingDays = ( const current_unix_timestamp = currentDate.getTime(); let found = false; - for (let i = 0; i < activities.length; i++) { + for (let i = 0; i < activities.length; i++) if (activities[i].date * 1000 + timezoneOffset === current_unix_timestamp) { found = true; break; } - } - if (!found) { + if (!found) activities.push({ date: current_unix_timestamp / 1000, amount: 0 }); - } currentDate.setDate(currentDate.getDate() + 1); } diff --git a/src/lib/AniList/character.ts b/src/lib/AniList/character.ts index 1a5cb3c4..534c27b0 100644 --- a/src/lib/AniList/character.ts +++ b/src/lib/AniList/character.ts @@ -42,7 +42,7 @@ export const todaysCharacterBirthdays = async (): Promise<Character[]> => { let page = 1; let currentPage = await charactersPage(page); - for (const character of currentPage['data']['Page']['characters']) { + for (const character of currentPage['data']['Page']['characters']) characters.push({ id: character['id'], name: { @@ -52,10 +52,9 @@ export const todaysCharacterBirthdays = async (): Promise<Character[]> => { large: character['image']['large'] } }); - } while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { - for (const character of currentPage['data']['Page']['characters']) { + for (const character of currentPage['data']['Page']['characters']) characters.push({ id: character['id'], name: { @@ -65,7 +64,6 @@ export const todaysCharacterBirthdays = async (): Promise<Character[]> => { large: character['image']['large'] } }); - } page += 1; currentPage = await charactersPage(page); diff --git a/src/lib/AniList/forum.ts b/src/lib/AniList/forum.ts index 6b95fa07..5085f3d8 100644 --- a/src/lib/AniList/forum.ts +++ b/src/lib/AniList/forum.ts @@ -41,17 +41,13 @@ export const threads = async (username: string): Promise<Thread[]> => { let page = 1; let currentPage = await threadPage(page, userId); - for (const thread of currentPage.data.Page.threads) { - allThreads.push(thread); - } + for (const thread of currentPage.data.Page.threads) allThreads.push(thread); while (currentPage.data.Page.pageInfo.hasNextPage) { page += 1; currentPage = await threadPage(page, userId); - for (const thread of currentPage.data.Page.threads) { - allThreads.push(thread); - } + for (const thread of currentPage.data.Page.threads) allThreads.push(thread); } return allThreads; diff --git a/src/lib/AniList/identity.ts b/src/lib/AniList/identity.ts index 5844bd66..a77891d6 100644 --- a/src/lib/AniList/identity.ts +++ b/src/lib/AniList/identity.ts @@ -25,8 +25,6 @@ export const userIdentity = async ( }) ).json(); - console.log(userIdResponse['data']['Viewer']['name']); - return { id: userIdResponse['data']['Viewer']['id'], name: userIdResponse['data']['Viewer']['name'] diff --git a/src/lib/AniList/media.ts b/src/lib/AniList/media.ts index 067db97a..6c0d02c6 100644 --- a/src/lib/AniList/media.ts +++ b/src/lib/AniList/media.ts @@ -62,20 +62,15 @@ export interface Media { } export const flattenLists = (lists: { name: string; entries: { media: Media }[] }[]) => { - if (lists === undefined) { - return []; - } + if (lists === undefined) return []; const flattenedList: Media[] = []; const ignoredMediaIds: number[] = []; - for (const list of lists) { - if (list.name.toLowerCase().includes('#dueignore')) { + for (const list of lists) + if (list.name.toLowerCase().includes('#dueignore')) ignoredMediaIds.push(...list.entries.map((entry) => entry.media.id)); - } else { - flattenedList.push(...list.entries.map((entry) => entry.media)); - } - } + else flattenedList.push(...list.entries.map((entry) => entry.media)); return flattenedList .filter((media) => !ignoredMediaIds.includes(media.id)) @@ -120,16 +115,11 @@ export const mediaListCollection = async ( ): Promise<Media[]> => { let currentCacheMinutes; - settings.subscribe((value) => { - currentCacheMinutes = value.cacheMinutes; - }); + settings.subscribe((value) => (currentCacheMinutes = value.cacheMinutes)); if (String(currentLastPruneAt) === '') { - if (type === Type.Anime) { - lastPruneTimes.setKey('anime', new Date().getTime()); - } else { - lastPruneTimes.setKey('manga', new Date().getTime()); - } + if (type === Type.Anime) lastPruneTimes.setKey('anime', new Date().getTime()); + else lastPruneTimes.setKey('manga', new Date().getTime()); } else { if ( (new Date().getTime() - Number(currentLastPruneAt)) / 1000 / 60 > @@ -148,9 +138,7 @@ export const mediaListCollection = async ( } } - if (mediaCache !== undefined && mediaCache !== '') { - return JSON.parse(mediaCache); - } + if (mediaCache !== undefined && mediaCache !== '') return JSON.parse(mediaCache); const userIdResponse = await ( await fetch('https://graphql.anilist.co', { @@ -166,37 +154,36 @@ export const mediaListCollection = async ( }) ).json(); - if (mediaCache === '') { - if (type === Type.Anime) { + if (mediaCache === '') + if (type === Type.Anime) anime.set( JSON.stringify(flattenLists(userIdResponse['data']['MediaListCollection']['lists'])) ); - } else { + else manga.set( JSON.stringify(flattenLists(userIdResponse['data']['MediaListCollection']['lists'])) ); - } - } return flattenLists(userIdResponse['data']['MediaListCollection']['lists']); }; -export const publicMediaListCollection = async (userId: number, type: Type): Promise<Media[]> => { - const userIdResponse = await ( - await fetch('https://graphql.anilist.co', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Accept: 'application/json' - }, - body: JSON.stringify({ - query: collectionQueryTemplate(type, userId, false) - }) - }) - ).json(); - - return flattenLists(userIdResponse['data']['MediaListCollection']['lists']); -}; +export const publicMediaListCollection = async (userId: number, type: Type): Promise<Media[]> => + flattenLists( + ( + await ( + await fetch('https://graphql.anilist.co', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Accept: 'application/json' + }, + body: JSON.stringify({ + query: collectionQueryTemplate(type, userId, false) + }) + }) + ).json() + )['data']['MediaListCollection']['lists'] + ); const countMedian = (guesses: number[]) => { guesses.sort((a: number, b: number) => a - b); @@ -264,9 +251,7 @@ export const recentMediaActivities = async ( if (activity.progress !== null) { const progress = Number((activity.progress.match(/\d+$/) || [0])[0]); - if (progress !== 65535) { - guesses.push(progress); - } + if (progress !== 65535) guesses.push(progress); } }); guesses.sort((a, b) => b - a); @@ -330,9 +315,8 @@ export const recentMediaActivities = async ( // } // if (activities['data']['MediaList']['progress'] !== null) { - if (activities['data']['MediaList']['progress'] > bestGuess) { + if (activities['data']['MediaList']['progress'] > bestGuess) bestGuess = activities['data']['MediaList']['progress']; - } // } return Math.round(bestGuess); diff --git a/src/lib/AniList/prequels.ts b/src/lib/AniList/prequels.ts index 68a153ba..726ad66e 100644 --- a/src/lib/AniList/prequels.ts +++ b/src/lib/AniList/prequels.ts @@ -95,14 +95,10 @@ export const prequels = async ( let page = 1; let currentPage = await prequelsPage(page, anilistAuthorisation, year, season); - for (const candidate of currentPage.data.Page.media) { - candidates.push(candidate); - } + for (const candidate of currentPage.data.Page.media) candidates.push(candidate); while (currentPage['data']['Page']['pageInfo']['hasNextPage']) { - for (const candidate of currentPage.data.Page.media) { - candidates.push(candidate); - } + for (const candidate of currentPage.data.Page.media) candidates.push(candidate); page += 1; currentPage = await prequelsPage(page, anilistAuthorisation, year, season); @@ -122,23 +118,21 @@ export const prequels = async ( ) { episodes += relation.node.episodes; - if (relation.node.mediaListEntry !== null) { + if (relation.node.mediaListEntry !== null) seen += relation.node.mediaListEntry.progress || 0; - } } } } if (media.some((m) => m.id === candidate.id)) continue; - if (episodes !== 0) { + if (episodes !== 0) media.push({ id: candidate.id, title: candidate.title, episodes, seen }); - } } return media; diff --git a/src/lib/AniList/user.ts b/src/lib/AniList/user.ts index fdc98a58..adc00c56 100644 --- a/src/lib/AniList/user.ts +++ b/src/lib/AniList/user.ts @@ -20,8 +20,8 @@ export interface User { }; } -export const user = async (username: string): Promise<User> => { - const userData = ( +export const user = async (username: string): Promise<User> => + ( await ( await fetch('https://graphql.anilist.co', { method: 'POST', @@ -44,6 +44,3 @@ export const user = async (username: string): Promise<User> => { }) ).json() )['data']['User']; - - return userData; -}; diff --git a/src/lib/AniList/wrapped.ts b/src/lib/AniList/wrapped.ts index 8a3b3f57..d3c67fa2 100644 --- a/src/lib/AniList/wrapped.ts +++ b/src/lib/AniList/wrapped.ts @@ -57,7 +57,6 @@ const profileActivities = async (user: AniListAuthorisation, identity: UserIdent }) }) ).json(); - const pages = []; let page = 1; let response = await get(page); |