mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
a78ceb1602
* feat: 11 read adapters across 8 sites (dblp / steam / bbc / devto / lobsters / medium / coingecko / hf) Round 2 of the adapter expansion sweep. All 11 commands hit public APIs (no browser, no auth), follow the post-#1332 typed-error / no-silent-failure discipline, and were live-verified against real endpoints. New adapters: - dblp/author : recent publications for one author (resolve PID by name, or pass --pid) - steam/search : storefront name search (storesearch API) - steam/app : single app detail (appdetails API; HTML entities decoded) - bbc/topic : per-topic RSS (8 canonical BBC News feeds) - devto/latest : /api/articles/latest with --page pagination - lobsters/domain : stories from a specific source domain (/domains/<d>.json) - medium/tag : tag RSS (description full-length, no silent truncation) - coingecko/exchanges : trust score + 24h BTC volume leaderboard - coingecko/categories : sector buckets with 6 sort options - coingecko/global : aggregate market totals + BTC/ETH dominance - hf/paper : single-paper detail by arXiv id (summary, ai_summary, ai_keywords, upvotes) Also adds clis/steam/utils.js + clis/bbc/utils.js as shared helpers (HTML entity decode, RSS parsing). All listings carry a round-trippable id where a detail sibling exists; advise:listing-id-pairing reports zero new violations. typed- error-lint and silent-column-drop gates both unchanged from baseline. Manifest: 698 → 709 (+11 entries). * fix: tighten adapter round2 contracts
80 lines
3.0 KiB
JavaScript
80 lines
3.0 KiB
JavaScript
// Shared helpers for the bbc adapters that hit BBC's public RSS feeds.
|
|
import { ArgumentError, CommandExecutionError } from '@jackwener/opencli/errors';
|
|
|
|
export const BBC_FEED_BASE = 'https://feeds.bbci.co.uk/news';
|
|
const UA = 'opencli-bbc-adapter (+https://github.com/jackwener/opencli)';
|
|
|
|
const HTML_ENTITIES = {
|
|
'&': '&', '<': '<', '>': '>', '"': '"', ''': "'", ''': "'", ' ': ' ',
|
|
};
|
|
|
|
export function decodeHtmlEntities(value) {
|
|
return String(value ?? '')
|
|
.replace(/&#x([0-9a-fA-F]+);/g, (_, h) => String.fromCodePoint(parseInt(h, 16)))
|
|
.replace(/&#(\d+);/g, (_, d) => String.fromCodePoint(parseInt(d, 10)))
|
|
.replace(/&(amp|lt|gt|quot|apos|#39|nbsp);/g, (m) => HTML_ENTITIES[m] || m);
|
|
}
|
|
|
|
/** Extract `<tag>…</tag>` (CDATA-aware) from a block. */
|
|
export function extractRssTag(block, tag) {
|
|
const cdata = block.match(new RegExp(`<${tag}[^>]*>\\s*<!\\[CDATA\\[([\\s\\S]*?)\\]\\]>\\s*<\\/${tag}>`));
|
|
if (cdata) return cdata[1];
|
|
const plain = block.match(new RegExp(`<${tag}[^>]*>([\\s\\S]*?)<\\/${tag}>`));
|
|
return plain ? plain[1] : '';
|
|
}
|
|
|
|
export function parseRssItems(xml) {
|
|
const out = [];
|
|
const re = /<item[^>]*>([\s\S]*?)<\/item>/g;
|
|
let m;
|
|
while ((m = re.exec(String(xml || ''))) !== null) {
|
|
const block = m[1];
|
|
out.push({
|
|
title: decodeHtmlEntities(extractRssTag(block, 'title')).trim(),
|
|
description: decodeHtmlEntities(extractRssTag(block, 'description')).trim(),
|
|
link: decodeHtmlEntities(extractRssTag(block, 'link')).trim(),
|
|
pubDate: decodeHtmlEntities(extractRssTag(block, 'pubDate')).trim(),
|
|
guid: decodeHtmlEntities(extractRssTag(block, 'guid')).trim(),
|
|
});
|
|
}
|
|
return out;
|
|
}
|
|
|
|
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
|
|
const raw = value ?? defaultValue;
|
|
const n = typeof raw === 'number' ? raw : Number(raw);
|
|
if (!Number.isInteger(n) || n <= 0) {
|
|
throw new ArgumentError(`bbc ${label} must be a positive integer`);
|
|
}
|
|
if (n > maxValue) {
|
|
throw new ArgumentError(`bbc ${label} must be <= ${maxValue}`);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
export async function bbcFetchRss(path, label) {
|
|
const url = `${BBC_FEED_BASE}/${path}`;
|
|
let resp;
|
|
try {
|
|
resp = await fetch(url, { headers: { 'user-agent': UA, accept: 'application/rss+xml, application/xml' } });
|
|
}
|
|
catch (err) {
|
|
throw new CommandExecutionError(
|
|
`${label} request failed: ${err?.message ?? err}`,
|
|
'Check that feeds.bbci.co.uk is reachable from this network.',
|
|
);
|
|
}
|
|
if (!resp.ok) {
|
|
throw new CommandExecutionError(`${label} returned HTTP ${resp.status} (${url})`);
|
|
}
|
|
return resp.text();
|
|
}
|
|
|
|
/** Convert RFC-822 pubDate to ISO `YYYY-MM-DD`; empty string on parse failure. */
|
|
export function pubDateToIso(value) {
|
|
if (!value) return '';
|
|
const d = new Date(value);
|
|
if (Number.isNaN(d.getTime())) return '';
|
|
return d.toISOString().slice(0, 10);
|
|
}
|