是否可以使用Python Youtube API提交批处理请求?

时间:2010-02-01 20:54:17

标签: python youtube gdata-api batch-processing playlist

我正在使用Python编写一个应用程序,将视频添加到Youtube上的用户播放列表中。一次执行此操作会导致Youtube开始限制我的请求。

有一个批处理API允许您一次提交50个请求,但我无法从文档中找到如何提交批处理请求。有关它的唯一信息包括需要为请求发送的XML内容。

有人知道如何提交批处理请求吗?

2 个答案:

答案 0 :(得分:3)

这看起来像是在gdata-python-client wiki上记录的:http://code.google.com/p/gdata-python-client/wiki/UsingBatchOperations。虽然该页面上的示例适用于Base和Spreadsheets,而不是YouTube,但将相同的技术应用于YouTube API应该相当简单。我相信,您需要使用v2 API。

答案 1 :(得分: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 )

生成的Feed可以作为标准youtube api feed进行迭代。虽然应该特别注意丢失的视频和其他<batch:status> - es:

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
相关问题