/ clis / linux-do / topic.js
topic.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  import { fetchLinuxDoJson } from './feed.js';
 3  function toLocalTime(utcStr) {
 4      if (!utcStr)
 5          return '';
 6      const date = new Date(utcStr);
 7      return Number.isNaN(date.getTime()) ? utcStr : date.toLocaleString();
 8  }
 9  function strip(html) {
10      return (html || '')
11          .replace(/<br\s*\/?>/gi, ' ')
12          .replace(/<\/(p|div|li|blockquote|h[1-6])>/gi, ' ')
13          .replace(/<[^>]+>/g, '')
14          .replace(/&nbsp;/g, ' ')
15          .replace(/&amp;/g, '&')
16          .replace(/&lt;/g, '<')
17          .replace(/&gt;/g, '>')
18          .replace(/&quot;/g, '"')
19          .replace(/&#(?:(\d+)|x([0-9a-fA-F]+));/g, (_, dec, hex) => {
20          try {
21              return String.fromCodePoint(dec !== undefined ? Number(dec) : parseInt(hex, 16));
22          }
23          catch {
24              return '';
25          }
26      })
27          .replace(/\s+/g, ' ')
28          .trim();
29  }
30  cli({
31      site: 'linux-do',
32      name: 'topic',
33      description: 'linux.do 帖子首页摘要和回复(首屏)',
34      domain: 'linux.do',
35      strategy: Strategy.COOKIE,
36      browser: true,
37      args: [
38          { name: 'id', type: 'int', required: true, positional: true, help: 'Topic ID' },
39          { name: 'limit', type: 'int', default: 20, help: 'Number of posts' },
40      ],
41      columns: ['author', 'content', 'likes', 'created_at'],
42      func: async (page, kwargs) => {
43          const data = await fetchLinuxDoJson(page, `/t/${kwargs.id}.json`);
44          const posts = (data?.post_stream?.posts || []);
45          return posts.slice(0, kwargs.limit).map((p) => ({
46              author: p.username,
47              content: strip(p.cooked).slice(0, 200),
48              likes: p.like_count,
49              created_at: toLocalTime(p.created_at),
50          }));
51      },
52  });