使用Python Youtube API提交批处理请求是否可行?
我正在用Python写一个应用程序,目的是把视频添加到用户的Youtube播放列表里。不过,如果我一个一个地添加,Youtube就会开始限制我的请求。
其实有一个批量处理的接口,可以一次提交50个请求,但我在文档里找不到怎么提交批量请求的具体方法。文档里只提到需要发送的XML内容。
有没有人知道怎么提交批量处理请求呢?
2 个回答
3
我已经成功用这种方式完成了事情:
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
3
看起来这个内容在gdata-python-client的维基上有说明:http://code.google.com/p/gdata-python-client/wiki/UsingBatchOperations。虽然那页上的例子是针对Base和电子表格的,而不是YouTube,但我觉得把同样的方法应用到YouTube API上应该也不难。你可能需要使用v2版本的API。