refactor: move non-public routes to internal subfolder

"non-public" in this case means only used for event handling (and not supposed to be called by clients)
This commit is contained in:
2024-09-08 14:35:00 -05:00
parent ff5e81f5d1
commit d4a5a4fbb1
5 changed files with 16 additions and 10 deletions

View File

@@ -0,0 +1,89 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import { DateTime } from "luxon";
import { getValue, setValue } from "~/models/kv";
import type { Env } from "~/types/env";
import type { Title } from "~/types/title";
import { MediaFragment } from "~/types/title/mediaFragment";
const GetUpcomingTitlesQuery = graphql(
`
query GetUpcomingTitles(
$page: Int!
$airingAtLowerBound: Int!
$airingAtUpperBound: Int!
) {
Page(page: $page) {
airingSchedules(
notYetAired: true
sort: TIME
airingAt_lesser: $airingAtUpperBound
airingAt_greater: $airingAtLowerBound
) {
id
airingAt
timeUntilAiring
episode
media {
...Media
}
}
pageInfo {
hasNextPage
}
}
}
`,
[MediaFragment],
);
type AiringSchedule = {
media: Title;
episode: number;
timeUntilAiring: number;
airingAt: number;
id: number;
};
export async function getUpcomingTitlesFromAnilist(env: Env) {
const client = new GraphQLClient("https://graphql.anilist.co/");
const lastCheckedScheduleAt = await getValue(
env,
"schedule_last_checked_at",
).then((value) => (value ? Number(value) : DateTime.now().toUnixInteger()));
const twoDaysFromNow = DateTime.now().plus({ days: 2 }).toUnixInteger();
let currentPage = 1;
let scheduleList: AiringSchedule[] = [];
let shouldContinue = true;
do {
const { Page } = await client.request(GetUpcomingTitlesQuery, {
page: currentPage++,
airingAtLowerBound: lastCheckedScheduleAt,
airingAtUpperBound: twoDaysFromNow,
});
const { airingSchedules, pageInfo } = Page!;
scheduleList = scheduleList.concat(
airingSchedules!.filter(
(schedule): schedule is AiringSchedule =>
!!schedule && schedule.media?.countryOfOrigin === "JP",
),
);
shouldContinue = pageInfo?.hasNextPage ?? false;
} while (shouldContinue);
if (scheduleList.length === 0) {
return [];
}
await setValue(
env,
"schedule_last_checked_at",
scheduleList[scheduleList.length - 1].airingAt.toString(),
);
return scheduleList;
}