support for headless browser & changes on book api

This commit is contained in:
2025-12-03 20:40:53 +01:00
parent 8e20743e8b
commit 920ce19cc2
9 changed files with 201 additions and 22 deletions

View File

@@ -85,23 +85,26 @@ export async function searchBooksInExtension(req: any, reply: FastifyReply) {
}
}
export async function getChapters(req: BookRequest, reply: FastifyReply) {
export async function getChapters(req: any, reply: FastifyReply) {
try {
const { id } = req.params;
return await booksService.getChaptersForBook(id);
const { ext } = req.query;
return await booksService.getChaptersForBook(id, Boolean(ext));
} catch (err) {
return { chapters: [] };
}
}
export async function getChapterContent(req: ChapterRequest, reply: FastifyReply) {
export async function getChapterContent(req: any, reply: FastifyReply) {
try {
const { bookId, chapter, provider } = req.params;
const { ext } = req.query;
const content = await booksService.getChapterContent(
bookId,
chapter,
provider
provider,
ext
);
return reply.send(content);

View File

@@ -126,6 +126,7 @@ export async function getBookInfoExtension(ext: Extension | null, id: string): P
if (ext.type === 'book-board' && ext.getMetadata) {
try {
const info = await ext.getMetadata(id);
if (info) {
@@ -198,8 +199,8 @@ async function fetchBookMetadata(id: string): Promise<Book | null> {
}
}
async function searchChaptersInExtension(ext: Extension, name: string, searchTitle: string, search: boolean): Promise<ChapterWithProvider[]> {
const cacheKey = `chapters:${name}:${searchTitle}`;
async function searchChaptersInExtension(ext: Extension, name: string, searchTitle: string, search: boolean, origin: string): Promise<ChapterWithProvider[]> {
const cacheKey = `chapters:${name}:${origin}:${search ? "search" : "id"}:${searchTitle}`;
const cached = await getCache(cacheKey);
if (cached) {
@@ -244,7 +245,7 @@ async function searchChaptersInExtension(ext: Extension, name: string, searchTit
const chaps = await ext.findChapters!(mediaId);
if (!chaps?.length) {
if (!chaps?.length){
return [];
}
@@ -267,11 +268,11 @@ async function searchChaptersInExtension(ext: Extension, name: string, searchTit
}
}
export async function getChaptersForBook(id: string): Promise<{ chapters: ChapterWithProvider[] }> {
export async function getChaptersForBook(id: string, ext: Boolean): Promise<{ chapters: ChapterWithProvider[] }> {
let bookData: Book | null = null;
let searchTitle: string = "";
if (!isNaN(Number(id))) {
if (!ext) {
const result = await getBookById(id);
if (!result || "error" in result) return { chapters: [] }
bookData = result;
@@ -292,13 +293,15 @@ export async function getChaptersForBook(id: string): Promise<{ chapters: Chapte
}
const allChapters: any[] = [];
let exts = "anilist";
if (ext) exts = "ext";
for (const [name, ext] of bookExtensions) {
if (name == extension) {
const chapters = await searchChaptersInExtension(ext, name, id, false);
const chapters = await searchChaptersInExtension(ext, name, id, false, exts);
allChapters.push(...chapters);
} else {
const chapters = await searchChaptersInExtension(ext, name, searchTitle, true);
const chapters = await searchChaptersInExtension(ext, name, searchTitle, true, exts);
allChapters.push(...chapters);
}
}
@@ -308,15 +311,17 @@ export async function getChaptersForBook(id: string): Promise<{ chapters: Chapte
};
}
export async function getChapterContent(bookId: string, chapterIndex: string, providerName: string): Promise<ChapterContent> {
export async function getChapterContent(bookId: string, chapterIndex: string, providerName: string, name: string): Promise<ChapterContent> {
const extensions = getAllExtensions();
const ext = extensions.get(providerName);
if (!ext) {
throw new Error("Provider not found");
}
let exts = "anilist";
if (name) exts = "ext";
const contentCacheKey = `content:${providerName}:${bookId}:${chapterIndex}`;
const contentCacheKey = `content:${providerName}:${exts}:${bookId}:${chapterIndex}`;
const cachedContent = await getCache(contentCacheKey);
if (cachedContent) {
@@ -335,7 +340,7 @@ export async function getChapterContent(bookId: string, chapterIndex: string, pr
}
}
const chapterList = await getChaptersForBook(bookId);
const chapterList = await getChaptersForBook(bookId, Boolean(name));
if (!chapterList?.chapters || chapterList.chapters.length === 0) {
throw new Error("Chapters not found");