45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import type { HonoRequest } from "hono";
|
|
import { DateTime } from "luxon";
|
|
|
|
import {
|
|
addUnreleasedTitle,
|
|
removeUnreleasedTitle,
|
|
} from "~/models/unreleasedTitles";
|
|
import type { Env } from "~/types/env";
|
|
|
|
import { getNextEpisodeTimeUntilAiring } from "./anilist/getNextEpisodeAiringAt";
|
|
import { getCurrentDomain } from "./getCurrentDomain";
|
|
import { queueTask } from "./tasks/queueTask";
|
|
|
|
export async function maybeScheduleNextAiringEpisode(
|
|
env: Env,
|
|
req: HonoRequest,
|
|
aniListId: number,
|
|
) {
|
|
const domain = getCurrentDomain(req);
|
|
if (!domain) {
|
|
return;
|
|
}
|
|
|
|
const { nextAiring, status } = await getNextEpisodeTimeUntilAiring(aniListId);
|
|
if (
|
|
!nextAiring ||
|
|
DateTime.fromSeconds(nextAiring.airingAt).diffNow("hours").hours >= 720
|
|
) {
|
|
if (status === "NOT_YET_RELEASED") {
|
|
await addUnreleasedTitle(env, aniListId);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
const { airingAt, episode: nextEpisode } = nextAiring;
|
|
await queueTask(
|
|
env,
|
|
"new-episode",
|
|
{ aniListId, episodeNumber: nextEpisode },
|
|
{ req, scheduleConfig: { epochTime: airingAt } },
|
|
);
|
|
await removeUnreleasedTitle(env, aniListId);
|
|
}
|