Esempio n. 1
0
def downloadFile(url: str,
                 fileName: str,
                 stimeout=3.05,
                 showProgress: bool = False,
                 append: bool = False):
    try:
        response = urlopen(url, timeout=stimeout)

        totalSize = response.length
        fileSize, unit = unitFix(totalSize, Unit.BYTE, Unit.MB)
        if showProgress:
            progress = ProgressTool(fileSize, 15, unit=unit.name)

        # mkdir
        path = getDirName(fileName)
        mkdirs(path)

        curcount = 0
        chunksize = 16 * 1024
        mode = 'wb' if not append else 'ab'
        with open(fileName, mode) as f:
            while True:
                chunk = response.read(chunksize)
                curcount += len(chunk)
                if showProgress:
                    progress.setCurCount(convert(curcount, Unit.BYTE, unit))
                f.write(chunk)
                if curcount >= totalSize:
                    break
            return True, None
    except Exception as e:
        return False, e
Esempio n. 2
0
def downloadFile(url,
                 fileName,
                 stimeout=None,
                 showprogress=False,
                 append=False):
    if sys.version_info > (3, 0):
        from urllib.request import urlopen
    else:
        from urllib2 import urlopen

    try:
        if stimeout is None:
            response = urlopen(url)
        else:
            response = urlopen(url, timeout=stimeout)

        unit = 'mb'
        if convertStorageUnit(response.length, 'byte', unit) < 1:
            unit = 'kb'
        progress = None
        if showprogress:
            desc = getFileName(fileName)
            progress = ProgressTool(convertStorageUnit(response.length, 'byte',
                                                       unit),
                                    10,
                                    unit=unit,
                                    desc=desc)

        mode = 'wb'
        if append:
            mode = 'ab'

        curcount = 0
        chunksize = 16 * 1024
        with open(fileName, mode) as f:
            while True:
                chunk = response.read(chunksize)
                curcount += len(chunk)
                if progress:
                    progress.setCurCount(
                        convertStorageUnit(curcount, 'byte', unit))
                if not chunk:
                    break
                f.write(chunk)
            return True
    except:
        return False
Esempio n. 3
0
def downloadFileByUrls(urlArray, fileName, stimeout=None, showprogress=False):
    if os.access(fileName, 0):
        os.remove(fileName)

    progress = None
    if showprogress:
        desc = getFileName(fileName)
        progress = ProgressTool(len(urlArray), 10, unit='', desc=desc)

    curcount = 1
    for item in urlArray:
        ret = downloadFile(item, fileName, stimeout, False, append=True)
        if ret != True:
            return False
        if progress:
            progress.setCurCount(curcount)
            curcount += 1
    return True