UNPKG

identify-media

Version:

Analyse file path and content to make search criteria for media APIs

82 lines (70 loc) 2.34 kB
export * from "./omdb"; export * from "./opensubtitles"; export * from "./tmdb"; //todo add more data export interface MediaInfo { plot?: string; images: Record<string, string>; } export interface Movie { type: 'movie'; imdbId?: string; tmdbId?: number; title: string; release?: string;//todo this ought to be some kind of date object... or at least standardized mediaInfo: MediaInfo; //language stuff (original title and language etc.) //reviews: {source: string, value: string} } export const isMovie = (possible: unknown): possible is Movie => { if (!possible) return false; const assumed = possible as Movie; return assumed.type === 'movie' && assumed.title !== undefined && (assumed.tmdbId !== undefined || assumed.imdbId !== undefined); } export interface TVShow { type: 'tv'; imdbId?: string; tmdbId?: number; name: string; firstAirDate?: string;//todo this ought to be some kind of date object... or at least standardized mediaInfo: MediaInfo; //language stuff (original title and language etc.) //reviews: {source: string, value: string} } export const isTVShow = (possible: unknown): possible is TVShow => { if (!possible) return false; const assumed = possible as TVShow; return assumed.type === 'tv' && assumed.name !== undefined && (assumed.tmdbId !== undefined || assumed.imdbId !== undefined); } export type Media = Movie | TVShow; //todo merge the sub objects individually const mergeMediaInfo = (a: MediaInfo, b: MediaInfo): MediaInfo => { return { ...a, ...b, images: {...a.images, ...b.images} } } export const mergeMedia = (a: Media, b: Media): Media => { if (a.type !== b.type) throw new Error('Unable to merge different media types'); if ((!!a.tmdbId && !!b.tmdbId && a.tmdbId !== b.tmdbId) || (!!a.imdbId && !!b.imdbId && a.imdbId !== b.imdbId)) throw new Error('Trying to merge different media'); return { ...a, ...b, mediaInfo: mergeMediaInfo(a.mediaInfo, b.mediaInfo), }; } //axios compatible export interface RequestConfig { baseURL: string, url: string method: 'GET'; params?: Record<string, unknown>, headers?: Record<string, string>, responseType?: 'json' | 'stream'; }