我正在编写一个应用程序,使用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