From a755339fb2b3770fdafd2f14486ebb967933f7fc Mon Sep 17 00:00:00 2001 From: Olof Johansson Date: Fri, 21 Dec 2012 23:23:16 +0100 Subject: [PATCH] Print a progressbar when downloading with HDS/HLS --- svtplay-dl | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/svtplay-dl b/svtplay-dl index 92509f4..64230db 100755 --- a/svtplay-dl +++ b/svtplay-dl @@ -330,8 +330,7 @@ def download_hds(options, url, output, swf): while i <= antal[1]["total"]: url = "%s/%sSeg1-Frag%s" % (baseurl, test["url"], i) if output != "-": - progresstr = "Downloading %s/%s fragments" % (i, antal[1]["total"]) - sys.stderr.write(progresstr + '\r') + progressbar(antal[1]["total"], i) data = get_http_data(url) number = decode_f4f(i, data) file_d.write(data[number:]) @@ -376,8 +375,7 @@ def download_hls(options, url, output, live, other): for i in files: if output != "-": - progresstr = "Downloading %s/%s fragments" % (n, len(files)) - sys.stderr.write(progresstr + '\r') + progressbar(len(files), n) data = get_http_data(i[0]) if encrypted: lots = StringIO.StringIO(data) @@ -911,6 +909,40 @@ class Svt(): else: download_http(filename, self.output) +def progressbar(total, pos, msg=""): + """ + Given a total and a progress position, output a progress bar + to stderr. It is important to not output anything else while + using this, as it relies soley on the behavior of carriage + return (\\r). + + Can also take an optioal message to add after the + progressbar. It must not contain newliens. + + The progress bar will look something like this: + + [099/500][=========...............................] ETA: 13:36:59 + + Of course, the ETA part should be supplied be the calling + function. + """ + width = 50 # TODO hardcoded progressbar width + rel_pos = int(float(pos)/total*width) + bar = str() + + # FIXME ugly generation of bar + for i in range(0, rel_pos): + bar += "=" + for i in range(rel_pos, width): + bar += "." + + # Determine how many digits in total (base 10) + digits_total = len(str(total)) + fmt_width = "%0" + str(digits_total) + "d" + fmt = "\r[" + fmt_width + "/" + fmt_width + "][%s] %s" + + sys.stderr.write(fmt % (pos, total, bar, msg)) + def main(): """ Main program """ usage = "usage: %prog [options] url"