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);
|
||||
}
|
||||
@@ -1,21 +1,26 @@
|
||||
import { about } from '#lib/content';
|
||||
import { getAniListProfile } from '#lib/server/anilist';
|
||||
import { getMediaDetails } from '#lib/server/omdb';
|
||||
import { storedDetailsFor } from '#lib/media/stored';
|
||||
import { getListeningProfile } from '#lib/server/lastfm/profile';
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const mediaUrls = [...about.shows, ...about.movies].map((item) => item.url);
|
||||
const anilistUsername = about.anilistUsername;
|
||||
const storedDetails = storedDetailsFor(mediaUrls);
|
||||
const unsavedUrls = mediaUrls.filter((url) => !storedDetails[url]);
|
||||
|
||||
const [anime, manga, mediaDetails, listening] = await Promise.all([
|
||||
const [anime, manga, fetchedDetails, listening] = await Promise.all([
|
||||
anilistUsername ? getAniListProfile(anilistUsername, 'ANIME') : null,
|
||||
anilistUsername ? getAniListProfile(anilistUsername, 'MANGA') : null,
|
||||
getMediaDetails(mediaUrls),
|
||||
unsavedUrls.length ? getMediaDetails(unsavedUrls) : {},
|
||||
about.lastfmUsername ? getListeningProfile(about.lastfmUsername) : null
|
||||
]);
|
||||
|
||||
const hasManga = !!manga?.statusCounts.some((item) => item.count > 0);
|
||||
|
||||
const mediaDetails = { ...fetchedDetails, ...storedDetails };
|
||||
|
||||
return { anime, manga: hasManga ? manga : null, mediaDetails, listening };
|
||||
};
|
||||
Reference in new issue
Block a user