feat: 完善本地书库与发布更新流程
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
parent
de3e1d8a44
commit
b8c8d24107
+17
-9
@@ -1,6 +1,9 @@
|
||||
const { fetchJson, clampPage } = require('./http');
|
||||
|
||||
const PAGE_SIZE = 30;
|
||||
const CATALOG_START = '2000-01-01';
|
||||
const SNAPSHOT_TTL = 5 * 60 * 1000;
|
||||
let catalogSnapshot = null;
|
||||
|
||||
function toItem(r, server) {
|
||||
const authors = String(r.authors || '').split(';').map((s) => s.trim()).filter(Boolean).slice(0, 3).join(', ');
|
||||
@@ -21,7 +24,7 @@ async function fetchWindow(server, from, to, cursor, tries = 3) {
|
||||
let last;
|
||||
for (let i = 0; i < tries; i++) {
|
||||
try {
|
||||
const j = await fetchJson(`https://api.biorxiv.org/details/${server}/${from}/${to}/${cursor}`);
|
||||
const j = await fetchJson(`https://api.biorxiv.org/details/${server}/${from}/${to}/${cursor}`, { retries: 0 });
|
||||
const total = parseInt(j.messages && j.messages[0] && j.messages[0].total, 10) || 0;
|
||||
return { total, collection: j.collection || [] };
|
||||
} catch (e) {
|
||||
@@ -41,14 +44,19 @@ module.exports = {
|
||||
async list(page) {
|
||||
page = clampPage(page);
|
||||
const server = 'biorxiv';
|
||||
const end = new Date();
|
||||
const daysPerPage = 3;
|
||||
const startIdx = (page - 1) * daysPerPage;
|
||||
const from = new Date(end.getTime() - (startIdx + daysPerPage) * 864e5);
|
||||
const to = new Date(end.getTime() - startIdx * 864e5);
|
||||
const { collection } = await fetchWindow(server, dateStr(from), dateStr(to), 0);
|
||||
const items = collection.slice(0, PAGE_SIZE).map((r) => toItem(r, server));
|
||||
return { items, maxPage: 1000, page };
|
||||
const to = dateStr(new Date());
|
||||
if (!catalogSnapshot || catalogSnapshot.to !== to || catalogSnapshot.expiresAt <= Date.now()) {
|
||||
const first = await fetchWindow(server, CATALOG_START, to, 0);
|
||||
catalogSnapshot = { to, total: first.total, expiresAt: Date.now() + SNAPSHOT_TTL };
|
||||
}
|
||||
const total = catalogSnapshot.total;
|
||||
const end = Math.max(0, total - (page - 1) * PAGE_SIZE);
|
||||
const cursor = Math.max(0, end - PAGE_SIZE);
|
||||
const count = end - cursor;
|
||||
if (!count) return { items: [], maxPage: Math.max(1, Math.ceil(total / PAGE_SIZE)), page };
|
||||
const { collection } = await fetchWindow(server, CATALOG_START, to, cursor);
|
||||
const items = collection.slice(0, count).reverse().map((r) => toItem(r, server));
|
||||
return { items, maxPage: Math.max(1, Math.ceil(total / PAGE_SIZE)), page };
|
||||
},
|
||||
|
||||
async search() {
|
||||
|
||||
Reference in New Issue
Block a user