instagram-private-api:TopicalExploreFeedResponseAdditional_candidates 接口解析——主题探索信息流中的视频首帧候选资源
instagram-private-api:TopicalExploreFeedResponseAdditional_candidates 接口解析——主题探索信息流中的视频首帧候选资源
本文以 TopicalExploreFeedResponseAdditional_candidates 这一响应类型文档为主体,结合 src/responses/topical-explore.feed.response.ts 的完整源码定义、TopicalExploreFeed 的请求实现以及 FeedFactory 的工厂方法,系统讲解该接口在 Instagram 主题探索(Topical Explore)信息流响应中的挂载位置、字段结构与实际消费方式,帮助读者准确解析 image_versions2.additional_candidates 下的首帧图片资源。
一、接口概览:它属于哪个模块、定义在哪里
TopicalExploreFeedResponseAdditional_candidates 是 instagram-private-api 项目 responses 模块中暴露的一个 TypeScript interface,用于描述 Instagram 主题探索信息流响应中、媒体图片版本(image_versions2)附带的“额外候选首帧图片”资源。按照官方生成的接口文档,该接口:
- 命名空间:
responses.TopicalExploreFeedResponseAdditional_candidates; - 归属文档入口:responses 模块索引;
- 源码定义位置:src/responses/topical-explore.feed.response.ts#L294-L297。
从源码看,其完整定义仅有两个成员:
// 见 src/responses/topical-explore.feed.response.ts#L294-L297
export interface TopicalExploreFeedResponseAdditional_candidates {
igtv_first_frame: TopicalExploreFeedResponseIgtv_first_frame;
first_frame: TopicalExploreFeedResponseFirst_frame;
}
这与接口文档中 “Properties: first_frame / igtv_first_frame” 的目录结构完全一致。两个属性均为必选(non-optional),这意味着:只要响应里出现了 additional_candidates 对象,就应当能同时取到 igtv_first_frame 与 first_frame 两项首帧候选。
二、属性详解:first_frame 与 igtv_first_frame
2.1 first_frame
文档定义为:
first_frame:
TopicalExploreFeedResponseFirst_frame
对应源码中 TopicalExploreFeedResponseFirst_frame:
| 字段 | 类型 | 含义(结合候选资源惯例理解) |
|---|---|---|
width |
number |
首帧图片宽度(像素) |
height |
number |
首帧图片高度(像素) |
url |
string |
首帧图片资源地址 |
scans_profile |
string |
扫描策略标识 |
estimated_scans_sizes |
number[] |
预估扫描尺寸列表 |
2.2 igtv_first_frame
文档定义为:
igtv_first_frame:
TopicalExploreFeedResponseIgtv_first_frame
对应源码中 TopicalExploreFeedResponseIgtv_first_frame,其五个字段(width、height、url、scans_profile、estimated_scans_sizes)与 First_frame 完全同构,专用于 IGTV 形态的首帧缩略图。
从源码结构看,这两个接口的字段集与同文件中普通的 TopicalExploreFeedResponseCandidatesItem(即 candidates 里的候选项:width/height/url/scans_profile?/estimated_scans_sizes?)高度相似,可以推断 additional_candidates 是候选资源列表(candidates)之外的补充候选集合:当主候选不足以覆盖某些展示场景(如视频首帧、IGTV 首帧)时,服务端会额外下发这两张首帧图片供客户端按场景选用。
三、接口在响应树中的挂载位置:从根对象到 additional_candidates
要理解这个“小接口”的实际价值,必须看清它被谁引用。从 topical-explore.feed.response.ts 的整体结构看,引用链如下:
TopicalExploreFeedResponseRootObject # L1-L9,顶层响应
└─ sectional_items: SectionalItemsItem[] # L10-L15,分节内容
└─ layout_content: Layout_content # L16-L20
├─ two_by_two_item? (channel / igtv) # L21-L24
├─ fill_items?: FillItemsItem[] # L181-L183
└─ medias?: MediasItem[] # L256-L258
└─ media: Media # L34-L98
└─ image_versions2?: Image_versions2 # L56, L136-L139
├─ candidates: CandidatesItem[] # 主候选列表
└─ additional_candidates?: Additional_candidates # L138 ← 本文接口
关键点有三个:
- 挂载点是可选的。TopicalExploreFeedResponseImage_versions2 中
additional_candidates声明为additional_candidates?: ...(对应 接口文档 中标注的 Optional)。这与Additional_candidates内部字段必选形成“外层可选、内层必选”的组合——消费方必须先判空再访问。 - 两个入口共用同一结构。
Media.image_versions2(L56)与轮播子项 TopicalExploreFeedResponseCarouselMediaItem.image_versions2(L265)都使用同一个Image_versions2,因此卡片内每一张轮播图都可能有各自的首帧候选。 - 与 IGTV 浏览/频道响应的同名词对比。在同目录的 igtv.browse.feed.response.ts 与 igtv.channel.feed.response.ts 中同样存在
additional_candidates,但那里是必选字段。从源码结构看,这说明首帧候选资源是 IGTV 类内容的稳定特征,而在主题探索信息流中则是按媒体类型按需下发的可选特征。
解析示例(TypeScript):
import {
TopicalExploreFeedResponseMediasItem,
} from './src/responses';
function extractFirstFrame(media: TopicalExploreFeedResponseMediasItem['media']) {
// image_versions2 本身也是可选的(Media 中声明为 image_versions2?)
const versions = media.image_versions2;
if (!versions) return null;
// additional_candidates 为 Optional,必须先判空
const extra = versions.additional_candidates;
if (!extra) return null;
// 内部字段是必选的,可直接使用
return {
firstFrame: extra.first_frame.url, // 普通首帧
igtvFirstFrame: extra.igtv_first_frame.url, // IGTV 首帧
width: extra.first_frame.width,
height: extra.first_frame.height,
};
}
四、数据从哪里来:TopicalExploreFeed 的请求参数与分页机制
TopicalExploreFeedResponseAdditional_candidates 的数据由 TopicalExploreFeed 发起请求获得。该 Feed 继承自抽象基类 Feed,泛型签名为 Feed<TopicalExploreFeedResponseRootObject, TopicalExploreFeedResponseSectionalItemsItem>,即一次请求返回顶层响应,items() 从 sectional_items 中取出条目。
4.1 请求端点与查询参数
TopicalExploreFeed.request()(src/feeds/topical-explore.feed.ts#L31-L51)向私有端点 GET /api/v1/discover/topical_explore/ 发送请求,完整查询参数如下:
| 参数 | 取值 | 说明 |
|---|---|---|
is_prefetch |
false |
非预取请求 |
omit_cover_media |
true |
省略封面媒体 |
max_id |
this.nextMaxId |
分页游标,由上一次响应的 next_max_id 写入 |
module |
默认 'explore_popular' |
客户端模块标识,来自 IgAppModule 类型 |
reels_configuration |
'hide_hero' |
Reels 展示配置 |
use_sectional_payload |
true |
使用分节载荷,对应响应中的 sectional_items |
timezone_offset |
this.client.state.timezoneOffset |
客户端时区偏移 |
lat / lng |
this.lat / this.lng |
可选经纬度,用于地理位置相关推荐 |
cluster_id |
默认 'explore_all:0' |
探索分类(cluster)标识,源码注释明确“Change this to set the category” |
session_id |
chance.guid({ version: 4 }) 生成 |
会话 ID,每次实例化随机生成一个 UUID v4 |
include_fixed_destinations |
true |
包含固定目标 |
其中 clusterId(默认 explore_all:0)与 lat/lng 是控制返回内容主题的关键开关;而响应中的每条媒体(含其 image_versions2.additional_candidates)会随这些参数变化而不同。
4.2 通过 FeedFactory 创建
工厂方法 FeedFactory.topicalExplore() 允许覆盖 sessionId、clusterId、lat、lng、module 五个属性:
import { IgApiClient } from './src';
(async () => {
const ig = new IgApiClient();
ig.state.generateDevice(process.env.IG_USERNAME);
const auth = await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
// 指定探索分类与地理位置
const feed = ig.feed.topicalExplore({
clusterId: 'explore_all:0',
lat: '31.2304',
lng: '121.4737',
});
// 单页请求:TopicalExploreFeedResponseRootObject
const page = await feed.items();
for (const section of page) {
// 常规媒体列表位于 layout_content.medias
const medias = section.layout_content?.medias ?? [];
for (const { media } of medias) {
const extra = media.image_versions2?.additional_candidates;
if (extra) {
console.log(media.pk, extra.first_frame.url, extra.igtv_first_frame.url);
}
}
}
})();
4.3 分页、重试与状态序列化
由于 TopicalExploreFeed 直接继承 Feed 基类,以下能力对该接口数据的获取同样生效:
- 自动分页:state setter 把响应的
next_max_id存入nextMaxId、more_available存入moreAvailable,每次调用items()自动携带下一页游标; - RxJS 流式消费:
feed.items$暴露Observable,Feed.observable() 内部使用@lifeomic/attempt做指数退避重试(默认maxAttempts: 10、minDelay: 60000、带抖动),并对 400/429/500/502 状态码执行“等待重试”而非立即中止; - 状态持久化:
serialize()/deserialize()可将游标与分页状态序列化,跨进程恢复拉取(参考 examples/account-followers.feed.example.ts 的用法,该模式同样适用于 TopicalExploreFeed)。
五、与相近响应类型的对照
在仓库的 responses 模块内,命名形如 *Additional_candidates 的接口共有三处(可通过搜索 additional_candidates 确认):
| 接口 | 定义位置 | 外层声明方式 |
|---|---|---|
TopicalExploreFeedResponseAdditional_candidates(本文主角) |
topical-explore.feed.response.ts#L294-L297 | Image_versions2#L138 中为 optional |
IgtvBrowseFeedResponseAdditional_candidates |
igtv.browse.feed.response.ts#L126 | 必选 |
IgtvChannelFeedResponseAdditional_candidates |
igtv.channel.feed.response.ts#L60 | 必选 |
这提示一个实践要点:处理主题探索响应时不能假设 additional_candidates 必然存在,而处理纯 IGTV 浏览/频道响应时可以按必选字段处理。编写类型收窄代码时,建议统一采用“可选链 + 判空”的写法,与 Image_versions2 的 Optional 声明保持一致。
六、小结与延伸阅读
TopicalExploreFeedResponseAdditional_candidates 虽然只有 first_frame 与 igtv_first_frame 两个必选属性,但它处在主题探索信息流响应解析的关键路径上:从 RootObject.sectional_items[].layout_content.medias[].media.image_versions2 出发,它是获取视频/IGTV 首帧图片(URL、宽高、扫描策略)的唯一结构化入口,且仅在服务端下发时存在,解析代码必须做可选性处理。
延伸阅读(均为仓库内相对路径):
- TopicalExploreFeed 类文档:
clusterId、sessionId、lat/lng、module属性及items()/request()方法签名; - TopicalExploreFeedResponseImage_versions2 文档:
additional_candidates的 Optional 声明出处; - TopicalExploreFeedResponseFirst_frame 与 TopicalExploreFeedResponseIgtv_first_frame:两个首帧候选接口的字段文档;
- src/responses/topical-explore.feed.response.ts:完整响应类型树,含
Media、CarouselMediaItem、Channel、Igtv等兄弟接口; - Feed 基类:分页、重试、序列化机制的通用实现。
需要说明的是:本文所有字段、默认值与调用关系均以当前仓库源码为准;explore_popular、explore_all:0 等取值是 Feed 类的默认常量,服务端返回内容(包括是否附带 additional_candidates)会随 Instagram 私有接口版本变化而波动,实际集成时建议以真实登录后的响应日志校准。