generated from ztimson/template
Decompression caching and preview links
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import {fuzzyMatch} from './utils.js';
|
||||
import {decodeHtml} from '@ztimson/utils';
|
||||
|
||||
export const CATALOG_URL = 'https://library.kiwix.org/catalog/v2/entries';
|
||||
export const CATALOG_URL = 'https://library.kiwix.org';
|
||||
|
||||
const PAGE_SIZE = 100;
|
||||
|
||||
function parseEntries(xml, baseUrl = 'https://library.kiwix.org') {
|
||||
function parseEntries(xml, catalog = CATALOG_URL) {
|
||||
const blocks = xml.match(/<entry>[\s\S]*?<\/entry>/g) || [];
|
||||
return blocks.map(b => {
|
||||
const grab = re => (b.match(re) || [])[1] || '';
|
||||
@@ -14,29 +14,31 @@ function parseEntries(xml, baseUrl = 'https://library.kiwix.org') {
|
||||
|| b.match(/<link[^>]*type=["']image\/[^"']*["'][^>]*href=["']([^"']+)["']/);
|
||||
let tags = grab(/<tags>([^<]*)<\/tags>/);
|
||||
if(tags) tags = tags.split(';');
|
||||
const name = grab(/<name>([^<]*)<\/name>/);
|
||||
return {
|
||||
id: grab(/<id>([^<]*)<\/id>/),
|
||||
title: decodeHtml(grab(/<title>([^<]*)<\/title>/)),
|
||||
updated: new Date(grab(/<updated>([^<]*)<\/updated>/)),
|
||||
summary: decodeHtml(grab(/<summary>([^<]*)<\/summary>/)),
|
||||
language: grab(/<language>([^<]*)<\/language>/),
|
||||
name: grab(/<name>([^<]*)<\/name>/),
|
||||
name,
|
||||
category: grab(/<category>([^<]*)<\/category>/),
|
||||
tags,
|
||||
mediaCount: Number(grab(/<mediaCount>([^<]*)<\/mediaCount>/)) || 0,
|
||||
author: grab(/<author>\s*<name>([^<]*)<\/name>\s*<\/author>/m),
|
||||
publisher: grab(/<publisher>\s*<name>([^<]*)<\/name>\s*<\/publisher>/m),
|
||||
articleCount: Number(grab(/<articleCount>([^<]*)<\/articleCount>/)) || 0,
|
||||
sizeMb: linkMatch ? (Number((b.match(/length=["'](\d+)["']/) || [])[1] || 0) / 1024 / 1024).toFixed(1) : '?',
|
||||
href: linkMatch ? linkMatch[1] : null,
|
||||
icon: iconMatch ? new URL(iconMatch[1], baseUrl).href : null,
|
||||
sizeMb: linkMatch ? +(Number((b.match(/length=["'](\d+)["']/) || [])[1] || 0) / 1024 / 1024).toFixed(1) : '?',
|
||||
download: linkMatch ? linkMatch[1] : null,
|
||||
icon: iconMatch ? new URL(iconMatch[1], catalog).href : null,
|
||||
viewer: name ? new URL(`viewer#${name}`, catalog).href : null,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchEntries(term, lang, url = CATALOG_URL) {
|
||||
const params = new URLSearchParams({q: term, count: String(PAGE_SIZE), lang: lang || 'eng'});
|
||||
const res = await fetch(`${url}?${params}`);
|
||||
const res = await fetch(`${url}/catalog/v2/entries?${params}`);
|
||||
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
|
||||
return parseEntries(await res.text());
|
||||
}
|
||||
@@ -44,7 +46,7 @@ async function fetchEntries(term, lang, url = CATALOG_URL) {
|
||||
/** Looks up a single catalog entry by exact `name` (used to check for available updates). */
|
||||
export async function zimCatalogInfo(name, url = CATALOG_URL) {
|
||||
const params = new URLSearchParams({name, count: '5'});
|
||||
const res = await fetch(`${url}?${params}`);
|
||||
const res = await fetch(`${url}/catalog/v2/entries?${params}`);
|
||||
if (!res.ok) return null;
|
||||
return parseEntries(await res.text()).find(e => e.name === name) || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user