我正在尝试使用YouTube Data API通过Freebase主题搜索视频。 它适用于具有一个主题的简单搜索,但它不适用于具有2个参数的搜索:主题ID和“publishedAfter”日期。
例如,要搜索有关Richard Branson(Freebase id = / m / 0n839)的视频,请访问youtube数据api网站: https://developers.google.com/youtube/v3/docs/search/list#try-it 参数:
part:snippet
topicId:/ m / 0n839
显示2165个视频的结果
但是当您尝试使用
搜索有关id = / m / 0n839的视频时part:snippet
topicId:/ 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搜索下一个视频:“CAUQAA”,则响应包含相同的视频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"
}
}
]
}
我做错了吗?
答案 0 :(得分:3)
在以前版本的API中,“totalResults”属性始终只是搜索算法在实际检索任何结果之前提供的估计值,因此可以安全地假设v3也是如此。然而,估计可能是如此遥远,这有点奇怪;对于你的查询,确实只有几个结果(5或6,我认为...因为你做了这个初始帖子后已经上传了几个)。
我已经玩了一些各种参数,看起来对于totalResults近似的准确性影响最大的因素是'q'参数 - 更具体的一个你在那里提供的价值,totalResults变得越准确。
当然,在你的查询中,q参数是空的,而且totalResults近似值相当远(事实上,如果你将publishedAfter参数更改为5月21日的5月1日,你实际上得到它近似总结果很少,即使它是较早的日期!)。但是,如果您执行此类查询:
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回复)。一个问题是,这将阻止您在您的应用程序中显示总结果的准确计数(即,如果您已经在某处进行分页),那么它并不完美,但是解决方法是什么?
答案 1 :(得分:0)
我很确定结果数量和实际显示数量之间的差异是由于日期限制(start_time:1369172858,“publishedAfter:2013-05-21T21:47:38Z”的表示)的实现。 顺便说一下,我现在看到10场比赛,包括上面提到的三场比赛。
使用包含所请求日期范围的窄边限制范围检索第一个结果。这就是你如何得到~140场比赛。然后过滤检索后的视频,拒绝实际请求范围之外的视频。在那个阶段放弃了130个视频,留下实际满足请求的10个视频,这是非常合理的。
匹配计数表示检索到的数字 - 对于日期范围,这通常是高估,可能是严重的。 我们通常不保证报告为“匹配”的数字实际上都匹配,因为检索后会发生各种过滤。