feat(onebot): add structured Qzone feeds and cursor pagination
Expose readable feed content, media and available statistics while preserving existing result fields and callers. Carry continuation state across pages and report invalid or stalled pagination explicitly.
This commit is contained in:
19 files changed
+1287
-120
No files matched your search
@@ -0,0 +1,189 @@
|
||||
import { DomUtils, parseDocument } from 'htmlparser2';
|
||||
|
||||
type Element = ReturnType<typeof DomUtils.findAll>[number];
|
||||
type Node = ReturnType<typeof parseDocument>['children'][number];
|
||||
|
||||
export interface QzoneFeedContent {
|
||||
tid: string | null;
|
||||
content: string | null;
|
||||
content_complete: boolean | null;
|
||||
images: string[] | null;
|
||||
like_count: number | null;
|
||||
comment_count: number | null;
|
||||
}
|
||||
|
||||
function hasClass(node: Element, name: string): boolean {
|
||||
return (node.attribs.class ?? '').split(/\s+/).includes(name);
|
||||
}
|
||||
|
||||
function isContentNode(node: Element, root: Element): boolean {
|
||||
for (let parent = node.parent; parent && parent !== root; parent = parent.parent) {
|
||||
if (
|
||||
'attribs' in parent &&
|
||||
(hasClass(parent, 'mod-comments') ||
|
||||
hasClass(parent, 'comments-list') ||
|
||||
hasClass(parent, 'f-item-passive') ||
|
||||
hasClass(parent, 'f-ct-video'))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function text(node: Element): string {
|
||||
const chunks: string[] = [];
|
||||
const stack: (Node | string)[] = [...node.children].reverse();
|
||||
|
||||
while (stack.length) {
|
||||
const next = stack.pop()!;
|
||||
|
||||
if (typeof next === 'string') {
|
||||
chunks.push(next);
|
||||
} else if (next.type === 'text') {
|
||||
chunks.push(next.data.replace(/\s+/g, ' '));
|
||||
} else if (next.type === 'tag') {
|
||||
if (next.attribs['data-cmd'] === 'qz_toggle' || hasClass(next, 'load_img')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (next.name === 'br') {
|
||||
chunks.push('\n');
|
||||
} else if (next.name === 'img') {
|
||||
chunks.push(next.attribs.alt ?? '');
|
||||
} else {
|
||||
if (['div', 'p', 'li'].includes(next.name)) {
|
||||
chunks.push('\n');
|
||||
stack.push('\n');
|
||||
}
|
||||
|
||||
for (let i = next.children.length - 1; i >= 0; i--) {
|
||||
stack.push(next.children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return chunks
|
||||
.join('')
|
||||
.replace(/ *\n */g, '\n')
|
||||
.replace(/\n{3,}/g, '\n\n')
|
||||
.trim();
|
||||
}
|
||||
|
||||
function count(value: unknown): number | null {
|
||||
if (typeof value !== 'number' && (typeof value !== 'string' || !/^\d+$/.test(value))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const number = Number(value);
|
||||
return Number.isSafeInteger(number) && number >= 0 ? number : null;
|
||||
}
|
||||
|
||||
function imageUrl(value: string | undefined): string | null {
|
||||
if (!value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(value.startsWith('//') ? `https:${value}` : value);
|
||||
return url.protocol === 'http:' || url.protocol === 'https:' ? url.href : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** Extract only the post's own content; comments, avatars and video covers are separate. */
|
||||
export function parseQzoneFeedContent(
|
||||
html: string,
|
||||
key: string,
|
||||
uin: number,
|
||||
appid: number,
|
||||
likeCount?: unknown,
|
||||
commentCount?: unknown,
|
||||
): QzoneFeedContent {
|
||||
const doc = parseDocument(html);
|
||||
const elements = DomUtils.findAll(() => true, doc.children);
|
||||
const body = elements.find(
|
||||
(node) => hasClass(node, 'f-item') && node.attribs['data-key'] === key,
|
||||
);
|
||||
const result: QzoneFeedContent = {
|
||||
tid: null,
|
||||
content: null,
|
||||
content_complete: null,
|
||||
images: null,
|
||||
like_count: count(likeCount),
|
||||
comment_count: count(commentCount),
|
||||
};
|
||||
|
||||
if (!body) {
|
||||
return result;
|
||||
}
|
||||
|
||||
let root = body;
|
||||
|
||||
for (let parent = body.parent; parent && 'attribs' in parent; parent = parent.parent) {
|
||||
if (hasClass(parent, 'f-single')) {
|
||||
root = parent;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const bodyElements = DomUtils.findAll((node) => isContentNode(node, body), body.children);
|
||||
const metadata = bodyElements.find(
|
||||
(node) =>
|
||||
node.attribs.name === 'feed_data' &&
|
||||
node.attribs['data-uin'] === String(uin) &&
|
||||
node.attribs['data-fkey'] === key,
|
||||
);
|
||||
|
||||
if (appid === 311 && metadata?.attribs['data-tid']) {
|
||||
result.tid = metadata.attribs['data-tid'];
|
||||
}
|
||||
|
||||
const contentNodes = bodyElements.filter((node) => hasClass(node, 'f-info'));
|
||||
const complete = contentNodes.find(
|
||||
(node) => hasClass(node, 'qz_info_complete') && text(node) !== '',
|
||||
);
|
||||
const content = complete ?? contentNodes.find((node) => !hasClass(node, 'qz_info_complete'));
|
||||
|
||||
if (content) {
|
||||
result.content = text(content);
|
||||
result.content_complete =
|
||||
!!complete ||
|
||||
!DomUtils.findAll((node) => node.attribs['data-cmd'] === 'qz_toggle', content.children)
|
||||
.length;
|
||||
}
|
||||
|
||||
const images = new Set<string>();
|
||||
|
||||
for (const item of bodyElements.filter((node) => hasClass(node, 'img-item'))) {
|
||||
if (item.attribs['data-v_itemid'] || item.attribs['data-videotype']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const full = imageUrl(item.attribs['data-originurl'] ?? item.attribs['data-url3']);
|
||||
|
||||
if (full) {
|
||||
images.add(full);
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const image of DomUtils.findAll((node) => node.name === 'img', item.children)) {
|
||||
const url = imageUrl(image.attribs.truesrc ?? image.attribs.src);
|
||||
|
||||
if (url) {
|
||||
images.add(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.images = [...images];
|
||||
const like = DomUtils.findAll(
|
||||
(node) => hasClass(node, 'qz_like_btn_v3') && isContentNode(node, root),
|
||||
root.children,
|
||||
)[0];
|
||||
result.like_count ??= count(like?.attribs['data-likecnt']);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
const numericFields = [
|
||||
'begintime',
|
||||
'dayspac',
|
||||
'pagenum',
|
||||
'aisortEndTime',
|
||||
'aisortOffset',
|
||||
'aisortBeginTime',
|
||||
'aisortNextTime',
|
||||
'icServerTime',
|
||||
] as const;
|
||||
const stringFields = ['externparam', 'daylist', 'uinlist'] as const;
|
||||
const fields = [...numericFields, ...stringFields];
|
||||
type StateField = (typeof fields)[number];
|
||||
export type QzoneFeedState = Record<StateField, string>;
|
||||
|
||||
export function initialQzoneFeedState(): QzoneFeedState {
|
||||
return {
|
||||
begintime: '0',
|
||||
dayspac: '0',
|
||||
pagenum: '1',
|
||||
aisortEndTime: '0',
|
||||
aisortOffset: '0',
|
||||
aisortBeginTime: '0',
|
||||
aisortNextTime: '0',
|
||||
icServerTime: '0',
|
||||
externparam: '',
|
||||
daylist: '',
|
||||
uinlist: '',
|
||||
};
|
||||
}
|
||||
|
||||
function record(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
/** Carry forward the fields used by the web client, including opaque server state. */
|
||||
export function nextQzoneFeedState(
|
||||
main: Record<string, unknown>,
|
||||
previous: QzoneFeedState,
|
||||
): QzoneFeedState {
|
||||
const next = { ...previous };
|
||||
|
||||
for (const field of numericFields) {
|
||||
const value = main[field];
|
||||
|
||||
if (value === undefined || value === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
(typeof value !== 'number' && typeof value !== 'string') ||
|
||||
!/^\d+$/.test(String(value)) ||
|
||||
!Number.isSafeInteger(Number(value)) ||
|
||||
(field === 'pagenum' && Number(value) < 1)
|
||||
) {
|
||||
throw new Error(`空间好友动态分页字段异常: ${field}`);
|
||||
}
|
||||
|
||||
next[field] = String(Number(value));
|
||||
}
|
||||
|
||||
for (const field of stringFields) {
|
||||
const value = main[field];
|
||||
|
||||
if (value === undefined || (value === '' && field !== 'uinlist')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeof value !== 'string' || value.length > 32_768) {
|
||||
throw new Error(`空间好友动态分页字段异常: ${field}`);
|
||||
}
|
||||
|
||||
next[field] = value;
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
/** The server clock alone does not constitute pagination progress. */
|
||||
export function qzoneFeedPosition(state: QzoneFeedState): string {
|
||||
return JSON.stringify(
|
||||
fields.filter((field) => field !== 'icServerTime').map((field) => state[field]),
|
||||
);
|
||||
}
|
||||
|
||||
export function encodeQzoneFeedCursor(uin: string, page: number, state: QzoneFeedState): string {
|
||||
if (!Number.isSafeInteger(page) || page < 2) {
|
||||
throw new Error('空间好友动态分页页码超出范围');
|
||||
}
|
||||
const cursor = Buffer.from(JSON.stringify({ version: 1, uin, page, state })).toString(
|
||||
'base64url',
|
||||
);
|
||||
|
||||
if (cursor.length > 65_536) {
|
||||
throw new Error('空间好友动态分页游标过大');
|
||||
}
|
||||
|
||||
return cursor;
|
||||
}
|
||||
|
||||
export function decodeQzoneFeedCursor(
|
||||
cursor: string,
|
||||
uin: string,
|
||||
): {
|
||||
page: number;
|
||||
state: QzoneFeedState;
|
||||
} {
|
||||
if (!cursor || cursor.length > 65_536 || !/^[A-Za-z0-9_-]+$/.test(cursor)) {
|
||||
throw new Error('无效的空间好友动态 cursor');
|
||||
}
|
||||
|
||||
let value: unknown;
|
||||
|
||||
try {
|
||||
const bytes = Buffer.from(cursor, 'base64url');
|
||||
|
||||
if (bytes.toString('base64url') !== cursor) {
|
||||
throw new Error('non-canonical encoding');
|
||||
}
|
||||
|
||||
value = JSON.parse(bytes.toString('utf8'));
|
||||
} catch {
|
||||
throw new Error('无效的空间好友动态 cursor');
|
||||
}
|
||||
|
||||
if (
|
||||
!record(value) ||
|
||||
value.version !== 1 ||
|
||||
value.uin !== uin ||
|
||||
typeof value.page !== 'number' ||
|
||||
!Number.isSafeInteger(value.page) ||
|
||||
value.page < 2 ||
|
||||
!record(value.state)
|
||||
) {
|
||||
throw new Error('空间好友动态 cursor 无效或不属于当前账号');
|
||||
}
|
||||
|
||||
const rawState = value.state;
|
||||
|
||||
if (
|
||||
Object.keys(rawState).length !== fields.length ||
|
||||
!fields.every((field) => typeof rawState[field] === 'string')
|
||||
) {
|
||||
throw new Error('无效的空间好友动态 cursor 状态');
|
||||
}
|
||||
|
||||
const state = nextQzoneFeedState(rawState, initialQzoneFeedState());
|
||||
|
||||
if (fields.some((field) => state[field] !== rawState[field])) {
|
||||
throw new Error('无效的空间好友动态 cursor 状态');
|
||||
}
|
||||
|
||||
return { page: value.page, state };
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
import type { JsonValue } from '@snowluma/common/json';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import { RequestUtil, cookieToString, getBknFromCookie } from './request-util';
|
||||
import { parseQzoneFeedContent, type QzoneFeedContent } from './qzone-feed-content';
|
||||
import {
|
||||
decodeQzoneFeedCursor,
|
||||
encodeQzoneFeedCursor,
|
||||
initialQzoneFeedState,
|
||||
nextQzoneFeedState,
|
||||
qzoneFeedPosition,
|
||||
} from './qzone-feed-cursor';
|
||||
|
||||
const log = createLogger('Bridge.Web');
|
||||
|
||||
@@ -638,13 +646,6 @@ export async function getQzoneMsgList(
|
||||
}
|
||||
|
||||
// ─────────────── 好友动态 (friend feeds) — feeds3_html_more ───────────────
|
||||
// The friend-feed CGI returns each feed as a pre-rendered HTML blob plus a
|
||||
// few structured fields. We surface the stable structured fields and pass
|
||||
// the `html` through verbatim (deep HTML→segment parsing is out of scope —
|
||||
// callers that want it parse the blob themselves). Exact field names /
|
||||
// pagination cursor to be re-confirmed against a live capture, same posture
|
||||
// as the msglist helper.
|
||||
|
||||
interface RawFeedItem {
|
||||
uin?: number | string;
|
||||
nickname?: string;
|
||||
@@ -654,6 +655,8 @@ interface RawFeedItem {
|
||||
key?: string;
|
||||
feedskey?: string;
|
||||
html?: string;
|
||||
likecnt?: number | string;
|
||||
commentcnt?: number | string;
|
||||
}
|
||||
|
||||
interface RawFeedsResponse {
|
||||
@@ -662,6 +665,7 @@ interface RawFeedsResponse {
|
||||
message?: string;
|
||||
data?: {
|
||||
data?: RawFeedItem[] | null;
|
||||
main?: Record<string, unknown>;
|
||||
hasmore?: number | string;
|
||||
};
|
||||
}
|
||||
@@ -689,107 +693,233 @@ export interface QzoneFeedsResult {
|
||||
has_more: boolean;
|
||||
}
|
||||
|
||||
/** Pure transform from the raw feeds response into the OneBot list. */
|
||||
export function mapFeeds(data: RawFeedsResponse): QzoneFeedsResult {
|
||||
// The CGI pads the array with trailing `undefined`/null holes — drop them.
|
||||
const list = (data.data?.data ?? []).filter((f): f is RawFeedItem => !!f);
|
||||
export interface QzoneStructuredFeed extends QzoneFeed, QzoneFeedContent {}
|
||||
|
||||
export interface QzoneFeedsPage extends QzoneFeedsResult {
|
||||
feeds: QzoneStructuredFeed[];
|
||||
/** Opaque, account-bound continuation. Null at the end of the list. */
|
||||
next_cursor: string | null;
|
||||
}
|
||||
|
||||
function feedHasMore(data: RawFeedsResponse): boolean {
|
||||
const value = data.data?.main === undefined ? data.data?.hasmore : data.data.main.hasMoreFeeds;
|
||||
|
||||
if (value === true || value === 1 || value === '1') {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (value === false || value === 0 || value === '0') {
|
||||
return false;
|
||||
}
|
||||
|
||||
throw new Error('空间好友动态缺少有效的分页结束标志');
|
||||
}
|
||||
|
||||
/** Pure mapping; request continuation is attached by getQzoneFeeds. */
|
||||
export function mapFeeds(data: RawFeedsResponse): QzoneFeedsPage {
|
||||
if (!Array.isArray(data.data?.data)) {
|
||||
throw new Error('无法获取空间好友动态(响应结构异常)');
|
||||
}
|
||||
|
||||
// The web payload pads lists with null/undefined; these are not feed entries.
|
||||
const list = data.data.data.filter((item) => item != null);
|
||||
return {
|
||||
feeds: list.map((f) => ({
|
||||
uin: Number(f.uin ?? 0),
|
||||
nickname: f.nickname ?? '',
|
||||
time: Number(f.abstime ?? 0),
|
||||
appid: Number(f.appid ?? 0),
|
||||
// `key` is the per-feed handle; `feedskey` is its older alias on some
|
||||
// feed types. They name the same per-feed identifier here (NOT the
|
||||
// list-level next-page cursor, which lives on data.* not the item).
|
||||
key: String(f.key ?? f.feedskey ?? ''),
|
||||
html: f.html ?? '',
|
||||
})),
|
||||
has_more: Number(data.data?.hasmore ?? 0) !== 0,
|
||||
feeds: list.map((item) => {
|
||||
if (typeof item !== 'object' || Array.isArray(item)) {
|
||||
throw new Error('空间好友动态条目结构异常');
|
||||
}
|
||||
|
||||
const feed: QzoneFeed = {
|
||||
uin: Number(item.uin ?? 0),
|
||||
nickname: item.nickname ?? '',
|
||||
time: Number(item.abstime ?? 0),
|
||||
appid: Number(item.appid ?? 0),
|
||||
key: String(item.key ?? item.feedskey ?? ''),
|
||||
html: item.html ?? '',
|
||||
};
|
||||
|
||||
if (
|
||||
![feed.uin, feed.time, feed.appid].every(
|
||||
(n) => Number.isSafeInteger(n) && n >= 0,
|
||||
) ||
|
||||
typeof feed.nickname !== 'string' ||
|
||||
typeof feed.html !== 'string'
|
||||
) {
|
||||
throw new Error('空间好友动态条目字段异常');
|
||||
}
|
||||
|
||||
return {
|
||||
...feed,
|
||||
...parseQzoneFeedContent(
|
||||
feed.html,
|
||||
feed.key,
|
||||
feed.uin,
|
||||
feed.appid,
|
||||
item.likecnt,
|
||||
item.commentcnt,
|
||||
),
|
||||
};
|
||||
}),
|
||||
has_more: feedHasMore(data),
|
||||
next_cursor: null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the 好友动态 (friend-feed) list via the feeds3_html_more CGI on
|
||||
* ic2.qzone.qq.com, routed through the h5.qzone.qq.com proxy gateway — the
|
||||
* same gateway {@link getQzoneMsgList} and group-album use, because the
|
||||
* qzone.qq.com cookie jar only authenticates against the proxy origin
|
||||
* (hitting ic2 directly fails the referer/same-origin check). Body is
|
||||
* requested as JSONP (`format=jsonp` + a callback) and parsed with the
|
||||
* shared tolerant parser, matching slice-1's contract exactly.
|
||||
*
|
||||
* `pageNum` is 1-based; `count` is the page size. PAGINATION CAVEAT: this
|
||||
* CGI's reliable deep-pagination is driven by a time cursor
|
||||
* (begintime/externparam/usertime carried forward from the previous page),
|
||||
* which we do not yet thread — so `pageNum` is dependable for the first
|
||||
* page and `has_more` only signals whether more exist, not a stable
|
||||
* page-2 fetch. Cursor pagination is deferred until a live capture.
|
||||
*
|
||||
* Same throw-on-auth-failure contract as {@link getQzoneMsgList}: a missing
|
||||
* `data.data` array means the cookie/auth failed and throws, whereas a
|
||||
* genuinely empty feed (`data.data: []`) maps to an empty list.
|
||||
*/
|
||||
/** Read one page. Legacy page numbers walk continuations from the beginning. */
|
||||
export async function getQzoneFeeds(
|
||||
cookieObject: Record<string, string>,
|
||||
selfUin: string,
|
||||
pageNum = 1,
|
||||
count = 10,
|
||||
): Promise<QzoneFeedsResult> {
|
||||
cursor?: string,
|
||||
): Promise<QzoneFeedsPage> {
|
||||
if (!cookieObject || typeof cookieObject !== 'object') {
|
||||
throw new Error('cookieObject is required');
|
||||
}
|
||||
|
||||
if (
|
||||
!Number.isSafeInteger(pageNum) ||
|
||||
pageNum < 1 ||
|
||||
!Number.isSafeInteger(count) ||
|
||||
count < 1 ||
|
||||
count > 50
|
||||
) {
|
||||
throw new Error('空间好友动态页码或数量无效');
|
||||
}
|
||||
|
||||
const start =
|
||||
cursor === undefined
|
||||
? { page: 1, state: initialQzoneFeedState() }
|
||||
: decodeQzoneFeedCursor(cursor, selfUin);
|
||||
|
||||
if (cursor !== undefined && pageNum !== 1 && pageNum !== start.page) {
|
||||
throw new Error('page_num 与空间好友动态 cursor 不匹配');
|
||||
}
|
||||
|
||||
const target = cursor === undefined ? pageNum : start.page;
|
||||
const bkn = getBknFromCookie(cookieObject);
|
||||
const url = `https://h5.qzone.qq.com/proxy/domain/ic2.qzone.qq.com/cgi-bin/feeds/feeds3_html_more?${new URLSearchParams(
|
||||
{
|
||||
let state = start.state;
|
||||
const seen = new Set([qzoneFeedPosition(state)]);
|
||||
log.debug(
|
||||
'getQzoneFeeds start (uin=%s page=%d count=%d cursor=%s)',
|
||||
selfUin,
|
||||
target,
|
||||
count,
|
||||
cursor !== undefined,
|
||||
);
|
||||
|
||||
for (let page = start.page; page <= target; page++) {
|
||||
const query = new URLSearchParams({
|
||||
uin: selfUin,
|
||||
scope: '0',
|
||||
view: '1',
|
||||
filter: 'all',
|
||||
gid: '',
|
||||
flag: '1',
|
||||
filter: 'all',
|
||||
applist: 'all',
|
||||
pagenum: String(pageNum),
|
||||
refresh: '0',
|
||||
firstGetGroup: '0',
|
||||
mixnocache: '0',
|
||||
scene: '0',
|
||||
sidomain: 'qzonestyle.gtimg.cn',
|
||||
count: String(count),
|
||||
aisortEndTime: '0',
|
||||
aisortOffset: '0',
|
||||
aisortBeginTime: '0',
|
||||
begintime: '0',
|
||||
...state,
|
||||
getAisort: state.aisortNextTime,
|
||||
g_tk: bkn,
|
||||
callback: '_preloadCallback',
|
||||
format: 'jsonp',
|
||||
useutf8: '1',
|
||||
outputhtmlfeed: '1',
|
||||
},
|
||||
).toString()}`;
|
||||
usertime: String(Date.now()),
|
||||
rd: String(Math.random()),
|
||||
windowId: String(Math.random()),
|
||||
});
|
||||
query.delete('aisortNextTime');
|
||||
const url = `https://h5.qzone.qq.com/proxy/domain/ic2.qzone.qq.com/cgi-bin/feeds/feeds3_html_more?${query}`;
|
||||
|
||||
const text = await RequestUtil.HttpGetText(url, 'GET', '', {
|
||||
Cookie: cookieToString(cookieObject),
|
||||
});
|
||||
log.trace('getQzoneFeeds raw body (uin=%s): %s', selfUin, text);
|
||||
// feeds3_html_more's payload is a JS object literal, not JSON — parse it as
|
||||
// inert data (see parseQzoneCallback; never executed) rather than JSON.parse.
|
||||
const data = parseQzoneCallback<RawFeedsResponse>(text);
|
||||
try {
|
||||
const body = await RequestUtil.HttpGetText(url, 'GET', '', {
|
||||
Cookie: cookieToString(cookieObject),
|
||||
});
|
||||
const data = parseQzoneCallback<RawFeedsResponse>(body);
|
||||
|
||||
if (typeof data.code === 'number' && data.code !== 0) {
|
||||
log.warn(
|
||||
'getQzoneFeeds: non-zero code (uin=%s) code=%d msg=%s',
|
||||
selfUin,
|
||||
data.code,
|
||||
data.message,
|
||||
);
|
||||
throw new Error(`qzone feeds failed: code=${data.code} ${data.message ?? ''}`.trim());
|
||||
if (data.code !== 0 || (data.subcode !== undefined && data.subcode !== 0)) {
|
||||
throw new Error(
|
||||
`qzone feeds failed: code=${data.code} subcode=${data.subcode ?? 0} ${typeof data.message === 'string' ? data.message : ''}`.trim(),
|
||||
);
|
||||
}
|
||||
|
||||
const main = data.data?.main;
|
||||
|
||||
if (
|
||||
main !== undefined &&
|
||||
(main === null || typeof main !== 'object' || Array.isArray(main))
|
||||
) {
|
||||
throw new Error('空间好友动态分页信息结构异常');
|
||||
}
|
||||
|
||||
if (main?.error !== undefined && main.error !== '') {
|
||||
throw new Error('空间好友动态服务端报告拉取失败');
|
||||
}
|
||||
|
||||
const result = mapFeeds(data);
|
||||
|
||||
if (result.has_more) {
|
||||
if (!main) {
|
||||
throw new Error('空间好友动态缺少下一页游标');
|
||||
}
|
||||
|
||||
state = nextQzoneFeedState(main, state);
|
||||
const position = qzoneFeedPosition(state);
|
||||
|
||||
if (seen.has(position)) {
|
||||
throw new Error('空间好友动态分页游标未推进');
|
||||
}
|
||||
|
||||
seen.add(position);
|
||||
result.next_cursor = encodeQzoneFeedCursor(selfUin, page + 1, state);
|
||||
}
|
||||
|
||||
const unavailable = result.feeds.filter(
|
||||
(feed) => feed.appid === 311 && feed.content === null,
|
||||
).length;
|
||||
|
||||
if (unavailable) {
|
||||
log.warn(
|
||||
'getQzoneFeeds content unavailable (uin=%s page=%d entries=%d)',
|
||||
selfUin,
|
||||
page,
|
||||
unavailable,
|
||||
);
|
||||
}
|
||||
|
||||
log.debug(
|
||||
'getQzoneFeeds page (uin=%s page=%d entries=%d more=%s)',
|
||||
selfUin,
|
||||
page,
|
||||
result.feeds.length,
|
||||
result.has_more,
|
||||
);
|
||||
|
||||
if (page === target) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (!result.has_more) {
|
||||
return { feeds: [], has_more: false, next_cursor: null };
|
||||
}
|
||||
} catch (error) {
|
||||
log.warn(
|
||||
'getQzoneFeeds failed (uin=%s page=%d): %s',
|
||||
selfUin,
|
||||
page,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Array.isArray(data.data?.data)) {
|
||||
log.warn(
|
||||
'getQzoneFeeds: no data array in response (uin=%s) — likely auth/cookie failure or unverified response shape; body head=%s',
|
||||
selfUin,
|
||||
text.slice(0, 300),
|
||||
);
|
||||
throw new Error(`无法获取空间好友动态(响应结构异常): ${text.slice(0, 200)}`);
|
||||
}
|
||||
|
||||
return mapFeeds(data);
|
||||
throw new Error('空间好友动态分页未完成');
|
||||
}
|
||||
|
||||
// ─────────────── 发说说 (publish emotion) — emotion_cgi_publish_v6 ───────────────
|
||||
|
||||
Reference in new issue
Block a user