instagram-private-api 数据模型解析:PostsInsightsFeedResponseUser 接口与帖子洞察(Posts Insights)数据链路
后端【免费下载链接】instagram-private-apiNodeJS Instagram private API SDK. Written in TypeScript.项目地址https://gitcode.com/gh_mirrors/in/instagram-private-api点击查看免费下载导读PostsInsightsFeedResponseUser是 instagram-private-apiNodeJS Instagram 私有 API SDKTypeScript 编写中帖子洞察Posts Insights响应体的用户节点接口它位于洞察数据嵌套结构的第二层承载当前登录商业账号的id与business_manager业务容器。本文将围绕该接口展开结合仓库源码posts-insights.feed.response.ts、posts-insights.feed.ts 等完整还原其数据结构、访问路径、底层 GraphQL 请求与分页机制帮助你读懂并实际取用账号帖子的曝光、触达、互动、分享、购物点击等洞察指标。接口定义速览PostsInsightsFeedResponseUser在文档中定义于 src/responses/posts-insights.feed.response.ts:7-10原型非常简单仅包含两个属性export interface PostsInsightsFeedResponseUser { id: string; business_manager: PostsInsightsFeedResponseBusiness_manager; }属性类型必填语义idstring是当前查询账号的用户 ID即登录用户 ID与client.state.cookieUserId对应business_managerPostsInsightsFeedResponseBusiness_manager是商业管理对象其下挂载热门帖子top posts洞察数据单元其中business_manager是关键的嵌套容器接口 PostsInsightsFeedResponseBusiness_manager 在 posts-insights.feed.response.ts:11-13 中定义目前仅有一个成员top_posts_unit。也就是说PostsInsightsFeedResponseUser是整个洞察响应体中通往帖子级指标的唯一入口。嵌套响应结构从根对象到用户节点PostsInsightsFeedResponseUser不是孤立存在的它嵌在一棵层级很深的响应树中。在 posts-insights.feed.response.ts 中定义的完整结构为PostsInsightsFeedResponseRootObject └── data: PostsInsightsFeedResponseData └── user: PostsInsightsFeedResponseUser // ← 本文主角 ├── id: string └── business_manager: PostsInsightsFeedResponseBusiness_manager └── top_posts_unit: PostsInsightsFeedResponseTop_posts_unit └── top_posts: PostsInsightsFeedResponseTop_posts ├── edges: PostsInsightsFeedResponseEdgesItem[] │ └── node: PostsInsightsFeedResponseNode // 每篇帖子的洞察指标 └── page_info: PostsInsightsFeedResponsePage_info // 分页游标信息PostsInsightsFeedResponseTop_posts定义位置持有edges数组与page_infoPostsInsightsFeedResponseEdgesItem定义位置是 GraphQL 边edge结构包含node帖子节点与可选cursorPostsInsightsFeedResponsePage_info定义位置提供end_cursor与has_next_page两个分页字段。这条路径也被PostsInsightsFeed的items()方法直接印证。在 posts-insights.feed.ts:12-15 中async items(): PromisePostsInsightsFeedResponseEdgesItem[] { const body await this.request(); return body.data.user.business_manager.top_posts_unit.top_posts.edges; }这正是PostsInsightsFeedResponseUser在真实调用链中的访问位置data.user之后必须经由business_manager → top_posts_unit → top_posts才能抵达帖子列表。调用入口Feed 工厂与选项参数PostsInsightsFeedResponseUser类型的响应由PostsInsightsFeed生成而该 Feed 实例由统一工厂方法创建。在 feed.factory.ts:210-212 中public postsInsightsFeed(options: PostsInsightsFeedOptions) { return plainToClassFromExist(new PostsInsightsFeed(this.client), { options }); }典型的调用方式为const ig new IgApiClient(); // ...登录后 const insightsFeed ig.feed.postsInsightsFeed({ timeframe: ONE_MONTH, dataOrdering: ENGAGEMENT_COUNT, postType: ALL, }); const edges await insightsFeed.items(); const user insightsFeed.state?.data?.user; // PostsInsightsFeedResponseUserPostsInsightsFeedOptions定义在 src/types/insights.options.ts:11-28三个必填选项的取值范围如下选项允许值说明timeframeONE_WEEK、ONE_MONTH、THREE_MONTHS、SIX_MONTHS、ONE_YEAR、TWO_YEARS洞察统计的时间范围dataOrderingCALL、COMMENT_COUNT、EMAIL、ENGAGEMENT_COUNT、FOLLOW、GET_DIRECTIONS、IMPRESSION_COUNT、LIKE_COUNT、PROFILE_VIEW、REACH_COUNT、SAVE_COUNT、SHARE_COUNT、TEXT、BIO_LINK_CLICK帖子排序所依据的指标维度postTypeALL、IMAGE、VIDEO、CAROUSEL_V2、SHOPPING过滤的帖子类型这些选项会被展开合并进 GraphQL 请求变量见下文直接影响返回的edges内容与顺序。底层请求AdsRepository.graphQL 与 GraphQL 文档 IDPostsInsightsFeed的request()通过this.client.ads.graphQL(...)发起请求posts-insights.feed.ts:17-34其中携带了固定的查询面与文档 IDasync request(): PromisePostsInsightsFeedResponseRootObject { const body await this.client.ads.graphQLPostsInsightsFeedResponseRootObject({ surface: { friendlyName: IgInsightsPostGridSurfaceQuery }, documentId: 1981884911894608, variables: { count: 15, cursor: this.nextCursor, IgInsightsGridMediaImage_SIZE: 256, queryParams: { access_token: , id: this.client.state.cookieUserId, }, ...this.options, }, }); this.state body; return body; }关键点每页固定取 15 条count: 15与cursor分页游标共同控制页大小与翻页查询主体账号 ID 取自client.state.cookieUserId这就是响应中PostsInsightsFeedResponseUser.id的语义来源封面图尺寸固定为IgInsightsGridMediaImage_SIZE: 256friendlyName为IgInsightsPostGridSurfaceQuerydocumentId为1981884911894608两者对应 Instagram 内部 GraphQL 查询标识。ads.graphQL的实现位于 src/repositories/ads.repository.ts:5-26它最终向/api/v1/ads/graphql/发起POST请求并组装出如下表单字段form: { access_token: options.accessToken, fb_api_caller_class: RelayModern, fb_api_req_friendly_name: options.surface.friendlyName, doc_id: options.documentId, variables: JSON.stringify(options.variables), }同时在qs中携带locale取自client.state.language与vc_policy: insights_policysurface名称在传入时还会作为查询参数追加。由此可见整条链路是Feed 配置选项 → GraphQL 变量 → AdsRepository 组装请求 → 解析响应树 → 从data.user.business_manager.top_posts_unit.top_posts.edges取数。分页机制end_cursor 与 has_next_pagePostsInsightsFeed继承了 src/core/feed.ts 的抽象 Feed 基类其分页状态由statesetter 驱动posts-insights.feed.ts:36-40protected set state(response: PostsInsightsFeedResponseRootObject) { const { end_cursor, has_next_page } response.data.user.business_manager.top_posts_unit.top_posts.page_info; this.nextCursor end_cursor; this.moreAvailable has_next_page; }每次request()后将page_info.end_cursor写入nextCursor将has_next_page写入moreAvailable。Feed 基类提供了两种消费方式直接循环调用items()并配合isMoreAvailable()判断是否还有下一页使用items$/observable()feed.ts:18-61以 RxJS 流的方式持续拉取直到has_next_page为false。该流内置基于lifeomic/attempt的重试机制默认delay/minDelay: 60000、maxAttempts: 10、jitter: true并对 400/429/500/502 这类请稍后再试的响应自动等待。此外基类的serialize()/deserialize()/toPlain()借助class-transformer支持将 Feed 状态含nextCursor、moreAvailable序列化为字符串并在后续恢复便于中断后断点续拉。帖子节点字段深入PostsInsightsFeedResponseNodeitems()返回的PostsInsightsFeedResponseEdgesItem[]中真正携带指标的是nodePostsInsightsFeedResponseNode定义位置。理解它有助于正确消费PostsInsightsFeedResponseUser之下的数据。字段与可选性如下字段类型说明instagram_media_idstring?帖子媒体 IDinstagram_media_typestring?媒体类型标识imagePostsInsightsFeedResponseImage?封面图信息含uricreation_timenumber?帖子创建时间时间戳comment_count/like_count/save_count/engagementnumber?评论、点赞、收藏与总互动数video_view_countnull?视频观看数该响应中恒为null注意处理shopping_product_click_count/shopping_outbound_click_countnumber?购物商品点击与站外点击次数inline_insights_nodePostsInsightsFeedResponseInline_insights_node?内联洞察节点idstring?节点 ID__typenamestringGraphQL 类型名必填name/valuestring?/number?指标名 / 指标值用于排序维度展开inline_insights_node定义位置进一步包含metrics: PostsInsightsFeedResponseMetricsimpression_count曝光、reach_count触达、owner_account_follows_count、owner_profile_views_count、profile_actions含actions.edges动作列表、share_count含tray.value分享数state: string指标状态标识。因此若要统计某账号近一个月的帖子曝光与互动表现可以直接遍历edges[].node聚合inline_insights_node.metrics与engagement等字段若要按dataOrdering指定的维度排序则关注节点上的name/value对。完整使用示例结合以上链路一段可运行的完整示例以已登录客户端为前提大致如下import { IgApiClient } from instagram-private-api; async function fetchPostsInsights() { const ig new IgApiClient(); ig.state.generateDevice(your_username); await ig.account.login(your_username, your_password); const feed ig.feed.postsInsightsFeed({ timeframe: ONE_MONTH, dataOrdering: ENGAGEMENT_COUNT, postType: ALL, }); do { const edges await feed.items(); for (const edge of edges) { const node edge.node; console.log({ mediaId: node.instagram_media_id, type: node.instagram_media_type, likeCount: node.like_count, commentCount: node.comment_count, saveCount: node.save_count, engagement: node.engagement, impressions: node.inline_insights_node?.metrics.impression_count, reach: node.inline_insights_node?.metrics.reach_count, shares: node.inline_insights_node?.metrics.share_count.tray.value, }); } } while (feed.isMoreAvailable()); } fetchPostsInsights().catch(console.error);注意PostsInsightsFeed面向的是**商业账号Business**的帖子洞察查询主体为用户自身若账号不具备商业主页权限或相应权限不足请求可能返回异常实际可用性以登录账号状态为准。总结PostsInsightsFeedResponseUser虽只有id与business_manager两个字段却是整个 Posts Insights 数据结构的枢纽id标识查询主体business_manager → top_posts_unit → top_posts串联起帖子列表、节点指标与分页游标。结合 Feed 工厂、GraphQL 请求实现 与 响应类型定义即可完整掌握从配置选项、请求发出到指标取用的整条数据链路为基于洞察数据的运营分析、报表生成等场景提供坚实的数据基础。赞分享后端【免费下载链接】instagram-private-apiNodeJS Instagram private API SDK. Written in TypeScript.项目地址https://gitcode.com/gh_mirrors/in/instagram-private-api点击查看免费下载相关推荐AutoHotkey DllCall 深度解析如何调用任意 DLL 函数突破语言边界 AutoHotkey DllCall 深度解析如何调用任意 DLL 函数突破语言边界 AutoHotkey DllCall 详解 本文面向 Windo后端instagram-private-api 帖子洞察PostsInsightsFeedResponseProfile_actions 接口解析与主页行为数据实战instagram private api 帖子洞察PostsInsightsFeedResponseProfile_actions 接口解析与主页行为数据实后端instagram-private-api 账号数据洞察Insights响应模型解读InsightsServiceAccountResponseGraph 接口详解instagram private api 账号数据洞察Insights响应模型解读InsightsServiceAccountResponseGraph后端上一篇backtesting.py 快速上手双均线策略到参数热力图一个下午跑通的量化回测全流程下一篇DataLoader缓存算法终极对比LRU vs Map的性能和内存使用深度分析创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考