This commit is contained in:
castdrian 2023-06-13 14:25:31 +02:00
parent a7af045308
commit b22e3ff8c1
3 changed files with 3 additions and 102 deletions

View File

@ -3,8 +3,10 @@ import {
JWMediaResult, JWMediaResult,
JWSeasonMetaResult, JWSeasonMetaResult,
JW_IMAGE_BASE, JW_IMAGE_BASE,
MWMediaMeta,
MWMediaType,
MWSeasonMeta,
} from "./types"; } from "./types";
import { MWMediaMeta, MWMediaType, MWSeasonMeta } from "./types_old";
export function mediaTypeToJW(type: MWMediaType): JWContentTypes { export function mediaTypeToJW(type: MWMediaType): JWContentTypes {
if (type === MWMediaType.MOVIE) return "movie"; if (type === MWMediaType.MOVIE) return "movie";

View File

@ -1,54 +0,0 @@
import { SimpleCache } from "@/utils/cache";
import { formatJWMeta, mediaTypeToJW } from "./justwatch";
import { JWContentTypes, JWMediaResult, JW_API_BASE } from "./types";
import { MWMediaMeta, MWQuery } from "./types_old";
import { proxiedFetch } from "../helpers/fetch";
const cache = new SimpleCache<MWQuery, MWMediaMeta[]>();
cache.setCompare((a, b) => {
return a.type === b.type && a.searchQuery.trim() === b.searchQuery.trim();
});
cache.initialize();
type JWSearchQuery = {
content_types: JWContentTypes[];
page: number;
page_size: number;
query: string;
};
type JWPage<T> = {
items: T[];
page: number;
page_size: number;
total_pages: number;
total_results: number;
};
export async function searchForMedia(query: MWQuery): Promise<MWMediaMeta[]> {
if (cache.has(query)) return cache.get(query) as MWMediaMeta[];
const { searchQuery, type } = query;
const contentType = mediaTypeToJW(type);
const body: JWSearchQuery = {
content_types: [contentType],
page: 1,
query: searchQuery,
page_size: 40,
};
const data = await proxiedFetch<JWPage<JWMediaResult>>(
"/content/titles/en_US/popular",
{
baseURL: JW_API_BASE,
params: {
body: JSON.stringify(body),
},
}
);
const returnData = data.items.map<MWMediaMeta>((v) => formatJWMeta(v));
cache.set(query, returnData, 3600); // cache for an hour
return returnData;
}

View File

@ -1,47 +0,0 @@
export enum MWMediaType {
MOVIE = "movie",
SERIES = "series",
ANIME = "anime",
}
export type MWSeasonMeta = {
id: string;
number: number;
title: string;
};
export type MWSeasonWithEpisodeMeta = {
id: string;
number: number;
title: string;
episodes: {
id: string;
number: number;
title: string;
}[];
};
type MWMediaMetaBase = {
title: string;
id: string;
year?: string;
poster?: string;
};
type MWMediaMetaSpecific =
| {
type: MWMediaType.MOVIE | MWMediaType.ANIME;
seasons: undefined;
}
| {
type: MWMediaType.SERIES;
seasons: MWSeasonMeta[];
seasonData: MWSeasonWithEpisodeMeta;
};
export type MWMediaMeta = MWMediaMetaBase & MWMediaMetaSpecific;
export interface MWQuery {
searchQuery: string;
type: MWMediaType;
}