mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
30b79d4813
- slock: agents, channels, messages, servers now use getSlockContext/resolveChannelId
from utils.ts; error handling uses { error, help } return instead of bare throws
- linux-do: export fetchLinuxDoJson from feed.ts; migrate search, topic, categories,
tags, user-posts, user-topics from pipeline+throw to func() using fetchLinuxDoJson
- xueqiu: add utils.ts with fetchXueqiuJson helper; migrate hot, feed, search, stock,
watchlist, hot-stock, groups, kline, earnings-date from pipeline+throw to func()
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
import { fetchLinuxDoJson } from './feed.js';
|
|
|
|
function toLocalTime(utcStr: string): string {
|
|
if (!utcStr) return '';
|
|
const date = new Date(utcStr);
|
|
return Number.isNaN(date.getTime()) ? utcStr : date.toLocaleString();
|
|
}
|
|
|
|
function strip(html: string): string {
|
|
return (html || '')
|
|
.replace(/<br\s*\/?>/gi, ' ')
|
|
.replace(/<\/(p|div|li|blockquote|h[1-6])>/gi, ' ')
|
|
.replace(/<[^>]+>/g, '')
|
|
.replace(/ /g, ' ')
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/&#(?:(\d+)|x([0-9a-fA-F]+));/g, (_, dec, hex) => {
|
|
try { return String.fromCodePoint(dec !== undefined ? Number(dec) : parseInt(hex, 16)); } catch { return ''; }
|
|
})
|
|
.replace(/\s+/g, ' ')
|
|
.trim();
|
|
}
|
|
|
|
cli({
|
|
site: 'linux-do',
|
|
name: 'topic',
|
|
description: 'linux.do 帖子首页摘要和回复(首屏)',
|
|
domain: 'linux.do',
|
|
strategy: Strategy.COOKIE,
|
|
browser: true,
|
|
args: [
|
|
{ name: 'id', type: 'int', required: true, positional: true, help: 'Topic ID' },
|
|
{ name: 'limit', type: 'int', default: 20, help: 'Number of posts' },
|
|
],
|
|
columns: ['author', 'content', 'likes', 'created_at'],
|
|
func: async (page, kwargs) => {
|
|
const data = await fetchLinuxDoJson(page, `/t/${kwargs.id}.json`);
|
|
const posts = (data?.post_stream?.posts || []) as any[];
|
|
return posts.slice(0, kwargs.limit as number).map((p: any) => ({
|
|
author: p.username,
|
|
content: strip(p.cooked).slice(0, 200),
|
|
likes: p.like_count,
|
|
created_at: toLocalTime(p.created_at),
|
|
}));
|
|
},
|
|
});
|