feat: store unreleased titles where first episode time is unknown
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
import type { HonoRequest } from "hono";
|
||||
import { DateTime } from "luxon";
|
||||
|
||||
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
||||
import { getValue, setValue } from "~/models/kv";
|
||||
import { filterUnreleasedTitles } from "~/models/unreleasedTitles";
|
||||
import type { Env } from "~/types/env";
|
||||
import type { Title } from "~/types/title";
|
||||
import { MediaFragment } from "~/types/title/mediaFragment";
|
||||
@@ -46,7 +49,7 @@ type AiringSchedule = {
|
||||
id: number;
|
||||
};
|
||||
|
||||
export async function getUpcomingTitlesFromAnilist(env: Env) {
|
||||
export async function getUpcomingTitlesFromAnilist(env: Env, req: HonoRequest) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
const lastCheckedScheduleAt = await getValue(
|
||||
env,
|
||||
@@ -55,6 +58,7 @@ export async function getUpcomingTitlesFromAnilist(env: Env) {
|
||||
const twoDaysFromNow = DateTime.now().plus({ days: 2 }).toUnixInteger();
|
||||
|
||||
let currentPage = 1;
|
||||
let plannedToWatchTitles = new Set<number>();
|
||||
let scheduleList: AiringSchedule[] = [];
|
||||
let shouldContinue = true;
|
||||
|
||||
@@ -66,10 +70,17 @@ export async function getUpcomingTitlesFromAnilist(env: Env) {
|
||||
});
|
||||
|
||||
const { airingSchedules, pageInfo } = Page!;
|
||||
plannedToWatchTitles = plannedToWatchTitles.union(
|
||||
await filterUnreleasedTitles(
|
||||
env,
|
||||
airingSchedules!.map((schedule) => schedule!.media?.id!),
|
||||
),
|
||||
);
|
||||
scheduleList = scheduleList.concat(
|
||||
airingSchedules!.filter(
|
||||
(schedule): schedule is AiringSchedule =>
|
||||
!!schedule &&
|
||||
!plannedToWatchTitles.has(schedule.media?.id) &&
|
||||
schedule.media?.countryOfOrigin === "JP" &&
|
||||
schedule.episode == 1,
|
||||
),
|
||||
@@ -77,6 +88,12 @@ export async function getUpcomingTitlesFromAnilist(env: Env) {
|
||||
shouldContinue = pageInfo?.hasNextPage ?? false;
|
||||
} while (shouldContinue);
|
||||
|
||||
await Promise.allSettled(
|
||||
Array.from(plannedToWatchTitles).map((titleId) =>
|
||||
maybeScheduleNextAiringEpisode(env, req, titleId),
|
||||
),
|
||||
);
|
||||
|
||||
if (scheduleList.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user