fixed some formatting and added komga integration stuff
This commit is contained in:
@@ -5,5 +5,8 @@ export const variables = defineEnvVars({
|
||||
JELLYFIN_API_KEY: {},
|
||||
JELLYFIN_PLAYLIST_ID: {},
|
||||
JELLYFIN_USER_ID: {},
|
||||
JELLYFIN_LIBRARY_ID: {}
|
||||
JELLYFIN_LIBRARY_ID: {},
|
||||
KOMGA_URL: {},
|
||||
KOMGA_API_KEY: {},
|
||||
KOMGA_LIBRARY_ID: {}
|
||||
});
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
@@ -1,19 +1,23 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { getLatestPlaylistSong, getLatestWatchedSeries } from '#lib/server/jellyfin';
|
||||
import { JELLYFIN_PLAYLIST_ID, JELLYFIN_LIBRARY_ID } from '$app/env/private';
|
||||
import { getLatestBook } from '#lib/server/komga';
|
||||
import { JELLYFIN_PLAYLIST_ID, JELLYFIN_LIBRARY_ID, KOMGA_LIBRARY_ID } from '$app/env/private';
|
||||
|
||||
export const load: PageServerLoad = () => {
|
||||
const latestSong = getLatestPlaylistSong(globalThis.fetch, JELLYFIN_PLAYLIST_ID);
|
||||
console.log(JELLYFIN_LIBRARY_ID);
|
||||
const latestSeries = getLatestWatchedSeries(globalThis.fetch, JELLYFIN_LIBRARY_ID);
|
||||
const latestBook = getLatestBook(globalThis.fetch, KOMGA_LIBRARY_ID);
|
||||
|
||||
latestSong.catch(() => {});
|
||||
latestSeries.catch(() => {});
|
||||
latestBook.catch(() => {});
|
||||
|
||||
return {
|
||||
streamed: {
|
||||
latestSong,
|
||||
latestSeries
|
||||
latestSeries,
|
||||
latestBook
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
@@ -108,7 +108,46 @@
|
||||
<p>{error}</p>
|
||||
{/await}
|
||||
</div>
|
||||
<div class="sub-container" style="--container-color:var(--ctp-red);">reading</div>
|
||||
<div
|
||||
class="sub-container flex items-center gap-3 overflow-hidden pr-3"
|
||||
style="--container-color:var(--ctp-red);"
|
||||
>
|
||||
{#await data.streamed.latestBook}
|
||||
<p>Loading Latest Book...</p>
|
||||
{:then book}
|
||||
<img
|
||||
src={book.coverArt}
|
||||
class="aspect-1/1 h-full w-auto shrink-0 object-contain"
|
||||
alt=""
|
||||
/>
|
||||
<div class="flex min-w-0 flex-col justify-center">
|
||||
<p class="fira-mono-medium">Currently Reading…</p>
|
||||
<div
|
||||
id="music-title-block"
|
||||
onclick={() =>
|
||||
window.open(
|
||||
`https://bookbrainz.org/search?q=${book.series} ${book.number}`,
|
||||
'_blank'
|
||||
)}
|
||||
style="cursor: pointer;"
|
||||
role="link"
|
||||
onkeydown={(e) => {
|
||||
if (e.key === 'Enter') {
|
||||
}
|
||||
}}
|
||||
tabindex="0"
|
||||
class="pl-2"
|
||||
>
|
||||
<p class="fira-mono-bold w-full truncate text-2xl">
|
||||
{book.series}
|
||||
</p>
|
||||
<p class="fira-mono-medium w-full truncate text-xl">
|
||||
v{book.number} p{book.currentPage}/{book.totalPages}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
{/await}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid grid-cols-[4fr_3fr] grid-rows-1 gap-4">
|
||||
|
||||
32
src/routes/books/[bookId]/+server.ts
Normal file
32
src/routes/books/[bookId]/+server.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
import { KOMGA_URL, KOMGA_API_KEY } from '$app/env/private';
|
||||
|
||||
export const GET: RequestHandler = async ({ params, fetch }) => {
|
||||
const { bookId } = params;
|
||||
|
||||
if (!/^[a-zA-Z0-9-]+$/.test(bookId)) {
|
||||
error(400, 'Invalid book ID');
|
||||
}
|
||||
|
||||
const response = await fetch(
|
||||
`${KOMGA_URL}/api/v1/books/${encodeURIComponent(bookId)}/thumbnail`,
|
||||
{
|
||||
headers: {
|
||||
'X-API-Key': KOMGA_API_KEY
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
error(response.status, 'Book cover unavailable');
|
||||
}
|
||||
|
||||
return new Response(response.body, {
|
||||
headers: {
|
||||
'Content-Type': response.headers.get('Content-Type') ?? 'image/jpeg',
|
||||
'Cache-Control': 'private, max-age=3600'
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user