Compare commits
5 Commits
291d2744af
...
9da626f17b
| Author | SHA1 | Date | |
|---|---|---|---|
| 9da626f17b | |||
| 3fab9cacbc | |||
| f9ca949d8e | |||
| 37b4f0bf2b | |||
| d66903400d |
@@ -66,6 +66,7 @@ pnpm run deploy
|
||||
|
||||
- `src/controllers`: API route handlers (titles, episodes, search, etc.)
|
||||
- `src/libs`: Shared utilities and logic (AniList integration, background tasks)
|
||||
- `src/middleware`: Middleware handlers (authentication, authorization, etc.)
|
||||
- `src/models`: Database schema and models
|
||||
- `src/scripts`: Utility scripts for maintenance and setup
|
||||
- `src/types`: TypeScript type definitions
|
||||
|
||||
@@ -30,7 +30,7 @@ export async function fetchPopularTitlesFromAnilist(
|
||||
);
|
||||
break;
|
||||
case "upcoming":
|
||||
data = await stub.nextSeasonPopular(next.season, next.year, limit);
|
||||
data = await stub.nextSeasonPopular(next.season, next.year, page, limit);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown category: ${category}`);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
import type { HonoRequest } from "hono";
|
||||
|
||||
import { AnilistUpdateType } from "~/libs/anilist/updateType.ts";
|
||||
import { maybeScheduleNextAiringEpisode } from "~/libs/maybeScheduleNextAiringEpisode";
|
||||
@@ -22,7 +21,6 @@ const UpdateWatchStatusRequest = z.object({
|
||||
deviceId: z.string(),
|
||||
watchStatus: WatchStatus.nullable(),
|
||||
titleId: AniListIdSchema,
|
||||
isRetrying: z.boolean().optional().default(false),
|
||||
});
|
||||
|
||||
const route = createRoute({
|
||||
@@ -81,12 +79,8 @@ export async function updateWatchStatus(
|
||||
}
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const {
|
||||
deviceId,
|
||||
watchStatus,
|
||||
titleId,
|
||||
isRetrying = false,
|
||||
} = await c.req.json<typeof UpdateWatchStatusRequest._type>();
|
||||
const { deviceId, watchStatus, titleId } =
|
||||
await c.req.json<typeof UpdateWatchStatusRequest._type>();
|
||||
// Check if we should use mock data
|
||||
const { useMockData } = await import("~/libs/useMockData");
|
||||
if (useMockData()) {
|
||||
@@ -94,7 +88,6 @@ app.openapi(route, async (c) => {
|
||||
return c.json(SuccessResponse, { status: 200 });
|
||||
}
|
||||
|
||||
if (!isRetrying) {
|
||||
try {
|
||||
await updateWatchStatus(deviceId, titleId, watchStatus);
|
||||
} catch (error) {
|
||||
@@ -102,7 +95,6 @@ app.openapi(route, async (c) => {
|
||||
console.error(error);
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
const aniListToken = c.req.header("X-AniList-Token");
|
||||
if (aniListToken) {
|
||||
|
||||
@@ -82,6 +82,7 @@ export default {
|
||||
case "ANILIST_UPDATES":
|
||||
const anilistUpdateBody =
|
||||
message.body as QueueBody["ANILIST_UPDATES"];
|
||||
console.log("queue run", message.body);
|
||||
switch (anilistUpdateBody.updateType) {
|
||||
case AnilistUpdateType.UpdateWatchStatus:
|
||||
if (!anilistUpdateBody[AnilistUpdateType.UpdateWatchStatus]) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
|
||||
import { DurableObject } from "cloudflare:workers";
|
||||
import { print } from "graphql";
|
||||
import { DateTime } from "luxon";
|
||||
import { z } from "zod";
|
||||
|
||||
import {
|
||||
@@ -99,7 +100,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
});
|
||||
return data?.Media;
|
||||
},
|
||||
60 * 60 * 1000,
|
||||
DateTime.now().plus({ hours: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
});
|
||||
return data?.Page;
|
||||
},
|
||||
60 * 60 * 1000,
|
||||
DateTime.now().plus({ hours: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -127,23 +128,28 @@ export class AnilistDurableObject extends DurableObject {
|
||||
) {
|
||||
return this.handleCachedRequest(
|
||||
`popular:${JSON.stringify({ season, seasonYear, nextSeason, nextYear, limit })}`,
|
||||
async () => {
|
||||
console.log(nextSeason, nextYear, print(BrowsePopularQuery));
|
||||
() => {
|
||||
return this.fetchFromAnilist(BrowsePopularQuery, {
|
||||
season,
|
||||
seasonYear,
|
||||
nextSeason,
|
||||
nextYear,
|
||||
limit,
|
||||
});
|
||||
page,
|
||||
}).then((data) => data?.Page);
|
||||
},
|
||||
24 * 60 * 60 * 1000,
|
||||
DateTime.now().plus({ days: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
async nextSeasonPopular(nextSeason: any, nextYear: number, limit: number) {
|
||||
async nextSeasonPopular(
|
||||
nextSeason: any,
|
||||
nextYear: number,
|
||||
page: number,
|
||||
limit: number,
|
||||
) {
|
||||
return this.handleCachedRequest(
|
||||
`next_season:${JSON.stringify({ nextSeason, nextYear, limit })}`,
|
||||
`next_season:${JSON.stringify({ nextSeason, nextYear, page, limit })}`,
|
||||
async () => {
|
||||
return this.fetchFromAnilist(NextSeasonPopularQuery, {
|
||||
nextSeason,
|
||||
@@ -151,7 +157,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
limit,
|
||||
});
|
||||
},
|
||||
24 * 60 * 60 * 1000,
|
||||
DateTime.now().plus({ days: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -164,15 +170,14 @@ export class AnilistDurableObject extends DurableObject {
|
||||
return this.handleCachedRequest(
|
||||
`popular:${JSON.stringify({ page, limit, season, seasonYear })}`,
|
||||
async () => {
|
||||
const data = await this.fetchFromAnilist(GetPopularTitlesQuery, {
|
||||
return this.fetchFromAnilist(GetPopularTitlesQuery, {
|
||||
page,
|
||||
limit,
|
||||
season,
|
||||
seasonYear,
|
||||
});
|
||||
return data?.Page;
|
||||
}).then((data) => data?.Page);
|
||||
},
|
||||
24 * 60 * 60 * 1000,
|
||||
DateTime.now().plus({ days: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -186,7 +191,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
});
|
||||
return data?.Page;
|
||||
},
|
||||
24 * 60 * 60 * 1000,
|
||||
DateTime.now().plus({ days: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -205,7 +210,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
});
|
||||
return data?.Page;
|
||||
},
|
||||
24 * 60 * 60 * 1000,
|
||||
DateTime.now().plus({ days: 1 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -213,10 +218,10 @@ export class AnilistDurableObject extends DurableObject {
|
||||
return this.handleCachedRequest(
|
||||
`user:${token}`,
|
||||
async () => {
|
||||
const data = await this.fetchFromAnilist(GetUserQuery, {}, token);
|
||||
const data = await this.fetchFromAnilist(GetUserQuery, {}, { token });
|
||||
return data?.Viewer;
|
||||
},
|
||||
60 * 60 * 24 * 30 * 1000,
|
||||
DateTime.now().plus({ days: 30 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -227,11 +232,11 @@ export class AnilistDurableObject extends DurableObject {
|
||||
const data = await this.fetchFromAnilist(
|
||||
GetUserProfileQuery,
|
||||
{ token },
|
||||
token,
|
||||
{ token },
|
||||
);
|
||||
return data?.Viewer;
|
||||
},
|
||||
60 * 60 * 24 * 30 * 1000,
|
||||
DateTime.now().plus({ days: 30 }),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -243,7 +248,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
const data = await this.fetchFromAnilist(
|
||||
MarkEpisodeAsWatchedMutation,
|
||||
{ titleId, episodeNumber },
|
||||
token,
|
||||
{ token },
|
||||
);
|
||||
return data?.SaveMediaListEntry;
|
||||
}
|
||||
@@ -252,7 +257,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
const data = await this.fetchFromAnilist(
|
||||
MarkTitleAsWatchedMutation,
|
||||
{ titleId },
|
||||
token,
|
||||
{ token },
|
||||
);
|
||||
return data?.SaveMediaListEntry;
|
||||
}
|
||||
@@ -261,7 +266,7 @@ export class AnilistDurableObject extends DurableObject {
|
||||
async handleCachedRequest<T>(
|
||||
key: string,
|
||||
fetcher: () => Promise<T>,
|
||||
ttl?: number | ((data: T) => number | undefined),
|
||||
ttl?: DateTime | ((data: T) => DateTime | undefined),
|
||||
) {
|
||||
const cache = await this.state.storage.get(key);
|
||||
console.debug(`Retrieving request ${key} from cache:`, cache != null);
|
||||
@@ -271,12 +276,13 @@ export class AnilistDurableObject extends DurableObject {
|
||||
|
||||
const result = await fetcher();
|
||||
await this.state.storage.put(key, result);
|
||||
console.debug(`Retrieved alarms from cache:`, Object.entries(alarms));
|
||||
|
||||
const calculatedTtl = typeof ttl === "function" ? ttl(result) : ttl;
|
||||
|
||||
if (calculatedTtl && calculatedTtl > 0) {
|
||||
const alarmTime = Date.now() + calculatedTtl;
|
||||
if (calculatedTtl) {
|
||||
const alarmTime = calculatedTtl.toMillis();
|
||||
await this.state.storage.setAlarm(alarmTime);
|
||||
console.debug(`Deleting storage key ${storageKey} & alarm ${key}`);
|
||||
await this.state.storage.put(`alarm:${key}`, alarmTime);
|
||||
}
|
||||
|
||||
@@ -300,8 +306,11 @@ export class AnilistDurableObject extends DurableObject {
|
||||
async fetchFromAnilist<Result = any, Variables = any>(
|
||||
query: TypedDocumentNode<Result, Variables>,
|
||||
variables: Variables,
|
||||
token?: string | undefined,
|
||||
): Promise<Result> {
|
||||
{
|
||||
token,
|
||||
shouldRetryOnRateLimit = true,
|
||||
}: { token?: string | undefined; shouldRetryOnRateLimit?: boolean } = {},
|
||||
): Promise<Result | undefined> {
|
||||
const headers: any = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
@@ -331,14 +340,17 @@ export class AnilistDurableObject extends DurableObject {
|
||||
});
|
||||
|
||||
// 1. Handle Rate Limiting (429)
|
||||
if (response.status === 429) {
|
||||
if (shouldRetryOnRateLimit && response.status === 429) {
|
||||
const retryAfter = await response
|
||||
.json()
|
||||
.json<{ headers: Record<string, string> }>()
|
||||
.then(({ headers }) => new Headers(headers).get("Retry-After"));
|
||||
console.log("429, retrying in", retryAfter);
|
||||
|
||||
await sleep(Number(retryAfter || 1) * 1000); // specific fallback or ensure logic
|
||||
return this.fetchFromAnilist(query, variables, token);
|
||||
return this.fetchFromAnilist(query, variables, {
|
||||
token,
|
||||
shouldRetryOnRateLimit: false,
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Handle HTTP Errors (like 404 or 500)
|
||||
|
||||
@@ -260,7 +260,8 @@ export const NextSeasonPopularQuery = graphql(
|
||||
$nextYear: Int
|
||||
$limit: Int!
|
||||
) {
|
||||
Page(page: 1, perPage: $limit) {
|
||||
$page: Int!
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(
|
||||
season: $nextSeason
|
||||
seasonYear: $nextYear
|
||||
|
||||
Reference in New Issue
Block a user