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.

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