1

我正在尝试使用 YouTube 数据 API 通过 Freebase 主题搜索视频。它适用于具有一个主题的简单搜索,但不适用于具有 2 个参数的搜索:主题 id 和“publishedAfter”日期。

例如,要搜索关于 Richard Branson (Freebase id = /m/0n839) 的视频,请使用 youtube 数据 api 站点: https ://developers.google.com/youtube/v3/docs/search/list#try-it参数:

部分:片段

主题ID:/m/0n839

显示 2165 个视频的结果

但是当您尝试搜索有关 id = /m/0n839 的视频时

部分:片段

主题ID:/m/0n839

发表时间:2013-05-21T21:47:38Z

回复显示 147 个视频,但回复中仅包含 3 个视频。在响应中,您可以看到 3 个视频 id 是 J6PY5vxLU8Y、SCkFwpW3kiE、CBvDp0i8Iok。

{
 "kind": "youtube#searchListResponse",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/_-rYO0M0nvpPO6QN7DPFGEYa9ho\"",
 "pageInfo": {
     "totalResults": 147,
     "resultsPerPage": 5
     },
 "nextPageToken": "CAUQAA",
 "items": [
     {
     "kind": "youtube#searchResult",
     "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/3nxQ-ejnv0qekcbyq09HD2RQt_w\"",
     "id": {
         "kind": "youtube#video",
         "videoId": "J6PY5vxLU8Y"
     },
     "snippet": {
         "publishedAt": "2013-05-22T09:58:34.000Z",
         "channelId": "UCimPiDCqxvfqwVJltL4YzHg",
         "title": "Bono, Richard Branson, and Olivia Wilde Joined Matt Damon's Strike!",
         "description": "Support the strike: http://strikewithme.org/ Millions of celebrities have joined Matt Damon's \"Toilet Strike\" protesting the lack of access to safe water and...",
         "thumbnails": {
             "default": {
                 "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/default.jpg"
                 },
             "medium": {
                 "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/mqdefault.jpg"
                 },
             "high": {
                 "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/hqdefault.jpg"
                 }
             },
         "channelTitle": "water"
    }
},
{
 "kind": "youtube#searchResult",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/gNslTbFkShGLlUBcXgHw7q9tuJc\"",
 "id": {
     "kind": "youtube#video",
     "videoId": "SCkFwpW3kiE"
     },
 "snippet": {
     "publishedAt": "2013-05-23T16:27:31.000Z",
     "channelId": "UCeF4WiRKOA4XzphWYzR9aVw",
     "title": "Sir Richard Branson dresses as an air stewardess after losing bet",
     "description": "(Reuters) - A man was arrested at the Cannes film festival on Friday after firing a starting pistol during a live TV broadcast on the palm-lined waterfront, ...",
     "thumbnails": {
         "default": {
             "url": "https://i.ytimg.com/vi/SCkFwpW3kiE/default.jpg"
             },
         "medium": {
             "url": "https://i.ytimg.com/vi/SCkFwpW3kiE/mqdefault.jpg"
             },
         "high": {
             "url": "https://i.ytimg.com/vi/SCkFwpW3kiE/hqdefault.jpg"
             }
         },
     "channelTitle": "TheDailyPolitics247"
     }
},
{
 "kind": "youtube#searchResult",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/-_OEOHhzgCBTLr7x5UoDk2kHlJM\"",
 "id": {
     "kind": "youtube#video",
     "videoId": "CBvDp0i8Iok"
     },
 "snippet": {
     "publishedAt": "2013-05-25T22:38:00.000Z",
     "channelId": "UC2j75rAKcfjBAhW7WmNY4Qg",
     "title": "Virgin Galactic Spaceship Passes Big Test (Y)",
     "description": "A spaceship bankrolled by British tycoon Sir Richard Branson made its first engine-powered flight Monday. The test flight moves Virgin Galactic toward its go...",
     "thumbnails": {
     "default": {
         "url": "https://i.ytimg.com/vi/CBvDp0i8Iok/default.jpg"
         },
     "medium": {
         "url": "https://i.ytimg.com/vi/CBvDp0i8Iok/mqdefault.jpg"
         },
     "high": {
         "url": "https://i.ytimg.com/vi/CBvDp0i8Iok/hqdefault.jpg"
     }
 },
"channelTitle": "NewActionNews"
}
}
]
}

如果您使用 nextPageToken 搜索下一个视频:“CAUCAA”,则响应包含相同的视频 ID(J6PY5vxLU8Y、SCkFwpW3kiE、CBvDp0i8Iok):

{
 "kind": "youtube#searchListResponse",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/xMtmG2pQsuo_TFF8AtaaPea-cNc\"",
 "pageInfo": {
     "totalResults": 144,
     "resultsPerPage": 5
     },
 "nextPageToken": "CAoQAA",
 "prevPageToken": "CAUQAQ",
 "items": [
     {
      "kind": "youtube#searchResult",
      "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/3nxQ-ejnv0qekcbyq09HD2RQt_w\"",
      "id": {
       "kind": "youtube#video",
       "videoId": "J6PY5vxLU8Y"
       },
      "snippet": {
      "publishedAt": "2013-05-22T09:58:34.000Z",
      "channelId": "UCimPiDCqxvfqwVJltL4YzHg",
      "title": "Bono, Richard Branson, and Olivia Wilde Joined Matt Damon's Strike!",
      "description": "Support the strike: http://strikewithme.org/ Millions of celebrities have joined Matt Damon's \"Toilet Strike\" protesting the lack of access to safe water and...",
      "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/default.jpg"
            },
          "medium": {
            "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/mqdefault.jpg"
            },
          "high": {
            "url": "https://i.ytimg.com/vi/J6PY5vxLU8Y/hqdefault.jpg"
            }
          },
      "channelTitle": "water"
    }
},
{
 "kind": "youtube#searchResult",
 "etag": "\"g-RLCMLrfPIk8n3AxYYPPliWWoo/cEIRgKqwt1aa9hcWMNtGTiCJImc\"",
 "id": {
    "kind": "youtube#video",
    "videoId": "h7hJ3FDGWY8"
    },
 "snippet": {
    "publishedAt": "2013-05-22T10:01:25.000Z",
    "channelId": "UCqcE1T9zcUQyX3hHH4EM7sQ",
    "title": "Sir Richard Branson in Dubai",
    "description": "The man behind the Virgin brand stopped by Kris Fade's show last week - broadcasting from the Burj Khalifa, the world's tallest building.",
    "thumbnails": {
      "default": {
        "url": "https://i.ytimg.com/vi/h7hJ3FDGWY8/default.jpg"
        },
      "medium": {
        "url": "https://i.ytimg.com/vi/h7hJ3FDGWY8/mqdefault.jpg"
        },
      "high": {
        "url": "https://i.ytimg.com/vi/h7hJ3FDGWY8/hqdefault.jpg"
        }
      },
    "channelTitle": "Kimberleyleonard"
   }
 }
]

}

我做错什么了吗 ?

4

2 回答 2

3

在 API 的早期版本中,“totalResults”属性始终只是搜索算法在实际检索任何结果之前提供的估计值,因此可以安全地假设 v3 也是如此。然而,这个估计可能相差这么远有点奇怪。对于您的查询,实际上只有几个结果(我认为是 5 或 6 个......因为自从您发布此初始帖子以来已经上传了几个)。

我已经玩了一段时间的各种参数,看起来对 totalResults 近似值的准确性影响最大的因素是“q”参数——你提供的值更具体在那里,totalResults 变得越准确。

当然,在您的查询中,q 参数是空的,并且 totalResults 近似值非常遥远(事实上,如果您将 publishedAfter 参数更改为 5 月 1 日而不是 5 月 21 日,您实际上会得到近似 LESS 总结果,即使它是更早的日期!)。但是,如果您执行这样的查询:

https://www.googleapis.com/youtube/v3/search?part=id&maxResults=50&publishedAfter=2010-05-01T21%3A47%3A38Z&topicId=%2Fm%2F0n839&key={YOUR_KEY}&q=Bran

然后你得到一个与实际结果数量完全匹配的 totalResults 近似值。

当然这可能令人沮丧,因为在合并 Freebase ID 时,您通常不想使用 q 参数——freebase ID 的全部意义在于使用语义相关的信息,而不仅仅是基于字符串匹配!但很明显,这告诉我们一些有关 YouTube 内部搜索算法及其依赖的信息。我敢冒险,随着 Freebase 集成变得更加成熟,搜索算法将能够更好地适应,并且您将再次开始看到更好的 totalResults 近似值。

作为一种解决方法,您可以使用您注意到的“nextPageToken”来获得更好的计数。在您的查询中,将您的 maxResults 设置为 50,当您获得实际结果时,让您的例程计算它们。如果它们小于 50,那么您就拥有了它们。如果该页面上的结果为 50,则您可能希望预取下一组结果(如果它们是新结果,那么您很高兴,而如果它们与您所拥有的结果相同,那么您完全有50 个回复)。一个问题是,这会阻止您在应用程序中显示准确的总结果计数(即,如果您在某个地方进行了分页),所以它并不完美,但解决方法是什么?

于 2013-05-30T17:32:48.487 回答
0

我很确定结果数量和实际显示的数量之间的差异是由于日期限制(start_time:1369172858,“publishedAfter:2013-05-21T21:47:38Z”的表示)是如何实现的。顺便说一句,我现在看到了 10 场比赛,包括上面提到的三场比赛。

使用包含请求日期范围的狭义限制范围检索第一个结果。这就是你可以获得〜140场比赛的方式。然后过滤在检索中幸存下来的视频,拒绝那些超出实际请求范围的视频。在那个阶段丢弃 130 个视频,只剩下 10 个真正满足请求的视频是很合理的。

匹配数表示检索到的数字——对于日期范围,这通常会被高估,可能很严重。我们通常不保证报告为“匹配”的数字实际上都匹配,因为检索后会发生各种过滤。

于 2013-06-28T15:37:53.223 回答