Files
jackwener__opencli/clis/weibo/comments.js
Benjamin Liu dadf01b56f fix(weibo): unwrap page.evaluate envelope in read adapters (#1568)
* fix(weibo): unwrap page.evaluate envelope in read adapters (#1567)

`page.evaluate(...)` returns a `{ session, data }` envelope rather than
the raw IIFE return value, so all weibo cookie-strategy read adapters
silently dropped their results on v1.7.19:

- `getSelfUid` returned the envelope object instead of the uid string,
  so `'10001' + uid` produced `'10001[object Object]'` and every
  feed/me/favorites request hit a broken list_id.
- `feed`, `hot`, `comments`, `search`, `favorites` did `Array.isArray`
  on the envelope (always false) and returned `[]`.
- `me`, `user`, `post` returned the envelope wrapper itself instead of
  the inner profile/post object.

Same pattern as #1561 for xiaohongshu/rednote. Adds an
`unwrapEvaluateResult` helper to `clis/weibo/utils.js` (kept local
rather than cross-importing from `xiaohongshu/search.js` since weibo
is an unrelated site) and wraps every `await page.evaluate(...)` in
the 8 read adapters plus the two helper calls in `getSelfUid`.

Skipped `publish.js` (write command, out of scope for this read fix).

Verified live:
- `opencli weibo hot --limit 3` returns 3 real trending items
- `opencli weibo feed --limit 3` returns 3 timeline posts with
  correct `https://weibo.com/<uid>/<mblogid>` URLs (proves
  `getSelfUid` unwrap works)
- `opencli weibo me` returns the logged-in profile object
- All 20 weibo unit tests pass (6 new for `unwrapEvaluateResult`)

* fix(weibo): fail typed on malformed evaluate payloads

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-05-14 22:34:43 +08:00

54 lines
2.1 KiB
JavaScript

/**
* Weibo comments — get comments on a post.
*/
import { cli, Strategy } from '@jackwener/opencli/registry';
import { requireArrayEvaluateResult, unwrapEvaluateResult } from './utils.js';
cli({
site: 'weibo',
name: 'comments',
access: 'read',
description: 'Get comments on a Weibo post',
domain: 'weibo.com',
strategy: Strategy.COOKIE,
args: [
{ name: 'id', required: true, positional: true, help: 'Post ID (numeric idstr)' },
{ name: 'limit', type: 'int', default: 20, help: 'Number of comments (max 50)' },
],
columns: ['rank', 'author', 'text', 'likes', 'replies', 'time'],
func: async (page, kwargs) => {
const count = Math.min(kwargs.limit || 20, 50);
await page.goto('https://weibo.com');
await page.wait(2);
const id = String(kwargs.id);
const data = requireArrayEvaluateResult(unwrapEvaluateResult(await page.evaluate(`
(async () => {
const id = ${JSON.stringify(id)};
const count = ${count};
const strip = (html) => (html || '').replace(/<[^>]+>/g, '').replace(/&nbsp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').trim();
const url = '/ajax/statuses/buildComments?flow=0&is_reload=1&id=' + id + '&is_show_bulletin=2&is_mix=0&count=' + count;
const resp = await fetch(url, {credentials: 'include'});
if (!resp.ok) return {error: 'HTTP ' + resp.status};
const data = await resp.json();
if (!data.ok) return {error: 'API error: ' + (data.msg || 'unknown')};
return (data.data || []).map((c, i) => {
const item = {
rank: i + 1,
author: c.user?.screen_name || '',
text: strip(c.text || ''),
likes: c.like_count || 0,
replies: c.total_number || 0,
time: c.created_at || '',
};
if (c.reply_comment) {
item.reply_to = (c.reply_comment.user?.screen_name || '') + ': ' + strip(c.reply_comment.text || '').substring(0, 80);
}
return item;
});
})()
`)), 'weibo comments');
return data;
},
});