fixed some formatting and added komga integration stuff
This commit is contained in:
@@ -144,18 +144,18 @@ export async function getLatestPlaylistSong(
|
||||
if (pending) return pending;
|
||||
|
||||
const request = fetchLatestPlaylistSong(fetch, playlistId)
|
||||
.then((song) => {
|
||||
playlistCache.set(playlistId, {
|
||||
value: song,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return song;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingRequests.delete(playlistId);
|
||||
.then((song) => {
|
||||
playlistCache.set(playlistId, {
|
||||
value: song,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return song;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingRequests.delete(playlistId);
|
||||
});
|
||||
|
||||
pendingRequests.set(playlistId, request);
|
||||
|
||||
return request;
|
||||
@@ -231,18 +231,18 @@ export async function getLatestWatchedSeries(
|
||||
if (pending) return pending;
|
||||
|
||||
const request = fetchLatestWatchedSeries(fetch, libraryId)
|
||||
.then((series) => {
|
||||
seriesCache.set(libraryId, {
|
||||
value: series,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return series;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingSeriesRequests.delete(libraryId);
|
||||
.then((series) => {
|
||||
seriesCache.set(libraryId, {
|
||||
value: series,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return series;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingSeriesRequests.delete(libraryId);
|
||||
});
|
||||
|
||||
pendingSeriesRequests.set(libraryId, request);
|
||||
|
||||
return request;
|
||||
|
||||
176
src/lib/server/komga.ts
Normal file
176
src/lib/server/komga.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
import { KOMGA_URL, KOMGA_API_KEY } from '$app/env/private';
|
||||
|
||||
const CACHE_TTL = 3 * 60 * 60 * 1000;
|
||||
|
||||
interface KomgaAuthor {
|
||||
name: string;
|
||||
role: string;
|
||||
}
|
||||
|
||||
interface KomgaBook {
|
||||
id: string;
|
||||
name: string;
|
||||
number?: number;
|
||||
seriesTitle?: string;
|
||||
seriesId?: string;
|
||||
metadata?: {
|
||||
title?: string;
|
||||
summary?: string;
|
||||
authors?: KomgaAuthor[];
|
||||
releaseDate?: string;
|
||||
};
|
||||
media?: {
|
||||
pagesCount?: number;
|
||||
mediaType?: string;
|
||||
status?: string;
|
||||
};
|
||||
readProgress?: {
|
||||
page: number;
|
||||
completed: boolean;
|
||||
lastModified?: string;
|
||||
readDate?: string;
|
||||
} | null;
|
||||
}
|
||||
|
||||
interface KomgaBooksResponse {
|
||||
content: KomgaBook[];
|
||||
totalElements: number;
|
||||
totalPages: number;
|
||||
number: number;
|
||||
size: number;
|
||||
}
|
||||
|
||||
export interface LatestBook {
|
||||
id: string;
|
||||
name: string;
|
||||
authors: string[];
|
||||
series: string | null;
|
||||
seriesId: string | null;
|
||||
number: number | null;
|
||||
summary: string | null;
|
||||
releaseDate: string | null;
|
||||
|
||||
currentPage: number;
|
||||
totalPages: number;
|
||||
completion: number;
|
||||
completed: boolean;
|
||||
lastRead: string | null;
|
||||
|
||||
coverArt: string;
|
||||
}
|
||||
|
||||
interface CacheEntry<T> {
|
||||
value: T;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
const bookCache = new Map<string, CacheEntry<LatestBook | null>>();
|
||||
const pendingRequests = new Map<string, Promise<LatestBook | null>>();
|
||||
|
||||
function getHeaders() {
|
||||
return {
|
||||
'X-API-Key': KOMGA_API_KEY,
|
||||
Accept: 'application/json'
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchLatestBook(
|
||||
fetch: typeof globalThis.fetch,
|
||||
libraryId?: string
|
||||
): Promise<LatestBook | null> {
|
||||
const url = new URL(`${KOMGA_URL}/api/v1/books`);
|
||||
|
||||
url.searchParams.set('read_status', 'IN_PROGRESS');
|
||||
url.searchParams.set('page', '0');
|
||||
url.searchParams.set('size', '100');
|
||||
url.searchParams.set('sort', 'readProgress.lastModified,desc');
|
||||
|
||||
if (libraryId) {
|
||||
url.searchParams.set('library_id', libraryId);
|
||||
}
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: getHeaders()
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const body = await response.text();
|
||||
|
||||
throw new Error(`Komga error: ${response.status} - ${body}`);
|
||||
}
|
||||
|
||||
const data: KomgaBooksResponse = await response.json();
|
||||
|
||||
const book = data.content?.[0];
|
||||
|
||||
if (!book || !book.readProgress) return null;
|
||||
|
||||
const currentPage = book.readProgress.page ?? 0;
|
||||
const totalPages = book.media?.pagesCount ?? 0;
|
||||
|
||||
const completion =
|
||||
totalPages > 0 ? Math.min(100, Math.round((currentPage / totalPages) * 100)) : 0;
|
||||
|
||||
return {
|
||||
id: book.id,
|
||||
name: book.metadata?.title ?? book.name,
|
||||
authors: book.metadata?.authors?.map((author) => author.name) ?? [],
|
||||
series:
|
||||
book.seriesTitle != undefined
|
||||
? book.seriesTitle
|
||||
.replace(/\([^)]*\)/g, ' ')
|
||||
.trim()
|
||||
.replace(/\s+/g, ' ')
|
||||
: null,
|
||||
seriesId: book.seriesId ?? null,
|
||||
number: book.number ?? null,
|
||||
summary: book.metadata?.summary ?? null,
|
||||
releaseDate: book.metadata?.releaseDate ?? null,
|
||||
|
||||
currentPage,
|
||||
totalPages,
|
||||
completion,
|
||||
completed: book.readProgress.completed ?? false,
|
||||
lastRead: book.readProgress.lastModified ?? null,
|
||||
|
||||
coverArt: `/books/${encodeURIComponent(book.id)}`
|
||||
};
|
||||
}
|
||||
|
||||
export async function getLatestBook(
|
||||
fetch: typeof globalThis.fetch,
|
||||
libraryId?: string
|
||||
): Promise<LatestBook | null> {
|
||||
const cacheKey = libraryId ?? 'all';
|
||||
|
||||
const cached = bookCache.get(cacheKey);
|
||||
|
||||
if (cached && cached.expiresAt > Date.now()) {
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
const pending = pendingRequests.get(cacheKey);
|
||||
|
||||
if (pending) return pending;
|
||||
|
||||
const request = fetchLatestBook(fetch, libraryId)
|
||||
.then((book) => {
|
||||
bookCache.set(cacheKey, {
|
||||
value: book,
|
||||
expiresAt: Date.now() + CACHE_TTL
|
||||
});
|
||||
|
||||
return book;
|
||||
})
|
||||
.finally(() => {
|
||||
pendingRequests.delete(cacheKey);
|
||||
});
|
||||
|
||||
pendingRequests.set(cacheKey, request);
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
export function invalidateBookCache(libraryId?: string): void {
|
||||
bookCache.delete(libraryId ?? 'all');
|
||||
}
|
||||
Reference in New Issue
Block a user