2013-05-28 111 views
1

我試圖使用YouTube數據API通過使用Freebase主題來搜索視頻。 它適用於一個主題的簡單搜索,但它不適用於帶有2個參數的搜索:主題ID和「publishedAfter」日期。Youtube Data API v3搜索查詢與Freebase主題ID和日期

例如,搜索有關布蘭森視頻(遊離鹼ID = /米/ 0n839),YouTube數據API站點: https://developers.google.com/youtube/v3/docs/search/list#try-it 與參數:

部分:代碼段

topicId:/M/0n839

顯示的2165個視頻

但結果當你嘗試搜索關於ID =/M/0n839視頻與

部分:代碼段

topicId:/米/ 0n839

publishedAfter: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日的值,實際上會得到它接近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個答覆)。一個問題是,這會阻止你在你的應用程序中顯示精確的總計結果(即如果你的分頁正在某處),所以它不是完美的,但是解決方法是什麼?

0

我很確定實際顯示的結果數量和數量之間的差異是由於日期限制(start_time:1369172858,「publishedAfter:2013-05-21T21:47:38Z」的表示)實現的。順便說一下,我現在看到10場比賽,其中包括上述三場比賽。

使用包含請求的日期範圍的窄限制範圍檢索第一個結果。這就是你可以得到140場比賽的方式。然後過濾檢索出的視頻,拒絕那些要求的範圍以外的視頻。在這個階段放棄130個視頻是非常合理的,剩下的10個視頻實際上滿足了這個要求。

匹配計數表示檢索的數量 - 對於日期範圍,這通常會被高估,可能是嚴重的。 我們通常不保證報告的「匹配」數量實際上匹配,因爲檢索後發生各種過濾。