Question

I'm writing an application using Python that adds videos to a user's playlist on Youtube. Doing this one at a time causes Youtube to start throttling my requests.

There is a batch processing API that allows you to submit 50 requests at once, but I can't find out from the docs how to submit a batch processing request. The only information about it covers the XML content that needs to be sent for the request.

Does anybody know how to submit a batch processing request?

Was it helpful?

Solution

It looks like this is documented on the gdata-python-client wiki: http://code.google.com/p/gdata-python-client/wiki/UsingBatchOperations. While the examples on that page are for Base and Spreadsheets, not YouTube, it should be fairly straightforward to apply the same techniques to the YouTube API. You will, I believe, need to be using the v2 API.

OTHER TIPS

I've managed to get things done this way:

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 )

Resulting feed can be iterated as standard youtube api feeds. Although special care of missing videos and other <batch:status>-es should be taken:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top