#!/usr/bin/env python import re import urllib, httplib __author__ = 'Kun Xi ' __version__ = '0.1.0' __license__ = 'Python Software Foundation' __idpattern = re.compile("\?v=([^&]+)") __urlpattern = re.compile('\/player2\.swf\?video_id=.*?&t=([^&]+)', re.M) class progressBar: '''Randy Pargman's progress bar class http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/168639''' def __init__(self, minValue = 0, maxValue = 10, totalWidth=12): self.progBar = "[]" # This holds the progress bar string self.min = minValue self.max = maxValue self.span = maxValue - minValue self.width = totalWidth self.amount = 0 # When amount == max, we are 100% done self.updateAmount(0) # Build progress bar string def updateAmount(self, newAmount = 0): if newAmount < self.min: newAmount = self.min if newAmount > self.max: newAmount = self.max self.amount = newAmount # Figure out the new percent done, round to an integer diffFromMin = float(self.amount - self.min) percentDone = (diffFromMin / float(self.span)) * 100.0 percentDone = round(percentDone) percentDone = int(percentDone) # Figure out how many hash bars the percentage should be allFull = self.width - 2 numHashes = (percentDone / 100.0) * allFull numHashes = int(round(numHashes)) # build a progress bar with hashes and spaces self.progBar = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]" # figure out where to put the percentage, roughly centered percentPlace = (len(self.progBar) / 2) - len(str(percentDone)) percentString = str(percentDone) + "%" # slice the percentage into the bar self.progBar = self.progBar[0:percentPlace] + percentString + \ self.progBar[percentPlace+len(percentString):] def __str__(self): return str(self.progBar) class badoption: pass def parseURL(url): '''return the magic number and the real url for the flv file''' try: id = __idpattern.search(url, 1).group(1) except AttributeError: raise badoption fh = urllib.urlopen(url) content = fh.read() fh.close() try: match = __urlpattern.search(content).group(1) except AttributeError: raise badoption url = 'http://youtube.com/get_video.php?video_id=' + id + '&t=' + match return id, url def main(): import sys, os, getopt def usage(): print '''TubeFetch: Download the video clips from YouTube Version %s. developed by Kun Xi Usage: tubefetch.py [options] files ... Examples: tubefetch.py -c http://the-url http://the-url2 ... Options: -h --help print this information -v --version version information -c convert the flv file to avi, needs ffmpeg''' % __version__ def version(): print '''tubefetch version %s: developed by Kun Xi ''' % __version__ def callback(transferred, blocksize, totalsize): if (totalsize != 0): pb.updateAmount(transferred * blocksize * 100 / totalsize) sys.stderr.write('Downloading %s: %s \r' % (id, pb)) try: opts, args = getopt.getopt(sys.argv[1:], "hvc", ["help", "version"]) except getopt.GetoptError: usage() sys.exit(2) convert = False for o, a in opts: if o in ('-h', '--help'): usage() sys.exit() if o in ('-v', '--version'): version() sys.exit() if o == '-c': convert = True if len(args) < 1: usage() sys.exit(2) pb = progressBar(0, 100, 45) for x in args: try: id, url = parseURL(x) except badoption: sys.stderr.write('Could not parse %s .... bypass\n' % x) continue urllib.urlretrieve(url, id + '.flv', callback) sys.stderr.write('\n') if convert: #l = ['ffmpeg'] + ('-i %s.flv %s.avi' % (id, id)).split() #os.spawnvp(os.P_NOWAIT, 'ffmpeg', l) childin, childout = os.popen4('ffmpeg -i %s.flv %s.avi' %(id, id)) childin.close() childout.close() os.wait() sys.stderr.write('\n') if __name__ == '__main__': main()