instagram-private-api 中 AccountFollowersFeedResponse:解析关注列表 Feed 的响应结构与分页机制

原创2026-10-14 17:24:17261 阅读
文章标签:后端

instagram-private-api 中 AccountFollowersFeedResponse:解析关注列表 Feed 的响应结构与分页机制

本文围绕 instagram-private-api 中 AccountFollowersFeedResponse 接口展开:它是调用 ig.feed.accountFollowers() 拉取某个账号“粉丝列表”时,Instagram 私有的 /api/v1/friendships/{user_id}/followers/ 接口返回的 JSON 结构在 TypeScript 中的类型定义。读完本文,你将完整掌握该响应接口的 6 个字段、粉丝条目类 AccountFollowersFeedResponseUsersItem 的继承关系与可用方法,以及如何基于 next_max_id 游标实现自动分页、状态序列化与 RxJS 流式订阅,进而完成“遍历全部粉丝”“对比互关关系”等实际任务。

接口定义与字段总览

AccountFollowersFeedResponse 定义在 src/responses/account-followers.feed.response.ts,并在 src/responses/index.ts 中通过 export * from './account-followers.feed.response' 对外导出,属于 SDK responses 模块的公开类型之一(对应 docs/interfaces/responses/AccountFollowersFeedResponse.md 文档页)。其完整定义为:

import { ProfileEntity } from '../entities/profile.entity';

export interface AccountFollowersFeedResponse {
  sections: null;
  users: AccountFollowersFeedResponseUsersItem[];
  big_list: boolean;
  next_max_id: string;
  page_size: number;
  status: string;
}

该接口共包含 6 个字段,逐一说明如下:

字段 类型 说明
sections null 固定为 null 的占位字段。类型上被显式声明为 null,说明在该接口的实际返回中此字段不参与数据承载(见 account-followers.feed.response.ts#L4)
users AccountFollowersFeedResponseUsersItem<a href="https://link.gitcode.com/i/81857f6c4e15d1f3478de793479acd90" target="_blank">] 本页粉丝列表的数组,元素类型为 AccountFollowersFeedResponseUsersItem 类,每个元素是一个可交互的 Profile 实体(见 [account-followers.feed.response.ts#L5)
big_list boolean 标识当前列表是否为“大列表”(粉丝量超过客户端单页上限时返回不同结构)。SDK 未基于该字段做特殊处理,仅作为透传字段保留(见 account-followers.feed.response.ts#L6)
next_max_id string 下一页的分页游标。为空字符串时代表没有更多数据,是驱动 Feed 自动翻页的核心字段(见 account-followers.feed.response.ts#L7)
page_size number 服务端每页返回的条目数量,可用于预估翻页次数
status string 固定状态标识,正常请求下为 "ok"

从源码结构看,这个接口与 Feed 的请求逻辑是严格一一对应的:AccountFollowersFeed 在 src/feeds/account-followers.feed.ts 中声明为 Feed<AccountFollowersFeedResponse, AccountFollowersFeedResponseUsersItem>,即响应类型绑定为本接口、条目类型绑定为 users 数组的元素类型。

条目类 AccountFollowersFeedResponseUsersItem

users 数组的元素不是普通接口,而是一个继承自 ProfileEntity 的类(定义见 account-followers.feed.response.ts#L11-L21),完整字段如下:

字段 类型 说明
pk number 用户数字 ID,覆盖了父类 ProfileEntity 中 string | number 的定义,粉丝列表接口中为纯数字(见 account-followers.feed.response.ts#L12)
username string 用户名
full_name string 展示昵称
is_private boolean 是否为私密账号
profile_pic_url string 头像 URL
profile_pic_id string(可选) 头像资源 ID
is_verified boolean 是否为认证(蓝标)账号
has_anonymous_profile_picture boolean 是否使用匿名(默认灰色)头像
latest_reel_media number(可选) 该用户最近一条 Reels 媒体 ID,未发布过 Reels 时可能缺省

这个类继承自 src/entities/profile.entity.ts 中的 ProfileEntity,因此每个粉丝条目除了携带上述只读属性,还自带两个与“关注关系”直接相关的方法(详见 ProfileEntity 文档):

  • checkFollow():先调用 ig.friendship.show(pk) 查询当前状态,若尚未关注则执行 ig.friendship.create(pk);
  • checkUnfollow():先查询,若当前处于关注状态则执行 ig.friendship.destroy(pk)。

这意味着遍历粉丝列表拿到的每一个条目都是“活的”实体——可以直接在上面追加关注/取关操作,而不必另行持有 client 实例。这也是将 users 元素设计为 ProfileEntity 子类而非纯数据接口的意义所在。

分页机制:next_max_id 如何驱动 Feed 自动翻页

AccountFollowersFeedResponse 之所以只暴露 next_max_id、page_size 这几个“看似简单”的分页字段,是因为翻页状态全部由 Feed 层消化。核心逻辑在 src/feeds/account-followers.feed.ts:

set state(body: AccountFollowersFeedResponse) {
  this.moreAvailable = !!body.next_max_id;   // 有游标即认为还有下一页
  this.nextMaxId = body.next_max_id;
}

async request() {
  const { body } = await this.client.request.send<AccountFollowersFeedResponse>({
    url: `/api/v1/friendships/${this.id}/followers/`,
    qs: {
      max_id: this.nextMaxId,      // 上一页返回的游标
      search_surface: this.searchSurface,
      order: this.order,           // 目前只有 'default' 生效
      query: this.query,
      enable_groups: this.enableGroups,
    },
  });
  this.state = body;
  return body;
}

async items() {
  const body = await this.request();
  return body.users.map(user => plainToClassFromExist(new AccountFollowersFeedResponseUsersItem(this.client), user));
}

三个关键点:

  1. 游标传递:request() 把上一次的 nextMaxId 作为 max_id 查询参数发回服务端。首次请求时 nextMaxId 为 undefined,服务端返回第一页及新的 next_max_id;state setter 随即把它存回 nextMaxId,并依据 !!body.next_max_id 置位基类的 moreAvailable 标志。当服务端返回空字符串游标时,翻页终止——这就是判断“粉丝列表是否取完”的唯一依据。
  2. 条目实例化:items() 并不直接返回原始 JSON,而是用 class-transformer 的 plainToClassFromExist 把 body.users 中每个纯对象“填充”到预建的 AccountFollowersFeedResponseUsersItem 实例上(构造函数需要 client),从而得到带 checkFollow/checkUnfollow 方法的实体数组。
  3. 可序列化状态:Feed 基类(src/core/feed.ts)提供了 serialize() / deserialize() 方法,配合 @Expose() 装饰器标记的 moreAvailable 与 nextMaxId,可以把“翻到第几页”的进度持久化为 JSON 字符串,之后在任意进程恢复后继续翻页。这对“分批处理百万级粉丝列表”的场景非常关键。

另外,Feed 基类的 observable() 方法内建了基于 @lifeomic/attempt 的重试策略(默认 maxAttempts: 10、指数退避、60s 起步最长 5min,见 feed.ts#L10-L17),且对 400/429/500/502 状态码采取“等待重试”而非直接失败的策略;只要 isMoreAvailable() 为真,RxJS 流就会持续拉取下一页,直到游标耗尽。

创建 Feed:ig.feed.accountFollowers(options)

该 Feed 通过 FeedFactory.accountFollowers() 创建,实现在 src/core/feed.factory.ts#L56-L66。参数有三种传法:

public accountFollowers(
  options?:
    | string
    | number
    | Partial<Pick<AccountFollowersFeed, 'searchSurface' | 'order' | 'query' | 'enableGroups' | 'id'>>,
): AccountFollowersFeed {
  return plainToClassFromExist(new AccountFollowersFeed(this.client), {
    id: options && typeof options !== 'object' ? options : this.client.state.cookieUserId,
    ...(typeof options === 'object' ? options : undefined),
  });
}
  • 传入 string | number:直接作为目标账号的 id(用户数字 ID 或用户名);
  • 传入对象:可指定 searchSurface、order、query、enableGroups、id 的任意子集;
  • 不传或传对象而未含 id:默认使用当前登录账号(cookieUserId),即查询“我自己的粉丝”。

Feed 自身的可调参数及默认值(见 account-followers.feed.ts#L6-L14):

参数 类型 默认值 说明
id number | string cookieUserId 目标账号 ID
searchSurface string 未定义 搜索场景标识,透传给 search_surface 查询参数
order 'default' 'default' 排序方式。源码注释明确“only 'default' seems to work”,即目前仅 default 可用
query string '' 用于在粉丝列表内做模糊搜索的关键字,透传给 query 参数
enableGroups boolean true 是否启用分组展示,透传给 enable_groups 参数

实战:完整示例解析

仓库提供了可直接参考的示例 examples/account-followers.feed.example.ts,覆盖了“原始响应、条目遍历、自动分页、状态序列化、RxJS 订阅”五种用法:

import 'dotenv/config';
import { IgApiClient } from '../src';

(async () => {
  const ig = new IgApiClient();
  ig.state.generateDevice(process.env.IG_USERNAME);
  ig.state.proxyUrl = process.env.IG_PROXY;
  const auth = await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);

  const followersFeed = ig.feed.accountFollowers(auth.pk);

  const wholeResponse = await followersFeed.request();
  // wholeResponse 即 AccountFollowersFeedResponse:
  // 可访问 sections/users/big_list/next_max_id/page_size/status 等任意原始字段

  const items = await followersFeed.items();          // 第一页,AccountFollowersFeedResponseUsersItem[]
  const thirdPageItems = await followersFeed.items(); // Feed 有状态且自动翻页:再次调用返回下一页

  const feedState = followersFeed.serialize();       // 序列化当前分页游标(nextMaxId/moreAvailable)
  followersFeed.deserialize(feedState);
  const fourthPageItems = await followersFeed.items();

  // 使用 RxJS 流一次性订阅全部结果,内建重试与限流退避
  followersFeed.items$.subscribe(
    followers => console.log(followers),
    error => console.error(error),
    () => console.log('Complete!'),
  );
})();

其中两点值得注意:

  • Feed 是有状态的:同一个 followersFeed 实例每调用一次 items() 都会消费一页并前移游标,因此“连续两次 items()”就是“翻到下一页”,无需手动传 max_id;
  • serialize()/deserialize():把游标进度存下来,可在长时间任务中断后恢复,避免从头重新拉取。

另一个典型应用是 examples/unfollow-users.example.ts:先用 accountFollowers(ig.state.cookieUserId) 与 accountFollowing(ig.state.cookieUserId) 分别遍历全部粉丝与全部关注,基于 username 构造 Set 求差集得到“没回关的人”,再逐个调用 ig.friendship.destroy(user.pk) 并在每次操作间随机延迟 1~7 秒以规避频率限制。这个模式展示了 AccountFollowersFeedResponse 在实际运营类脚本中的完整价值——粉丝列表(users 字段 + next_max_id 游标)是“互关分析、粉丝导出、批量维护”类功能的数据基础。

相关文档与源码索引

需要说明的前提:order 参数目前源码注释确认仅 default 可用;sections 在该接口的类型中被声明为 null,若实际返回中出现非空内容,应以运行时数据为准。所有字段语义均以当前仓库源码为准,Instagram 私有接口结构可能随服务端版本变化,使用第三方私有接口时请遵守平台服务条款。

登录后查看全文
instagram-private-api