Save OMDb details and posters locally
This commit is contained in:
1 parent
1f7e88f117
commit
4f78883f0b
6 files changed
+239
-7
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import mediaDetailsData from '#data/mediaDetails.json';
|
||||
import type { MediaDetails } from './types';
|
||||
|
||||
interface StoredMediaDetails extends MediaDetails {
|
||||
title?: string | null;
|
||||
posterSource?: string | null;
|
||||
}
|
||||
|
||||
const imdbIdPattern = /tt\d+/;
|
||||
const storedDetails = mediaDetailsData as Record<string, StoredMediaDetails>;
|
||||
|
||||
function toMediaDetails(stored: StoredMediaDetails): MediaDetails {
|
||||
return {
|
||||
poster: stored.poster ?? null,
|
||||
year: stored.year ?? null,
|
||||
genre: stored.genre ?? null,
|
||||
runtime: stored.runtime ?? null,
|
||||
plot: stored.plot ?? null
|
||||
};
|
||||
}
|
||||
|
||||
export function storedDetailsFor(urls: string[]): Record<string, MediaDetails> {
|
||||
const entries = urls.flatMap((url) => {
|
||||
const stored = storedDetails[url.match(imdbIdPattern)?.[0] ?? ''];
|
||||
return stored ? [[url, toMediaDetails(stored)] as const] : [];
|
||||
});
|
||||
return Object.fromEntries(entries);
|
||||
}
|
||||
Reference in new issue
Block a user