3 changed files with 3 additions and 102 deletions
@ -1,54 +0,0 @@
@@ -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; |
||||
} |
@ -1,47 +0,0 @@
@@ -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; |
||||
} |
Loading…
Reference in new issue