feat: create routes to load popular titles
This commit is contained in:
157
src/controllers/popular/browse/anilist.ts
Normal file
157
src/controllers/popular/browse/anilist.ts
Normal file
@@ -0,0 +1,157 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
import { getCurrentAndNextSeason } from "~/libs/getCurrentAndNextSeason";
|
||||
import { sleep } from "~/libs/sleep";
|
||||
import { HomeTitleFragment } from "~/types/title/homeTitle";
|
||||
|
||||
import { mapTitle } from "../mapTitle";
|
||||
|
||||
const BrowsePopularQuery = graphql(
|
||||
`
|
||||
query BrowsePopular(
|
||||
$season: MediaSeason!
|
||||
$seasonYear: Int!
|
||||
$nextSeason: MediaSeason!
|
||||
$nextYear: Int!
|
||||
$limit: Int!
|
||||
) {
|
||||
trending: Page(page: 1, perPage: $limit) {
|
||||
media(sort: TRENDING_DESC, type: ANIME, isAdult: false) {
|
||||
...HomeTitle
|
||||
}
|
||||
}
|
||||
season: Page(page: 1, perPage: $limit) {
|
||||
media(
|
||||
season: $season
|
||||
seasonYear: $seasonYear
|
||||
sort: POPULARITY_DESC
|
||||
type: ANIME
|
||||
isAdult: false
|
||||
) {
|
||||
...HomeTitle
|
||||
}
|
||||
}
|
||||
nextSeason: Page(page: 1, perPage: 1) {
|
||||
media(
|
||||
season: $nextSeason
|
||||
seasonYear: $nextYear
|
||||
sort: START_DATE
|
||||
type: ANIME
|
||||
isAdult: false
|
||||
) {
|
||||
nextAiringEpisode {
|
||||
airingAt
|
||||
timeUntilAiring
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
[HomeTitleFragment],
|
||||
);
|
||||
|
||||
const NextSeasonPopularQuery = graphql(`
|
||||
query NextSeasonPopular(
|
||||
$nextSeason: MediaSeason
|
||||
$nextYear: Int
|
||||
$limit: Int!
|
||||
) {
|
||||
Page(page: 1, perPage: $limit) {
|
||||
media(
|
||||
season: $nextSeason
|
||||
seasonYear: $nextYear
|
||||
sort: POPULARITY_DESC
|
||||
type: ANIME
|
||||
isAdult: false
|
||||
) {
|
||||
...media
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fragment media on Media {
|
||||
id
|
||||
title {
|
||||
english
|
||||
userPreferred
|
||||
}
|
||||
coverImage {
|
||||
extraLarge
|
||||
large
|
||||
medium
|
||||
}
|
||||
}
|
||||
`);
|
||||
|
||||
export async function fetchPopularTitlesFromAnilist(
|
||||
limit: number,
|
||||
): Promise<any> {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
const {
|
||||
current: { season: currentSeason, year: currentYear },
|
||||
next: { season: nextSeason, year: nextYear },
|
||||
} = getCurrentAndNextSeason();
|
||||
|
||||
try {
|
||||
const data = await client.request(BrowsePopularQuery, {
|
||||
limit,
|
||||
season: currentSeason,
|
||||
seasonYear: currentYear,
|
||||
nextSeason,
|
||||
nextYear,
|
||||
});
|
||||
if (!data) return undefined;
|
||||
|
||||
const trendingTitles = data.trending?.media?.map((title) =>
|
||||
mapTitle(title),
|
||||
);
|
||||
const popularSeasonTitles = data.season?.media?.map((title) =>
|
||||
mapTitle(title),
|
||||
);
|
||||
|
||||
if (!data.nextSeason?.media?.[0]?.nextAiringEpisode) {
|
||||
return {
|
||||
trending: trendingTitles,
|
||||
season: popularSeasonTitles,
|
||||
};
|
||||
}
|
||||
|
||||
return await client
|
||||
.request(NextSeasonPopularQuery, {
|
||||
limit,
|
||||
nextSeason,
|
||||
nextYear,
|
||||
})
|
||||
.then((data) => ({
|
||||
trending: trendingTitles,
|
||||
season: popularSeasonTitles,
|
||||
upcoming: data?.Page?.media?.map((title) => mapTitle(title)),
|
||||
}));
|
||||
} catch (error) {
|
||||
const response = error.response;
|
||||
if (response.status === 429) {
|
||||
console.log("429, retrying in", response.headers.get("Retry-After"));
|
||||
return sleep(Number(response.headers.get("Retry-After")!) * 1000).then(
|
||||
() => fetchPopularTitlesFromAnilist(limit),
|
||||
);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
type SearchResultsResponse = {
|
||||
results:
|
||||
| ({
|
||||
id: number;
|
||||
title: { userPreferred: string | null; english: string | null } | null;
|
||||
coverImage: {
|
||||
extraLarge: string | null;
|
||||
large: string | null;
|
||||
medium: string | null;
|
||||
} | null;
|
||||
} | null)[]
|
||||
| null;
|
||||
hasNextPage: boolean | null | undefined;
|
||||
};
|
||||
56
src/controllers/popular/browse/index.ts
Normal file
56
src/controllers/popular/browse/index.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import { ErrorResponse, SuccessResponseSchema } from "~/types/schema";
|
||||
import { HomeTitle } from "~/types/title/homeTitle";
|
||||
|
||||
import { fetchPopularTitlesFromAnilist } from "./anilist";
|
||||
|
||||
const BrowsePopularResponse = SuccessResponseSchema(
|
||||
z.object({
|
||||
trending: z.array(HomeTitle),
|
||||
popular: z.array(HomeTitle),
|
||||
upcoming: z.array(HomeTitle).optional(),
|
||||
}),
|
||||
);
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const route = createRoute({
|
||||
tags: ["aniplay", "title"],
|
||||
operationId: "browsePopularTitles",
|
||||
summary: "Get a preview of popular titles",
|
||||
method: "get",
|
||||
path: "/",
|
||||
request: {
|
||||
query: z.object({
|
||||
limit: z
|
||||
.number({ coerce: true })
|
||||
.int()
|
||||
.default(10)
|
||||
.describe("The number of titles to return"),
|
||||
}),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: BrowsePopularResponse,
|
||||
},
|
||||
},
|
||||
description: "Returns an object containing a preview of popular titles",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const limit = Number(c.req.query("limit") ?? 10);
|
||||
|
||||
const response = await fetchPopularTitlesFromAnilist(limit);
|
||||
if (!response) {
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
|
||||
return c.json({ success: true, result: response });
|
||||
});
|
||||
|
||||
export default app;
|
||||
105
src/controllers/popular/category/anilist.ts
Normal file
105
src/controllers/popular/category/anilist.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
import { getCurrentAndNextSeason } from "~/libs/getCurrentAndNextSeason";
|
||||
import { HomeTitleFragment } from "~/types/title/homeTitle";
|
||||
|
||||
import { mapTitle } from "../mapTitle";
|
||||
import type { PopularCategory } from "./enum";
|
||||
|
||||
const TrendingQuery = graphql(
|
||||
`
|
||||
query Trending($limit: Int!, $page: Int!) {
|
||||
trending: Page(page: $page, perPage: $limit) {
|
||||
media(sort: TRENDING_DESC, type: ANIME, isAdult: false) {
|
||||
...HomeTitle
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
[HomeTitleFragment],
|
||||
);
|
||||
|
||||
const PopularQuery = graphql(
|
||||
`
|
||||
query Popular(
|
||||
$limit: Int!
|
||||
$page: Int!
|
||||
$season: MediaSeason!
|
||||
$seasonYear: Int!
|
||||
) {
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(
|
||||
season: $season
|
||||
seasonYear: $seasonYear
|
||||
sort: POPULARITY_DESC
|
||||
type: ANIME
|
||||
isAdult: false
|
||||
) {
|
||||
...HomeTitle
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
[HomeTitleFragment],
|
||||
);
|
||||
|
||||
const UpcomingQuery = graphql(
|
||||
`
|
||||
query Upcoming(
|
||||
$limit: Int!
|
||||
$page: Int!
|
||||
$nextSeason: MediaSeason!
|
||||
$nextSeasonYear: Int!
|
||||
) {
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(
|
||||
season: $nextSeason
|
||||
seasonYear: $nextYear
|
||||
sort: POPULARITY_DESC
|
||||
type: ANIME
|
||||
isAdult: false
|
||||
) {
|
||||
...HomeTitle
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
[HomeTitleFragment],
|
||||
);
|
||||
|
||||
export function fetchPopularTitlesFromAnilist(
|
||||
category: PopularCategory,
|
||||
page: number,
|
||||
limit: number,
|
||||
) {
|
||||
const client = new GraphQLClient("https://graphql.anilist.co/");
|
||||
|
||||
const { current, next } = getCurrentAndNextSeason();
|
||||
switch (category) {
|
||||
case "trending":
|
||||
return client
|
||||
.request(TrendingQuery, { limit, page })
|
||||
.then((data) => data?.trending?.media?.map((title) => mapTitle(title)));
|
||||
case "popular":
|
||||
return client
|
||||
.request(PopularQuery, {
|
||||
limit,
|
||||
page,
|
||||
season: current.season,
|
||||
seasonYear: current.year,
|
||||
})
|
||||
.then((data) => data?.Page?.media?.map((title) => mapTitle(title)));
|
||||
case "upcoming":
|
||||
return client
|
||||
.request(UpcomingQuery, {
|
||||
limit,
|
||||
page,
|
||||
nextSeason: next.season,
|
||||
nextSeasonYear: next.year,
|
||||
})
|
||||
.then((data) => data?.Page?.media?.map((title) => mapTitle(title)));
|
||||
default:
|
||||
throw new Error(`Unknown category: ${category}`);
|
||||
}
|
||||
}
|
||||
4
src/controllers/popular/category/enum.ts
Normal file
4
src/controllers/popular/category/enum.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export type PopularCategory = z.infer<typeof PopularCategory>;
|
||||
export const PopularCategory = z.enum(["trending", "popular", "upcoming"]);
|
||||
59
src/controllers/popular/category/index.ts
Normal file
59
src/controllers/popular/category/index.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import { ErrorResponse, SuccessResponseSchema } from "~/types/schema";
|
||||
import { HomeTitle } from "~/types/title/homeTitle";
|
||||
|
||||
import { fetchPopularTitlesFromAnilist } from "./anilist";
|
||||
import { PopularCategory } from "./enum";
|
||||
|
||||
const BrowsePopularResponse = SuccessResponseSchema(z.array(HomeTitle));
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const route = createRoute({
|
||||
tags: ["aniplay", "title"],
|
||||
operationId: "browsePopularTitlesWithCategory",
|
||||
summary: "Get a preview of popular titles for a category",
|
||||
method: "get",
|
||||
path: "/{category}",
|
||||
request: {
|
||||
query: z.object({
|
||||
limit: z
|
||||
.number({ coerce: true })
|
||||
.int()
|
||||
.default(10)
|
||||
.describe("The number of titles to return"),
|
||||
page: z.number({ coerce: true }).int().min(1).default(1),
|
||||
}),
|
||||
params: z.object({ category: PopularCategory }),
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: BrowsePopularResponse,
|
||||
},
|
||||
},
|
||||
description: "Returns an object containing a preview of popular titles",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
app.openapi(route, async (c) => {
|
||||
const page = Number(c.req.query("page") ?? 1);
|
||||
const limit = Number(c.req.query("limit") ?? 10);
|
||||
const popularCategory = c.req.param("category") as PopularCategory;
|
||||
|
||||
const response = await fetchPopularTitlesFromAnilist(
|
||||
popularCategory,
|
||||
page,
|
||||
limit,
|
||||
);
|
||||
if (!response) {
|
||||
return c.json(ErrorResponse, { status: 500 });
|
||||
}
|
||||
|
||||
return c.json({ success: true, result: response });
|
||||
});
|
||||
|
||||
export default app;
|
||||
15
src/controllers/popular/index.ts
Normal file
15
src/controllers/popular/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { OpenAPIHono } from "@hono/zod-openapi";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
app.route(
|
||||
"/browse",
|
||||
await import("./browse").then((controller) => controller.default),
|
||||
);
|
||||
|
||||
app.route(
|
||||
"/",
|
||||
await import("./category").then((controller) => controller.default),
|
||||
);
|
||||
|
||||
export default app;
|
||||
12
src/controllers/popular/mapTitle.ts
Normal file
12
src/controllers/popular/mapTitle.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export function mapTitle(
|
||||
media: {
|
||||
title: { english: string | null; userPreferred: string | null } | null;
|
||||
} | null,
|
||||
) {
|
||||
if (!media) return null;
|
||||
|
||||
return {
|
||||
...media,
|
||||
title: media?.title?.userPreferred ?? media?.title?.english,
|
||||
};
|
||||
}
|
||||
@@ -2,28 +2,27 @@ import { graphql } from "gql.tada";
|
||||
import { GraphQLClient } from "graphql-request";
|
||||
|
||||
import { sleep } from "~/libs/sleep";
|
||||
import { HomeTitleFragment } from "~/types/title/homeTitle";
|
||||
|
||||
const SearchQuery = graphql(`
|
||||
query Search($query: String!, $page: Int!, $limit: Int!) {
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(search: $query, type: ANIME, sort: [POPULARITY_DESC, SCORE_DESC]) {
|
||||
id
|
||||
title {
|
||||
userPreferred
|
||||
english
|
||||
const SearchQuery = graphql(
|
||||
`
|
||||
query Search($query: String!, $page: Int!, $limit: Int!) {
|
||||
Page(page: $page, perPage: $limit) {
|
||||
media(
|
||||
search: $query
|
||||
type: ANIME
|
||||
sort: [POPULARITY_DESC, SCORE_DESC]
|
||||
) {
|
||||
...HomeTitle
|
||||
}
|
||||
coverImage {
|
||||
extraLarge
|
||||
large
|
||||
medium
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
}
|
||||
}
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
}
|
||||
}
|
||||
}
|
||||
`);
|
||||
`,
|
||||
[HomeTitleFragment],
|
||||
);
|
||||
|
||||
export async function fetchSearchResultsFromAnilist(
|
||||
query: string,
|
||||
|
||||
@@ -2,9 +2,9 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
||||
|
||||
import { fetchFromMultipleSources } from "~/libs/fetchFromMultipleSources";
|
||||
import { PaginatedResponseSchema } from "~/types/schema";
|
||||
import { HomeTitle } from "~/types/title/homeTitle";
|
||||
|
||||
import { fetchSearchResultsFromAnilist } from "./anilist";
|
||||
import { SearchResult } from "./searchResult";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
@@ -25,7 +25,7 @@ const route = createRoute({
|
||||
200: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PaginatedResponseSchema(SearchResult),
|
||||
schema: PaginatedResponseSchema(HomeTitle),
|
||||
},
|
||||
},
|
||||
description: "Returns a list of paginated results for the query",
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { z } from "@hono/zod-openapi";
|
||||
|
||||
export type SearchResult = z.infer<typeof SearchResult>;
|
||||
export const SearchResult = z.object({
|
||||
id: z.number().openapi({ type: "integer", format: "int64" }),
|
||||
title: z.nullable(z.string()),
|
||||
coverImage: z.nullable(
|
||||
z.object({
|
||||
medium: z.nullable(z.string()).optional(),
|
||||
large: z.nullable(z.string()).optional(),
|
||||
extraLarge: z.nullable(z.string()).optional(),
|
||||
}),
|
||||
),
|
||||
});
|
||||
Reference in New Issue
Block a user