Работа над 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

@@ -1,9 +1,12 @@
const _ = require('lodash');
const he = require('he');
const WebWorker = require('../WebWorker');//singleton
const XmlParser = require('../xml/XmlParser');
const spaceChar = String.fromCodePoint(0x00B7);
const emptyFieldValue = '?';
const maxUtf8Char = String.fromCodePoint(0xFFFFF);
const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
const enruArr = (ruAlphabet + enAlphabet).split('');
@@ -37,7 +40,7 @@ class BasePage {
return this.makeEntry({
id: this.id,
title: this.title,
link: this.navLink({rel: 'subsection', href: `/${this.id}`}),
link: this.navLink({href: `/${this.id}`}),
});
}
@@ -48,11 +51,35 @@ class BasePage {
navLink(attrs) {
return this.makeLink({
href: this.opdsRoot + (attrs.href || ''),
rel: attrs.rel || '',
rel: attrs.rel || 'subsection',
type: 'application/atom+xml; profile=opds-catalog; kind=navigation',
});
}
acqLink(attrs) {
if (!attrs.href)
throw new Error('acqLink: no href');
if (!attrs.type)
throw new Error('acqLink: no type');
return this.makeLink({
href: attrs.href,
rel: 'http://opds-spec.org/acquisition/open-access',
type: attrs.type,
});
}
imgLink(attrs) {
if (!attrs.href)
throw new Error('acqLink: no href');
return this.makeLink({
href: attrs.href,
rel: `http://opds-spec.org/image${attrs.thumb ? '/thumbnail' : ''}`,
type: attrs.type || 'image/jpeg',
});
}
baseLinks() {
return [
this.navLink({rel: 'start'}),
@@ -92,7 +119,7 @@ class BasePage {
for (const row of queryRes.found) {
const rec = {
id: row.id,
title: '=' + (row[from] || 'Без имени'),
title: (row[from] || 'Без автора'),
q: `=${encodeURIComponent(row[from])}`,
};
@@ -103,8 +130,6 @@ class BasePage {
}
async opdsQuery(from, query) {
const result = [];
const queryRes = await this.webWorker.opdsQuery(from, query);
let count = 0;
for (const row of queryRes.found)
@@ -113,8 +138,9 @@ class BasePage {
if (count <= query.limit)
return await this.search(from, query);
const names = new Set();
const result = [];
const others = [];
const names = new Set();
for (const row of queryRes.found) {
const name = row.name.toUpperCase();
@@ -134,11 +160,129 @@ class BasePage {
}
}
if (query.depth > 1 && result.length == 1 && query[from]) {
const newQuery = _.cloneDeep(query);
newQuery[from] = decodeURIComponent(result[0].q);
if (newQuery[from].length >= query.depth) {
newQuery.depth = newQuery[from].length + 1;
return await this.opdsQuery(from, newQuery);
}
}
if (!query.others && query.depth == 1)
result.push({id: 'other', title: 'Все остальные', q: '___others'});
return (!query.others ? result : others);
}
//скопировано из BaseList.js, часть функционала не используется
filterBooks(books, query) {
const s = query;
const splitAuthor = (author) => {
if (!author) {
author = emptyFieldValue;
}
const result = author.split(',');
if (result.length > 1)
result.push(author);
return result;
};
const filterBySearch = (bookValue, searchValue) => {
if (!searchValue)
return true;
if (!bookValue)
bookValue = emptyFieldValue;
bookValue = bookValue.toLowerCase();
searchValue = searchValue.toLowerCase();
//особая обработка префиксов
if (searchValue[0] == '=') {
searchValue = searchValue.substring(1);
return bookValue.localeCompare(searchValue) == 0;
} else if (searchValue[0] == '*') {
searchValue = searchValue.substring(1);
return bookValue !== emptyFieldValue && bookValue.indexOf(searchValue) >= 0;
} else if (searchValue[0] == '#') {
searchValue = searchValue.substring(1);
return !bookValue || (bookValue !== emptyFieldValue && !enru.has(bookValue[0]) && bookValue.indexOf(searchValue) >= 0);
} else {
//where = `@dirtyIndexLR('value', ${db.esc(a)}, ${db.esc(a + maxUtf8Char)})`;
return bookValue.localeCompare(searchValue) >= 0 && bookValue.localeCompare(searchValue + maxUtf8Char) <= 0;
}
};
return books.filter((book) => {
//author
let authorFound = false;
const authors = splitAuthor(book.author);
for (const a of authors) {
if (filterBySearch(a, s.author)) {
authorFound = true;
break;
}
}
//genre
let genreFound = !s.genre;
if (!genreFound) {
const searchGenres = new Set(s.genre.split(','));
const bookGenres = book.genre.split(',');
for (let g of bookGenres) {
if (!g)
g = emptyFieldValue;
if (searchGenres.has(g)) {
genreFound = true;
break;
}
}
}
//lang
let langFound = !s.lang;
if (!langFound) {
const searchLang = new Set(s.lang.split(','));
langFound = searchLang.has(book.lang || emptyFieldValue);
}
//date
let dateFound = !s.date;
if (!dateFound) {
const date = this.queryDate(s.date).split(',');
let [from = '0000-00-00', to = '9999-99-99'] = date;
dateFound = (book.date >= from && book.date <= to);
}
//librate
let librateFound = !s.librate;
if (!librateFound) {
const searchLibrate = new Set(s.librate.split(',').map(n => parseInt(n, 10)).filter(n => !isNaN(n)));
librateFound = searchLibrate.has(book.librate);
}
return (this.showDeleted || !book.del)
&& authorFound
&& filterBySearch(book.series, s.series)
&& filterBySearch(book.title, s.title)
&& genreFound
&& langFound
&& dateFound
&& librateFound
;
});
}
}
module.exports = BasePage;