/ clis / weread / ranking.js
ranking.js
 1  import { cli, Strategy } from '@jackwener/opencli/registry';
 2  import { fetchWebApi } from './utils.js';
 3  cli({
 4      site: 'weread',
 5      name: 'ranking',
 6      description: 'WeRead book rankings by category',
 7      domain: 'weread.qq.com',
 8      strategy: Strategy.PUBLIC,
 9      browser: false,
10      args: [
11          { name: 'category', positional: true, default: 'all', help: 'Category: all (default), rising, or numeric category ID' },
12          { name: 'limit', type: 'int', default: 20, help: 'Max results' },
13      ],
14      columns: ['rank', 'title', 'author', 'category', 'readingCount', 'bookId'],
15      func: async (_page, args) => {
16          const cat = encodeURIComponent(args.category ?? 'all');
17          const data = await fetchWebApi(`/bookListInCategory/${cat}`, { rank: '1' });
18          const books = data?.books ?? [];
19          return books.slice(0, Number(args.limit)).map((item, i) => ({
20              rank: i + 1,
21              title: item.bookInfo?.title ?? '',
22              author: item.bookInfo?.author ?? '',
23              category: item.bookInfo?.category ?? '',
24              readingCount: item.readingCount ?? 0,
25              bookId: item.bookInfo?.bookId ?? '',
26          }));
27      },
28  });