def calcResizeRatio(resizedImgBytes, originImg):
    """Calculate imgage resize ratio

    Args:
        resizedImgBytes (bytes): resized image binary bytes
        originImg (Image): original image Object
    Returns:
        resize ratio (float)
    Raises:
    """
    originSize = originImg.width, originImg.height
    originBytes = imageToBytes(originImg)

    resizedImg = bytesToImage(resizedImgBytes)
    resizedSize = resizedImg.width, resizedImg.height

    originLen = len(originBytes)
    resizedLen = len(resizedImgBytes)
    resizeRatio = float(resizedLen) / float(originLen)
    resizeRatioPercent = int(resizeRatio * 100)
    printStr = "\t-> Compress ratio=%d%%, from [fmt=%s, size=%sx%s, len=%s] to [fmt=%s, size=%sx%s, len=%s]" %  (
        resizeRatioPercent, 
            originImg.format, originSize[0], originSize[1], formatSize(originLen),
            resizedImg.format, resizedSize[0], resizedSize[1], formatSize(resizedLen)
    )
    print(printStr)
    # logging.debug(printStr)
    return resizeRatio
Example #2
0
def downloadFile(
    url,
    fileToSave=None,
    proxies=None,
    isStreamMode=True,
    isResume=True,
):
    """Download file from url then save to file

    Args:
        url (str): file online url
        fileToSave (str): filename or full file path
        proxies (dict): requests proxies
        isStreamMode (bool): use stream mode or not
    Returns:
        download ok or not (bool)
    Raises:
    Examples:
        input: 
            'https://book.crifan.com/books/5g_message_rcs_tech_summary/pdf/5g_message_rcs_tech_summary.pdf'
            'downloaded/pdf/5g_message_rcs_tech_summary.pdf'
        output:
            True
    """
    isDownloadOk = False

    if not fileToSave:
        urlPartList = url.split("/")
        fileToSave = urlPartList[-1]  # 5g_message_rcs_tech_summary.pdf

    try:
        if isStreamMode:
            totalFileSize = getFileSizeFromUrl(url, proxies)  # 154551625
            if not totalFileSize:
                print("Failed to get total file size from %s" % url)
                return isDownloadOk

            totalSizeStr = formatSize(totalFileSize)
            print("Get total file size %s from %s" % (totalSizeStr, url))

            isDownloadedAndValid = isFileExistAndValid(
                fileToSave, fullFileSize=totalFileSize)
            if isDownloadedAndValid:
                print("%s is already download" % fileToSave)
                isDownloadOk = True
                return isDownloadOk

            curDownloadedSize = 0
            isExistFile = os.path.isfile(fileToSave)
            if isExistFile:
                curDownloadedSize = os.path.getsize(fileToSave)
                curDownloadedSizeStr = formatSize(curDownloadedSize)
                print("Already downloaded %s for %s" %
                      (curDownloadedSizeStr, fileToSave))

                if curDownloadedSize > totalFileSize:
                    # possible is local is new version, so consider as downloaded
                    print("Downloaded=%s > online=%s, consider as downloaded" %
                          (curDownloadedSizeStr, totalSizeStr))
                    isDownloadOk = True
                    return isDownloadOk

            if not isResume:
                curDownloadedSize = 0

            isDownloadOk = streamingDownloadFile(
                url,
                fileToSave=fileToSave,
                proxies=proxies,
                isShowSpeed=True,
                resumeSize=curDownloadedSize,
                totalSize=totalFileSize,
            )
        else:
            resp = requests.get(url, proxies=proxies)
            with open(fileToSave, 'wb') as saveFp:
                saveFp.write(resp.content)
                isDownloadOk = True
    except BaseException as curException:
        print("Exception %s when download %s to %s" %
              (curException, url, fileToSave))

    return isDownloadOk
Example #3
0
def streamingDownloadFile(
    url,
    fileToSave=None,
    proxies=None,
    isShowSpeed=True,
    chunkSize=1024 * 512,
    resumeSize=0,
    totalSize=0,
):
    """Download file using stream mode, support showing process with current speed, percent, size

    Args:
        url (str): file online url
        fileToSave (str): filename or full file path
        proxies (dict): requests proxies
        isShowSpeed (bool): show downloading speed or not
        chunkSize (int): when showing download speed, need use stream downloading, need set chunck size
        resumeSize (bool): the size to start download, normally is the local already downloaded size
        totalSize (int): total file size, only used for calculate downloaded percent
    Returns:
        download ok or not (bool)
    Raises:
    Examples:
    """
    isDownloadOk = False

    if isShowSpeed:
        if totalSize == 0:
            gotTotalSize = getFileSizeFromUrl(url, proxies)  # 154551625
            if gotTotalSize:
                totalSize = gotTotalSize

    headers = {
        'Range': 'bytes=%d-' % resumeSize,
        # "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36",
    }
    resp = requests.get(url, proxies=proxies, headers=headers, stream=True)
    curDownloadedSize = 0
    with open(fileToSave, "ab") as f:
        startTime = time.time()
        prevTime = startTime
        for curChunkBytes in resp.iter_content(chunk_size=chunkSize):
            if curChunkBytes:
                curTime = time.time()  # 1606456020.0718982
                f.write(curChunkBytes)
                f.flush()

                curChunkSize = len(curChunkBytes)  # 524288
                curDownloadedSize += curChunkSize  # 524288
                totalDownloadedSize = curDownloadedSize + resumeSize  # 12058624
                totalDownloadedSizeStr = formatSize(
                    totalDownloadedSize)  # '11.5MB'

                curDownloadTime = curTime - prevTime  # 15.63818907737732
                curSpeed = curChunkSize / curDownloadTime  # 670522.651191692
                curSpeedStr = formatSize(curSpeed)  # '231.3KB'

                totalDownloadTime = curTime - startTime  # 15.63818907737732
                averageSpeed = curDownloadedSize / totalDownloadTime  # 670522.651191692
                averageSpeedStr = formatSize(averageSpeed)  # '231.3KB'

                totalDownloadTimeDict = floatSecondsToDatetimeDict(
                    totalDownloadTime)
                totalDownloadTimeStr = datetimeDictToStr(
                    totalDownloadTimeDict, isShowMilliSecPart=False)

                if isShowSpeed:
                    showStr = "downloading speed: cur=%s/s, avg=%s/s, time: total=%s, size: %s" % (
                        curSpeedStr, averageSpeedStr, totalDownloadTimeStr,
                        totalDownloadedSizeStr)

                    if totalSize > 0:
                        downloadedPercent100 = round(
                            100 * totalDownloadedSize / totalSize, 2)  # 47.23
                        downloadedPercent100Str = str(
                            downloadedPercent100)  # '47.23'
                        percentStr = ", percent: %s%%" % downloadedPercent100Str  # ', percent: 47.23%'
                    else:
                        percentStr = ""

                    showStr += percentStr
                    # 'downloading speed: cur=231.3KB/s, avg=231.3KB/s, time: total=00:00:02, size: 11.5MB, percent: 49.38%'
                    print(showStr)

                prevTime = curTime

    return isDownloadOk