【问题标题】:Is it possible to submit batch processing requests with the Python Youtube API?是否可以使用 Python Youtube API 提交批处理请求?
【发布时间】:2010-02-01 20:54:16
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我已经设法以这种方式完成工作:

      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>-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
      

      【讨论】:

      • 快速、简单且有效。一段很好的代码解决了我的问题。谢谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      相关资源
      最近更新 更多