tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

misc.py (1046B)


      1 import os
      2 import progressbar
      3 from urllib.request import urlopen
      4 
      5 UnicodeXMLURL = "https://raw.githubusercontent.com/w3c/xml-entities/gh-pages/unicode.xml"
      6 InlineAxisOperatorsURL = "https://w3c.github.io/mathml-core/tables/inline-axis-operators.txt"
      7 
      8 
      9 def downloadWithProgressBar(url, outputDirectory="./", forceDownload=False):
     10 
     11    baseName = os.path.basename(url)
     12    fileName = os.path.join(outputDirectory, baseName)
     13 
     14    if not forceDownload and os.path.exists(fileName):
     15        return fileName
     16 
     17    request = urlopen(url)
     18    totalSize = int(request.info().get('Content-Length').strip())
     19    bar = progressbar.ProgressBar(maxval=totalSize).start()
     20 
     21    chunkSize = 16 * 1024
     22    downloaded = 0
     23    print("Downloading %s" % url)
     24    os.umask(0o002)
     25    with open(fileName, 'wb') as fp:
     26        while True:
     27            chunk = request.read(chunkSize)
     28            downloaded += len(chunk)
     29            bar.update(downloaded)
     30            if not chunk:
     31                break
     32            fp.write(chunk)
     33        bar.finish()
     34 
     35    return fileName