Example #1
0
def download(url):
    tmpfn = tempfile.mktemp()
    logging.info("Downloading '%s' to '%s'", url, tmpfn)

    ud = urllib2.urlopen(url)
    total_bytes = float(ud.headers['Content-Length'])

    chunksize = 4096
    bytes_read = 0
    start_time = time.time()

    # Variables to keep track of progress.
    last_log = time.time()
    last_bytes = 0

    fd = open(tmpfn, "wb")
    while True:
        data = ud.read(chunksize)
        if not data:
            fd.close()
            break
        fd.write(data)
        bytes_read += len(data)
        last_bytes += len(data)

        if time.time() > last_log + 2:
            speed = convert_bytes((last_bytes / (time.time() - last_log)))
            percent_complete = (bytes_read / total_bytes) * 100

            logging.debug("Progress %0.2f%% @ %s/s", percent_complete, speed)

            # Reset
            last_log = time.time()
            last_bytes = 0

    elapsed = time.time() - start_time
    average_speed = total_bytes / elapsed
    logging.info("Downloaded %s in %0.0f seconds, average speed %s/s", convert_bytes(total_bytes), elapsed, convert_bytes(average_speed))

    return tmpfn
Example #2
0
 def human_size(self):
     return convert_bytes(self.size)
Example #3
0
 def human_size(self):
     return convert_bytes(self.size)