I was playing with Google Webmaster tools and noticed the video sitemap option, and decided to generate one. After bit of searching I did come up with not many entries on how this could be quickly generated automatically.

So I had to manually create the sitemap for a single video based on the example schema, but then I realised this would be a nice quick python code if we could automated as much as possible.

Here is youtube_video_sitemap.py:

from xml.dom.minidom import Document
import gdata.youtube
import gdata.youtube.service

#http://code.google.com/apis/youtube/1.0/developers_guide_python.html#RetrievingVideoEntry
client = gdata.youtube.service.YouTubeService()
query = gdata.youtube.service.YouTubeVideoQuery()

############################
# Change these
location="http://www.ted.com"
changefreq="weekly"

query.max_results = 25
query.start_index = 1
query.author = "TEDtalksDirector"
############################

feed = client.YouTubeQuery(query)

doc = Document()
urlset  = doc.createElement("urlset")
urlset.setAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
urlset.setAttribute("xmlns:video", "http://www.sitemaps.org/schemas/sitemap/0.9")
doc.appendChild(urlset)

url = doc.createElement("url")

# Location  
elem = doc.createElement("loc")
elem_text = doc.createTextNode(location)
elem.appendChild(elem_text)
url.appendChild(elem)

for entry in feed.entry:
    # video
    video = doc.createElement("video:video")

    # video:thumbnail_loc
    elem = doc.createElement("video:thumbnail_loc")
    elem_text = doc.createTextNode(entry.media.thumbnail[0].url)
    elem.appendChild(elem_text)
    video.appendChild(elem)

    # video:title
    elem = doc.createElement("video:title")
    elem_text = doc.createTextNode(entry.media.title.text)
    elem.appendChild(elem_text)
    video.appendChild(elem)

    # video:description
    elem = doc.createElement("video:description")
    elem_text = doc.createTextNode(entry.media.description.text)
    elem.appendChild(elem_text)
    video.appendChild(elem)
    [...]

# end of url    
urlset.appendChild(url)
#print doc.toxml()
print doc.toprettyxml(indent="  ")

A video sitemap for TED talks, maybe next if I get the time I can code up an AppEngine app with a web ui.

Update 20110717: I wrote some still buggy javascript which you can generate a video sitemap in your browser