Python script to download youtube vids

[sourcecode language=”python”]

import sys, os, random, time

if len(sys.argv) < 3:
print "Usage: youtube_pl2vids [playlist.html] [destination directory]"
sys.exit(1)

source = sys.argv[1]
dest = sys.argv[2]
urllines = []
f = open(os.path.join(dest, ‘youtube.dl’), ‘w’)
for line in open(source, ‘r’).readlines():
if ‘<a href="/watch?v=’ in line and ‘class="tile-link-block video-tile"’ in line:
url = "http://www.youtube.com" + line.split(‘&amp’)[0].split(‘<a href="’)[1]
f.write(url + ‘n’)
urllines.append(url)
f.close()
print urllines
# we assume youtube-dl is installed
for line in urllines:
os.system(‘youtube-dl -t –max-quality 22 %s’ % line)
s = random.randint(1, 6)
print "sleeping %s secs" % s
time.sleep(s)
[/sourcecode]