The YouTube API and Python

I’ve last week I played with the Del.icio.us API a tiny bit, and wrote a quick Python script to download all of my bookmarks and tags to a file as a backup. That was the first time I ever implemented anything using an API.

So, today I found out that the YouTube Developer API is available. Apparently you can use either XML-RPC or the REST interface. And if you choose the XML-RPC interface, you can just pass the server a dictionary that holds the parameters for each call. Cool!

Update: Jason Golod has some Google API and Yahoo API links as well.

9 Responses to “The YouTube API and Python”

  1. gindc Says:

    This program continuously plays youtube
    videos using a python script and VLC.

    Anyway, the program plays continues feeds from
    youtube via os.system calls to VLC.

    Just put the script in your VLC directory and run it.

    Love the site, please keep up the free flow of info.

    If it gets posted anywhere just put gindc.

    Thanks again,

    gindc

    #!/usr/bin/python
    #Load continues video from youtube.com.
    #Requires videolan (VLC) media player (see http://www.videolan.org/)
    #Just put this python script in your vlc.exe directory.
    import urllib,sys
    import os
    from time import sleep
    from string import find,replace
    from htmllib import HTMLParser
    from formatter import AbstractFormatter,DumbWriter
    from cStringIO import StringIO
    from random import shuffle

    number_of_videos=30
    startpage=’http://www.youtube.com/categories_portal?c=23&e=1′

    def readurl(url):
    try:
    f=urllib.urlopen(url)
    txt=f.read()
    f.close()
    except:
    txt=”
    return txt

    def FindLinks(html):

    tmp = StringIO()
    f = AbstractFormatter(DumbWriter(tmp))
    parser=HTMLParser(f)
    parser.feed(html)
    return parser.anchorlist

    txt=readurl(startpage)
    linklist=FindLinks(txt)
    shuffle(linklist)
    started=False
    cnter=999
    linklist2=[]
    print ‘loading link pages, please wait…’
    for startpage in linklist:
    if find(startpage,’/watch’)==-1 and number_of_videos>len(linklist2):
    txt=readurl(’http://www.youtube.com’+startpage)
    ll=FindLinks(txt)
    for l in ll:
    if find(l,’/watch’)>-1:
    if not l in linklist2:
    linklist2.append(l)
    else:
    if not startpage in linklist2:
    linklist2.append(startpage)
    shuffle(linklist2)
    for startpage in linklist2:
    txt=readurl(’http://www.youtube.com’+startpage)
    spot=find(txt,’new SWFObject’)
    if spot>-1:
    cnter+=1
    txt=txt[spot+37:]
    spot2=find(txt,’”‘)
    txt=txt[:spot2]
    video_id = txt[:]
    fname=’test’+str(cnter)+’.flv’
    print ‘retrieving video::’+str(cnter)

    urllib.urlretrieve(”http://www.youtube.com/get_video?video_id=”+video_id,fna
    me)
    if started:
    o.close()
    o=os.popen(”vlc.exe “+fname+” -f vlc:quit”)
    started=True

  2. Sam Says:

    I have created a very simple PHP class that handles a few of the key features of the wordpress API.

    You can find it at http://www.napolitopia.com/youtube_videos.php

    Let me know what you think, or if you have any suggestions.

    -Sam

  3. Stubbleblog Says:

    YouTube API How-To…

    What is it The YouTube API lets you search for videos and display them on your site. For example, I use the API to pull videos tagged dance and display them on Rate My Dance Moves. The YouTube API is……

  4. reverse Says:

    Hi gindc

    I get following error!
    >C:\Python25\pythonw -u “youtubeApi.py”
    File “youtubeApi.py”, line 15
    SyntaxError: Non-ASCII character ‘\x92′ in file youtubeApi.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

    I am quite a newbie to python….sorry for if it sounds dumb.

  5. reverse Says:

    Hi gindc
    I tried and tried but just can`t get the a properly indented to make it work, therefore I would appericiate a lot if you could send me a a properly indented version.

    many thanks and regards
    reverse

  6. import this. » Blog Archive » Download videos from YouTube Says:

    [...] Related: YouTube API [...]

  7. Schwo.com Says:

    YouTube Kills TubePress++…

    Well, the success of my addition to TubePress was short lived. Youtube, and some users, had a problem with the undocumented api call I found. The call returned public and private playlists for any user. Needless to say that was a problem YouTube had…

  8. Schwo.com Says:

    Undocumented YouTube API Method…

    I’m starting a project to remodel the video section on my blog to be more like the photos section. I’d like to have albums of videos just as I have albums of photos. The logical way to group videos with YouTube is by using playlists. The…

  9. YouTube API for Python » Idea Critic Says:

    [...] I wanted to use the YouTube API a few weeks back for a project I’m working on, but after prowling around the Internet, I couldn’t find any Python library that implements it (something like flickr.py). So I wrote my own. [...]

Leave a Reply