mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
d2974a9ff6
* refactor(adapters): convert adapter layer from TypeScript to JavaScript Core framework stays TypeScript; adapter layer moves to JS-first. Adapters are essentially "executable config + browser scripts" that barely use TS features — this simplifies the build/distribution pipeline by removing the dist/clis/ intermediate compilation step. Changes: - Convert all 753 adapter files in clis/ from .ts to .js - Update tsconfig to exclude clis/ from compilation - Simplify build-manifest to scan clis/*.js directly (no dist/clis/) - Update discovery, main, fetch-adapters to load JS adapters from clis/ - Update generate-verified to output .js artifacts - Update package.json files field: dist/clis/ → clis/ - Fix all test files for the .ts → .js transition * fix(main): use findPackageRoot for BUILTIN_CLIS path The previous relative path (../../clis from __dirname) only worked for dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname is <repo>/src — resolving to /clis instead of <repo>/clis. Use findPackageRoot() which works for both dev and prod paths.
87 lines
3.4 KiB
JavaScript
87 lines
3.4 KiB
JavaScript
/**
|
|
* 36kr hot-list — DOM scraping.
|
|
*
|
|
* Navigates to the 36kr hot-list page and scrapes rendered article links.
|
|
* Supports category types: renqi (人气), zonghe (综合), shoucang (收藏), catalog (综合热门).
|
|
*/
|
|
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { CliError } from '@jackwener/opencli/errors';
|
|
const TYPE_MAP = {
|
|
renqi: '人气榜',
|
|
zonghe: '综合榜',
|
|
shoucang: '收藏榜',
|
|
catalog: '热门资讯',
|
|
};
|
|
function getShanghaiDate(date = new Date()) {
|
|
// Shanghai stays on UTC+8 year-round, so a fixed offset is sufficient here
|
|
// and avoids the slow Intl timezone path that timed out on Windows CI.
|
|
return new Date(date.getTime() + 8 * 60 * 60 * 1000).toISOString().slice(0, 10);
|
|
}
|
|
function buildHotListUrl(listType, date = new Date()) {
|
|
if (listType === 'catalog') {
|
|
return 'https://www.36kr.com/hot-list/catalog';
|
|
}
|
|
return `https://www.36kr.com/hot-list/${listType}/${getShanghaiDate(date)}/1`;
|
|
}
|
|
cli({
|
|
site: '36kr',
|
|
name: 'hot',
|
|
description: '36氪热榜 — trending articles (renqi/zonghe/shoucang/catalog)',
|
|
domain: 'www.36kr.com',
|
|
strategy: Strategy.PUBLIC,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Number of items (max 50)' },
|
|
{
|
|
name: 'type',
|
|
type: 'string',
|
|
default: 'catalog',
|
|
help: 'List type: renqi (人气), zonghe (综合), shoucang (收藏), catalog (热门资讯)',
|
|
},
|
|
],
|
|
columns: ['rank', 'title', 'url'],
|
|
func: async (page, args) => {
|
|
const count = Math.min(Number(args.limit) || 20, 50);
|
|
const listType = String(args.type ?? 'catalog');
|
|
if (!TYPE_MAP[listType]) {
|
|
throw new CliError('INVALID_ARGUMENT', `Unknown type "${listType}". Valid types: ${Object.keys(TYPE_MAP).join(', ')}`);
|
|
}
|
|
const url = buildHotListUrl(listType);
|
|
await page.goto(url);
|
|
// Poll DOM until article links appear (36kr renders client-side)
|
|
const deadline = Date.now() + 5000;
|
|
while (Date.now() < deadline) {
|
|
if (await page.evaluate('document.querySelectorAll("a[href*=\\"/p/\\"]").length'))
|
|
break;
|
|
await new Promise(r => setTimeout(r, 300));
|
|
}
|
|
// Scrape rendered article links from DOM (deduplicated)
|
|
const domItems = await page.evaluate(`
|
|
(() => {
|
|
const seen = new Set();
|
|
const results = [];
|
|
const links = document.querySelectorAll('a[href*="/p/"]');
|
|
for (const el of links) {
|
|
const href = el.getAttribute('href') || '';
|
|
const title = el.textContent?.trim() || '';
|
|
if (!title || title.length < 5 || seen.has(href) || seen.has(title)) continue;
|
|
seen.add(href);
|
|
seen.add(title);
|
|
results.push({ title, url: href.startsWith('http') ? href : 'https://36kr.com' + href });
|
|
}
|
|
return results;
|
|
})()
|
|
`);
|
|
const items = Array.isArray(domItems) ? domItems : [];
|
|
if (items.length === 0) {
|
|
throw new CliError('NO_DATA', 'Could not retrieve 36kr hot list', '36kr may have changed its DOM structure');
|
|
}
|
|
return items.slice(0, count).map((item, i) => ({
|
|
rank: i + 1,
|
|
title: item.title,
|
|
url: item.url,
|
|
}));
|
|
},
|
|
});
|
|
export { buildHotListUrl, getShanghaiDate };
|