来自 YouTube Data API v3 的意外 pageInfo [英] Unexpected pageInfo from YouTube Data API v3

查看:29
本文介绍了来自 YouTube Data API v3 的意外 pageInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 YouTube Data API v3 (https://developers.google.com/youtube/v3/docs/search/list).
我成功获得了一些信息,但我发现 pageInfo 的结果不是我所期望的.

这是我得到的查询和回复:

https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC5fslQUCCo3OE1EaA1n5ZmQ&key=...&publishedAfter=2019-01-15T08:09:38Z&publishedBefore=2019-01-15T10:09:38Z&order=date .

{种类":youtube#searchListResponse",etag":iLvO2LgGDksqADNU-_xPwG4QMCU",nextPageToken":CAQAA",地区代码":JP",页面信息":{总结果":49,每页结果":5},项目":[{种类":youtube#searchResult",etag":Adtll-9lbQDiKGJEkBb2IEjPfjw",身份证":{种类":youtube#video",videoId":d4Z7MZo3-Ac"}}]}

我为publishAfterpublishBefore 指定了日期字符串,以便结果只包含单个视频信息.
我猜 pageInfo.totalResultpageInfo.resultPerPage 都是 1.
与我的预期相反,pageInfo.totalResult 是 49,pageInfo.resultsPerPage 是 5.

什么描述了pageInfo?我的理解有误吗?

解决方案

以下是您的两个问题的答案:

1.为什么 pageInfo.resultsPerPage5?

此属性的规范如下:

<块引用>

pageInfo.resultsPerPage(整数)
API 响应中包含的结果数.

可以将本规范解读为(我改写上述文本):从 API 获得的结果集中的项目数,即 JSON 数组 items 的元素数.>

不幸的是,这与现实不符.根据我的经验,pageInfo.resultsPerPage 只是请求参数 maxResults 的值:

<块引用>

maxResults(无符号整数)
maxResults 参数指定应在结果集中返回的最大项目数.可接受的值为 050(含).默认值为 5.

由于你上面的API调用URL没有指定maxResults,所以这个参数取默认值;因此 pageInfo.resultsPerPage 变为 5.

再说明一点:这个属性的官方说明确实有误导性;这个问题很可能作为文档错误报告给 Google(通过其自己的问题跟踪网站).>

2.为什么 pageInfo.totalResults49 而不是 1?

这个属性的说明如下(下面的重点是我的):

<块引用>

pageInfo.totalResults(整数)
结果集中的结果总数.请注意,该值是近似值,可能不代表准确值.此外,最大值为 1,000,000.

您不应使用此值来创建分页链接.而是使用 nextPageTokenprevPageToken 属性值来确定是否显示分页链接.

根据本规范,pageInfo.totalResults 不应被视为用户可以依赖的精确数量.这一特性也得到了 Google 工作人员的正式确认.

此外,如果对您的 URL 进行一些试验,消除两个日期请求参数 publishedAfterpublishedBefore,您将看到 pageInfo.totalResults 变得不稳定:连续多次重复相同的 API 调用(彼此之间仅相隔几秒),我从未从 API 中获得相同的值,但是,交替,13741375.

I've tried to search videos using YouTube Data API v3 (https://developers.google.com/youtube/v3/docs/search/list).
I successfully got some information, but I figured out the result of pageInfo wasn't something I expected.

Here is the query and the response I got:

https://www.googleapis.com/youtube/v3/search?part=id&channelId=UC5fslQUCCo3OE1EaA1n5ZmQ&key=...&publishedAfter=2019-01-15T08:09:38Z&publishedBefore=2019-01-15T10:09:38Z&order=date .

{
  "kind": "youtube#searchListResponse",
  "etag": "iLvO2LgGDksqADNU-_xPwG4QMCU",
  "nextPageToken": "CAUQAA",
  "regionCode": "JP",
  "pageInfo": {
    "totalResults": 49,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "Adtll-9lbQDiKGJEkBb2IEjPfjw",
      "id": {
        "kind": "youtube#video",
        "videoId": "d4Z7MZo3-Ac"
      }
    }
  ]
}

I specified date strings to publishAfter and publishBefore so that the result include single video information only.
I guessed both pageInfo.totalResult and pageInfo.resultPerPage were 1.
Contrary to my anticipation, pageInfo.totalResult was 49 and pageInfo.resultsPerPage was 5.

What describes pageInfo? Is my understanding incorrect?

解决方案

Here are the answers to your two questions:

1. Why pageInfo.resultsPerPage is 5?

The specification of this property is the following:

pageInfo.resultsPerPage (integer)
The number of results included in the API response.

One may read this specification as (am rephrasing the text above): the number of items in the result set obtained from the API, i.e the number of elements of the JSON array items.

Unfortunately, this does not correspond to reality. In my experience, pageInfo.resultsPerPage is simply the value of the request parameter maxResults:

maxResults (unsigned integer)
The maxResults parameter specifies the maximum number of items that should be returned in the result set. Acceptable values are 0 to 50, inclusive. The default value is 5.

Since your API invoking URL above does not specify maxResults, this parameter is taken as having the default value; hence pageInfo.resultsPerPage gets to be 5.

One more remark: indeed the official specification of this property is misleading; this issue may well be reported to Google (via its own issue tracker site) as a documentation bug.

2. Why pageInfo.totalResults is 49 and is not 1?

The specification of this property is the following (the emphasis below is mine):

pageInfo.totalResults (integer)
The total number of results in the result set. Please note that the value is an approximation and may not represent an exact value. In addition, the maximum value is 1,000,000.

You should not use this value to create pagination links. Instead, use the nextPageToken and prevPageToken property values to determine whether to show pagination links.

According to this specification, pageInfo.totalResults is not to be taken as a precise quantity that the user can rely upon. This peculiarity is also confirmed officially by Google's staff.

Moreover, if experimenting a bit with your URL, eliminating the two date request parameters publishedAfter and publishedBefore, you'll see that the value of pageInfo.totalResults becomes unstable: repeating the same API call several times in a row (only seconds apart one from another), I never got the same value of out of the API, but, alternating, 1374 and 1375.

这篇关于来自 YouTube Data API v3 的意外 pageInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆