Работа над opds

This commit is contained in:
Book Pauk
2022-11-22 20:09:00 +07:00
parent d0e79b0abb
commit 35925dbc6e
5 changed files with 216 additions and 14 deletions

View File

@@ -1,6 +1,14 @@
const he = require('he');
const WebWorker = require('../WebWorker');//singleton
const XmlParser = require('../xml/XmlParser');
const spaceChar = String.fromCodePoint(0x00B7);
const ruAlphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
const enAlphabet = 'abcdefghijklmnopqrstuvwxyz';
const enruArr = (ruAlphabet + enAlphabet).split('');
const enru = new Set(enruArr);
class BasePage {
constructor(config) {
this.config = config;
@@ -16,6 +24,8 @@ class BasePage {
if (!entry.title)
throw new Error('makeEntry: no title');
entry.title = he.escape(entry.title);
const result = {
updated: (new Date()).toISOString().substring(0, 19) + 'Z',
};
@@ -73,6 +83,62 @@ class BasePage {
async body() {
throw new Error('Body not implemented');
}
// -- stuff -------------------------------------------
async search(from, query) {
const result = [];
const queryRes = await this.webWorker.search(from, query);
for (const row of queryRes.found) {
const rec = {
id: row.id,
title: '=' + (row[from] || 'Без имени'),
q: `=${encodeURIComponent(row[from])}`,
};
result.push(rec);
}
return result;
}
async opdsQuery(from, query) {
const result = [];
const queryRes = await this.webWorker.opdsQuery(from, query);
let count = 0;
for (const row of queryRes.found)
count += row.count;
if (count <= query.limit)
return await this.search(from, query);
const names = new Set();
const others = [];
for (const row of queryRes.found) {
const name = row.name.toUpperCase();
if (!names.has(name)) {
const rec = {
id: row.id,
title: name.replace(/ /g, spaceChar),
q: encodeURIComponent(row.name.toLowerCase()),
count: row.count,
};
if (query.depth > 1 || enru.has(row.name[0].toLowerCase())) {
result.push(rec);
} else {
others.push(rec);
}
names.add(name);
}
}
if (!query.others && query.depth == 1)
result.push({id: 'other', title: 'Все остальные', q: '___others'});
return (!query.others ? result : others);
}
}
module.exports = BasePage;