/ clis / weibo / hot.js
hot.js
 1  /**
 2   * Weibo hot search — browser cookie API.
 3   */
 4  import { cli, Strategy } from '@jackwener/opencli/registry';
 5  cli({
 6      site: 'weibo',
 7      name: 'hot',
 8      description: '微博热搜',
 9      domain: 'weibo.com',
10      strategy: Strategy.COOKIE,
11      args: [
12          { name: 'limit', type: 'int', default: 30, help: 'Number of items (max 50)' },
13      ],
14      columns: ['rank', 'word', 'hot_value', 'category', 'label', 'url'],
15      func: async (page, kwargs) => {
16          const count = Math.min(kwargs.limit || 30, 50);
17          await page.goto('https://weibo.com');
18          const data = await page.evaluate(`
19        (async () => {
20          const resp = await fetch('/ajax/statuses/hot_band', {credentials: 'include'});
21          if (!resp.ok) return {error: 'HTTP ' + resp.status};
22          const data = await resp.json();
23          if (!data.ok) return {error: 'API error'};
24          const bandList = data.data?.band_list || [];
25          return bandList.map((item, i) => ({
26            rank: item.realpos || (i + 1),
27            word: item.word,
28            hot_value: item.num || 0,
29            category: item.category || '',
30            label: item.label_name || '',
31            url: 'https://s.weibo.com/weibo?q=' + encodeURIComponent('#' + item.word + '#')
32          }));
33        })()
34      `);
35          if (!Array.isArray(data))
36              return [];
37          return data.slice(0, count);
38      },
39  });