我正在編寫一個應用程序,使用Python將視頻添加到Youtube上的用戶播放列表中。每次這樣做會導致Youtube開始限制我的請求。是否可以使用Python Youtube API提交批處理請求?
有一個批處理API,允許您一次提交50個請求,但我無法從文檔中找到如何提交批處理請求。關於它的唯一信息涵蓋了需要爲請求發送的XML內容。
有誰知道如何提交批處理請求?
我正在編寫一個應用程序,使用Python將視頻添加到Youtube上的用戶播放列表中。每次這樣做會導致Youtube開始限制我的請求。是否可以使用Python Youtube API提交批處理請求?
有一個批處理API,允許您一次提交50個請求,但我無法從文檔中找到如何提交批處理請求。關於它的唯一信息涵蓋了需要爲請求發送的XML內容。
有誰知道如何提交批處理請求?
它看起來像這是記錄在gdata-python-客戶端wiki:http://code.google.com/p/gdata-python-client/wiki/UsingBatchOperations。雖然該頁面上的示例適用於基本和電子表格,而不是YouTube,但將相同的技術應用於YouTube API應該相當簡單。我相信你會使用v2 API。
我已經成功地完成事情是這樣的:
query = "<feed xmlns=\"http://www.w3.org/2005/Atom\""
query += " xmlns:media=\"http://search.yahoo.com/mrss/\""
query += " xmlns:batch=\"http://schemas.google.com/gdata/batch\""
query += " xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">"
query += "<batch:operation type=\"query\"/>"
# Assume ids contain list of YouTube video IDs
for vid in ids:
query += ("<entry><id>http://gdata.youtube.com/feeds/api/videos/%s</id></entry>" % vid)
query += "</feed>"
uri = 'http://gdata.youtube.com/feeds/api/videos/batch'
feed = client.Post(query, uri, converter=gdata.youtube.YouTubeVideoFeedFromString)
得到的進料可以作爲標準的YouTube API供稿迭代。雖然特別照顧丟失的視頻和其他<batch:status>-應採取:
if len(feed.entry):
for entry in feed.entry:
skip = False
for x in entry.extension_elements:
if x.tag == "status" and x.namespace == "http://schemas.google.com/gdata/batch" and x.attributes["code"] != "200":
if x.attributes["code"] == "404":
skip = True
# Likewize you can check for entry's 403 e.g. Quota Exceeded etc
... # Your entry processing goes here
快速,簡單,工作。好的一段代碼解決了我的問題。謝謝! – Damien 2012-01-06 09:21:11
這看起來很完美,謝謝! – mdm 2010-02-01 22:00:57