feat: create script to initialize "next episode" queue

This commit is contained in:
2024-09-09 05:07:21 -05:00
parent 336701a84b
commit 25ed096b38
6 changed files with 150 additions and 9 deletions

View File

@@ -1,12 +1,11 @@
import { findBestMatchingTitle } from "~/libs/findBestMatchingTitle";
import { Episode, type EpisodesResponse } from "./episode";
import { Episode, type EpisodesResponse } from "~/types/episode";
export async function getEpisodesFromAniwatch(
aniListId: number,
): Promise<EpisodesResponse | null> {
try {
const animeTitle = await import("~/controllers/title/anilist")
const animeTitle = await import("~/libs/anilist/getTitle")
.then(({ fetchTitleFromAnilist }) =>
fetchTitleFromAnilist(aniListId, undefined),
)

View File

@@ -10,6 +10,10 @@ export async function getSourcesFromAniwatch(
)
.then((res) => res.json<AniwatchEpisodeUrlResponse>())
.then(({ intro, outro, sources, tracks }) => {
if (!sources || sources.length === 0) {
return { source: null };
}
return {
intro: convertSkipTime(intro),
outro: convertSkipTime(outro),

View File

@@ -39,10 +39,13 @@ app.post(
aniListId: number;
episodeNumber: number;
}>();
console.log(
`Internal new episode route, aniListId: ${aniListId}, episodeNumber: ${episodeNumber}`,
);
if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
return c.json(ErrorResponse, { status: 401 });
}
// if (!(await verifyQstashHeader(env<Env, typeof c>(c, "workerd"), c.req))) {
// return c.json(ErrorResponse, { status: 401 });
// }
const domain = getCurrentDomain(c.req);
const { success, result: fetchEpisodesResult } = await fetch(

View File

@@ -1,38 +0,0 @@
import { graphql } from "gql.tada";
import { GraphQLClient } from "graphql-request";
import type { Title } from "~/types/title";
import { MediaFragment } from "~/types/title/mediaFragment";
const GetTitleQuery = graphql(
`
query GetTitle($id: Int!) {
Media(id: $id) {
...Media
}
}
`,
[MediaFragment],
);
export async function fetchTitleFromAnilist(
id: number,
token: string | undefined,
): Promise<Title | undefined> {
const client = new GraphQLClient("https://graphql.anilist.co/");
const headers = new Headers();
if (token) {
headers.append("Authorization", `Bearer ${token}`);
}
return client
.request(GetTitleQuery, { id }, headers)
.then((data) => data?.Media ?? undefined)
.catch((error) => {
if (error.message.includes("Not Found")) {
return undefined;
}
throw error;
});
}

View File

@@ -1,5 +1,6 @@
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { fetchTitleFromAnilist } from "~/libs/anilist/getTitle";
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
import {
AniListIdQuerySchema,
@@ -9,8 +10,6 @@ import {
} from "~/types/schema";
import { Title } from "~/types/title";
import { fetchTitleFromAnilist } from "./anilist";
const app = new OpenAPIHono();
const route = createRoute({