/ clis / linux-do / user-topics.js
user-topics.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  cli({
10      site: 'linux-do',
11      name: 'user-topics',
12      description: 'linux.do 用户创建的话题',
13      domain: 'linux.do',
14      strategy: Strategy.COOKIE,
15      browser: true,
16      args: [
17          { name: 'username', required: true, positional: true, help: 'Username' },
18          { name: 'limit', type: 'int', default: 20, help: 'Number of topics' },
19      ],
20      columns: ['rank', 'title', 'replies', 'created_at', 'likes', 'views', 'url'],
21      func: async (page, kwargs) => {
22          const username = String(kwargs.username);
23          const limit = kwargs.limit;
24          const data = await fetchLinuxDoJson(page, `/topics/created-by/${encodeURIComponent(username)}.json`);
25          const topics = (data?.topic_list?.topics || []);
26          return topics.slice(0, limit).map((t, i) => ({
27              rank: i + 1,
28              title: t.fancy_title || t.title || '',
29              replies: t.posts_count || 0,
30              created_at: toLocalTime(t.created_at),
31              likes: t.like_count || 0,
32              views: t.views || 0,
33              url: 'https://linux.do/t/topic/' + t.id,
34          }));
35      },
36  });