TotalResults数量与YouTube v3搜索API中返回的实际结果不符

时间:2017-04-19 22:41:31

标签: youtube youtube-api youtube-data-api

我们正在使用youtube v3搜索API。我们在“totalResults”计数和response.items字段中返回的项目列表中不匹配。我在请求中请求了50个视频。返回的响应显示totalResults计为65但响应有效负载只有3个视频。

API:www.googleapis.com/youtube/v3/search/list

请求有效负载

{key=AIzaSyDXXQAtfbLwl6ZVoDDJPeeHKiWz3SzfOmM, maxResults=50, order=date, part=snippet, publishedAfter=2017-03-20T23:59:59.999-04:00, publishedBefore=2017-04-19T23:59:59.001-04:00, q=( ( "Wood Mackenzie" OR "Wood Mac" OR Woodmac OR @woodmackenzie ) AND NOT (fleetwood OR "fleet wood" OR 9WoodMac ) )  , type=video}

响应有效负载

{
 "kind": "youtube#searchListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/HTW4WQODZeERIv51VrRfOhir8dg\"",
 "nextPageToken": "CDIQAA",
 "regionCode": "US",
 "pageInfo": {
  "totalResults": 65,
  "resultsPerPage": 50
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/lnNNrToZHGTehIEJ6bvEx5RZMo4\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "obzlB21t904"
   },
   "snippet": {
    "publishedAt": "2017-04-14T11:38:08.000Z",
    "channelId": "UC60Sa1LQjgy_a-K8ThN4eQw",
    "title": "Fleet wood mac Live at Leeds",
    "description": "",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/obzlB21t904/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "KEVIN MARSHALL",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/uYuuTAL3vtnFI1WOYjth0cso2Xo\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "gHAn0SbwKMg"
   },
   "snippet": {
    "publishedAt": "2017-04-06T22:17:31.000Z",
    "channelId": "UC3_MxBSSbmGznoSTgoBQ7_w",
    "title": "Beat Club: British Invasion 60's, Pt-2; Animals-Move-Fleet Wood Mac-J.Mayall-Who-Pacemakers-Kinks",
    "description": "Eric Burdon & Animals: C.C. Rider 0:01 The Move: Wild Tiger Woman 3:55 Fleet Wood Mac: Dragonfly 6:26 John Mayall: My Pretty Girl 9:47 Thunderclap ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/gHAn0SbwKMg/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "ROCK&POPS1_koba",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/tapelxOG4Q3gZRJCR2qaVdt-ZO8\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "5GvLbFBdcF4"
   },
   "snippet": {
    "publishedAt": "2017-04-02T04:48:47.000Z",
    "channelId": "UC6fn383TI1HVDYW4dSEIogQ",
    "title": "\"Dreams\" by fleet wood mac (vocal cover)",
    "description": "I do not own this song. I do not make a profit off of this song. It belongs to the person(s) who wrote it. Please subscribe and comment below any suggestions?!",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/5GvLbFBdcF4/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Musikallitee",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

1 个答案:

答案 0 :(得分:3)

正如documentation本身所述,totalResults是:

  

<强>整数

     

结果集中的结果总数。请注意,该值是近似值,可能不代表精确值。此外,最大值为1,000,000。

     

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

该值仅是近似值/估计值,但不是返回的items总数的准确值。我使用与您相同的详细信息对API进行了测试,其值从63变为6465

尽管如此,你的代码没有任何问题。这只是预期的行为。遗憾的是,totalResults的值基于何处或最佳使用位置的地方没有说明。最好继续执行不依赖于此参数的实现。