added book entries from extensions
This commit is contained in:
@@ -107,94 +107,121 @@ async function searchBooksAniList(query) {
|
||||
return [];
|
||||
}
|
||||
|
||||
async function searchBooksExtensions(query) {
|
||||
const extensions = getAllExtensions();
|
||||
async function searchBooksInExtension(ext, name, query) {
|
||||
if (!ext) return [];
|
||||
|
||||
for (const [name, ext] of extensions) {
|
||||
if ((ext.type === 'book-board' || ext.type === 'manga-board') && ext.search) {
|
||||
try {
|
||||
console.log(`[${name}] Searching for book: ${query}`);
|
||||
const matches = await ext.search({
|
||||
query: query,
|
||||
media: {
|
||||
romajiTitle: query,
|
||||
englishTitle: query,
|
||||
startDate: { year: 0, month: 0, day: 0 }
|
||||
}
|
||||
});
|
||||
|
||||
if (matches && matches.length > 0) {
|
||||
return matches.map(m => ({
|
||||
id: m.id,
|
||||
title: { romaji: m.title, english: m.title },
|
||||
coverImage: { large: m.image || '' },
|
||||
averageScore: m.rating || m.score || null,
|
||||
format: 'MANGA',
|
||||
seasonYear: null,
|
||||
isExtensionResult: true
|
||||
}));
|
||||
if ((ext.type === 'book-board' || ext.type === 'manga-board') && ext.search) {
|
||||
try {
|
||||
console.log(`[${name}] Searching for book: ${query}`);
|
||||
const matches = await ext.search({
|
||||
query: query,
|
||||
media: {
|
||||
romajiTitle: query,
|
||||
englishTitle: query,
|
||||
startDate: { year: 0, month: 0, day: 0 }
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Extension search failed for ${name}:`, e);
|
||||
});
|
||||
|
||||
if (matches && matches.length > 0) {
|
||||
return matches.map(m => ({
|
||||
id: m.id,
|
||||
extensionName: name,
|
||||
title: { romaji: m.title, english: m.title },
|
||||
coverImage: { large: m.image || '' },
|
||||
averageScore: m.rating || m.score || null,
|
||||
format: 'MANGA',
|
||||
seasonYear: null,
|
||||
isExtensionResult: true
|
||||
}));
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Extension search failed for ${name}:`, e);
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
async function getChaptersForBook(id) {
|
||||
let bookData = await queryOne("SELECT full_data FROM books WHERE id = ?", [id])
|
||||
.then(row => row ? JSON.parse(row.full_data) : null)
|
||||
.catch(() => null);
|
||||
async function searchBooksExtensions(query) {
|
||||
const extensions = getAllExtensions();
|
||||
|
||||
if (!bookData) {
|
||||
try {
|
||||
const query = `query ($id: Int) { Media(id: $id, type: MANGA) { title { romaji english } startDate { year month day } } }`;
|
||||
const res = await fetch('https://graphql.anilist.co', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query, variables: { id: parseInt(id) } })
|
||||
});
|
||||
const d = await res.json();
|
||||
if(d.data?.Media) bookData = d.data.Media;
|
||||
} catch(e) {}
|
||||
for (const [name, ext] of extensions) {
|
||||
const results = await searchBooksInExtension(ext, name, query);
|
||||
if (results.length > 0) return results;
|
||||
}
|
||||
|
||||
if (!bookData) return { chapters: [] };
|
||||
return [];
|
||||
}
|
||||
|
||||
async function getChaptersForBook(id) {
|
||||
let bookData = null;
|
||||
let searchTitle = null;
|
||||
|
||||
if (typeof id === "string" && isNaN(Number(id))) {
|
||||
searchTitle = id.replaceAll("-", " ");
|
||||
} else {
|
||||
bookData = await queryOne("SELECT full_data FROM books WHERE id = ?", [id])
|
||||
.then(row => row ? JSON.parse(row.full_data) : null)
|
||||
.catch(() => null);
|
||||
|
||||
if (!bookData) {
|
||||
try {
|
||||
const query = `query ($id: Int) {
|
||||
Media(id: $id, type: MANGA) {
|
||||
title { romaji english }
|
||||
startDate { year month day }
|
||||
}
|
||||
}`;
|
||||
|
||||
const res = await fetch('https://graphql.anilist.co', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ query, variables: { id: parseInt(id) } })
|
||||
});
|
||||
|
||||
const d = await res.json();
|
||||
if (d.data?.Media) bookData = d.data.Media;
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
if (!bookData) return { chapters: [] };
|
||||
|
||||
const titles = [bookData.title.english, bookData.title.romaji].filter(Boolean);
|
||||
searchTitle = titles[0];
|
||||
}
|
||||
|
||||
const titles = [bookData.title.english, bookData.title.romaji].filter(t => t);
|
||||
const searchTitle = titles[0];
|
||||
|
||||
const allChapters = [];
|
||||
const extensions = getAllExtensions();
|
||||
|
||||
const searchPromises = Array.from(extensions.entries())
|
||||
.filter(([name, ext]) => (ext.type === 'book-board' || ext.type === 'manga-board') && ext.search && ext.findChapters)
|
||||
.filter(([_, ext]) =>
|
||||
(ext.type === 'book-board' || ext.type === 'manga-board') &&
|
||||
ext.search && ext.findChapters
|
||||
)
|
||||
.map(async ([name, ext]) => {
|
||||
try {
|
||||
console.log(`[${name}] Searching chapters for: ${searchTitle}`);
|
||||
|
||||
const matches = await ext.search({
|
||||
query: searchTitle,
|
||||
media: {
|
||||
media: bookData ? {
|
||||
romajiTitle: bookData.title.romaji,
|
||||
englishTitle: bookData.title.english,
|
||||
startDate: bookData.startDate
|
||||
}
|
||||
} : {}
|
||||
});
|
||||
|
||||
if (matches && matches.length > 0) {
|
||||
if (matches?.length) {
|
||||
const best = matches[0];
|
||||
const chaps = await ext.findChapters(best.id);
|
||||
|
||||
if (chaps && chaps.length > 0) {
|
||||
if (chaps?.length) {
|
||||
console.log(`[${name}] Found ${chaps.length} chapters.`);
|
||||
chaps.forEach(ch => {
|
||||
const num = parseFloat(ch.number);
|
||||
allChapters.push({
|
||||
id: ch.id,
|
||||
number: num,
|
||||
number: parseFloat(ch.number),
|
||||
title: ch.title,
|
||||
date: ch.releaseDate,
|
||||
provider: name
|
||||
@@ -284,6 +311,7 @@ module.exports = {
|
||||
searchBooksLocal,
|
||||
searchBooksAniList,
|
||||
searchBooksExtensions,
|
||||
searchBooksInExtension,
|
||||
getChaptersForBook,
|
||||
getChapterContent
|
||||
};
|
||||
Reference in New Issue
Block a user