Работа над opds

This commit is contained in:
Book Pauk
2022-11-23 01:21:29 +07:00
parent 35925dbc6e
commit 8cf370c79d
4 changed files with 287 additions and 22 deletions

View File

@@ -17,51 +17,129 @@ class AuthorPage extends BasePage {
return '';
}
sortBooks(bookList) {
//схлопывание серий
const books = [];
const seriesSet = new Set();
for (const book of bookList) {
if (book.series) {
if (!seriesSet.has(book.series)) {
books.push({
type: 'series',
book
});
seriesSet.add(book.series);
}
} else {
books.push({
type: 'book',
book
});
}
}
//сортировка
books.sort((a, b) => {
if (a.type == 'series') {
return (b.type == 'series' ? a.book.series.localeCompare(b.book.series) : -1);
} else {
return (b.type == 'book' ? a.book.title.localeCompare(b.book.title) : 1);
}
});
return books;
}
sortSeriesBooks(seriesBooks) {
seriesBooks.sort((a, b) => {
const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
const dtitle = a.title.localeCompare(b.title);
const dext = a.ext.localeCompare(b.ext);
return (dserno ? dserno : (dtitle ? dtitle : dext));
});
return seriesBooks;
}
async body(req) {
const result = {};
const query = {author: '', depth: 1, del: 0, limit: 100};
if (req.query.author) {
query.author = req.query.author;
query.depth = query.author.length + 1;
}
const query = {
author: req.query.author || '',
series: req.query.series || '',
depth: 0,
del: 0,
limit: 100
};
query.depth = query.author.length + 1;
if (req.query.author == '___others') {
if (query.author == '___others') {
query.author = '';
query.depth = 1;
query.others = true;
}
const entry = [];
if (query.author && query.author[0] == '=') {
//книги по автору
const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
if (query.series) {
//книги по серии
const bookList = await this.webWorker.getSeriesBookList(query.series);
if (bookList.books) {
const books = JSON.parse(bookList.books);
let books = JSON.parse(bookList.books);
books = this.sortSeriesBooks(this.filterBooks(books, query));
for (const book of books) {
const title = book.title || 'Без названия';
const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'}`;
entry.push(
this.makeEntry({
id: book._uid,
title,
link: this.navLink({rel: 'subsection', href: `/${this.id}?book=${book._uid}`}),
link: this.navLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
})
);
}
}
} else if (query.author && query.author[0] == '=') {
//книги по автору
const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
if (bookList.books) {
let books = JSON.parse(bookList.books);
books = this.sortBooks(this.filterBooks(books, query));
for (const b of books) {
if (b.type == 'series') {
entry.push(
this.makeEntry({
id: b.book._uid,
title: `Серия: ${b.book.series}`,
link: this.navLink({
href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
`&series=${encodeURIComponent(b.book.series)}`}),
})
);
} else {
const title = b.book.title || 'Без названия';
entry.push(
this.makeEntry({
id: b.book._uid,
title,
link: this.navLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}),
})
);
}
}
}
} else {
//поиск по каталогу
const queryRes = await this.opdsQuery('author', query);
for (const rec of queryRes) {
console.log(rec);
for (const rec of queryRes) {
entry.push(
this.makeEntry({
id: rec.id,
title: this.bookAuthor(rec.title),//${(query.depth > 1 && rec.count ? ` (${rec.count})` : '')}
link: this.navLink({rel: 'subsection', href: `/${this.id}?author=${rec.q}`}),
link: this.navLink({href: `/${this.id}?author=${rec.q}`}),
})
);
}