#!/usr/bin/python # R(ange) URL D(ownloader) see usage() # version: see below # author: Ludovic Bellier see http://home.zyrianes.net # licence: GNU General Public License see http://www.gnu.org/copyleft/gpl.html version=0.9 import getopt, sys, os, re, string def parseRangeURL(rangeURL, dry_run): """ Parse range URL, and wget urls if desired (ie: not dry_run mode) :Parameters: -`rangeURL`: url like with range indication with '[' and ']' -`dry_run`: if true, just echo urls, do not download """ def print_url(urls): if type(urls) == type([]): for url in urls: print url else: print urls def get_files(urls): str_url='' for url in urls: str_url=str_url+' %s '% url os.system('wget %s' % str_url) if dry_run: dl_f=print_url else: dl_f=get_files urls=[] match=re.match('(.*)\[(.*)-(.*)\](.*)',rangeURL) if not match: urls=[rangeURL] else: base_url,range_left,range_right,end_url=match.groups() # number of '0', and see zfill below (don't know how to # use variable notation like '%.(nb_zero)d' ) re_nb_zero=re.match('(0*)(.*)',range_left) if re_nb_zero: nb_zero=len(re_nb_zero.group(1))+1 else: nb_zero=0 for i in range(int(range_left), int(range_right)+1): urls.append("%s%s%s" % (base_url, string.zfill(i, nb_zero), end_url)) # download or print map(dl_f,(urls,)) def usage(): sys.stderr.write("""Range URL Downloader, version %s, gpl Usage: rurld [-n] [-h] -n dry_run mode, just show urls -h this help for example: rurld http://foo.com/pic[01-11].jpg downloads files from http://foo.com/pic01.jpg to http://foo.com/pic11.jpg note that [01-11] produces 01->11 and [1-11] 1->11 required: wget ( http://www.gnu.org/software/wget/wget.html ) """ % version ) sys.exit(1) def main(args): dry_run=0 try: options, args = getopt.getopt(args[1:], 'hn') except: usage() for k, v in options: if k=='-h': usage() if k=='-n': dry_run=1 if len(args)!=1: usage() parseRangeURL(args[0], dry_run) if __name__=='__main__': main(sys.argv)