def uploadImageToWordpress(self, imgResource, isCheckExisted=False): """Upload image resource to wordpress Args: imgResource (Resouce): evernote image Resouce isCheckExisted (bool): whether check image is uploaded or not Returns: (bool, dict) Raises: """ respInfo = {} imgData = imgResource.data imgBytes = imgData.body imgDataSize = imgData.size # guid:'f6956c30-ef0b-475f-a2b9-9c2f49622e35' imgGuid = imgResource.guid logging.debug("imgGuid=%s, imgDataSize=%s", imgGuid, imgDataSize) curImg = utils.bytesToImage(imgBytes) logging.debug("curImg=%s", curImg) # # for debug # curImg.show() imgFormat = curImg.format # 'PNG' imgSuffix = utils.ImageFormatToSuffix[imgFormat] # 'png' imgMime = utils.ImageSuffixToMime[imgSuffix] # 'image/png' # curDatetimeStr = utils.getCurDatetimeStr() # '20200307_173141' processedGuid = imgGuid.replace( "-", "") # 'f6956c30ef0b475fa2b99c2f49622e35' # imgeFilename = "%s.%s" % (curDatetimeStr, imgSuffix) # '20200307_173141.png' imgeFilename = "%s.%s" % (processedGuid, imgSuffix ) # 'f6956c30ef0b475fa2b99c2f49622e35.png' isNeedUpload = True if isCheckExisted: generatedImgeUrl = self.wordpress.generateUploadedImageUrl( imgeFilename) # https://www.crifan.org/files/pic/uploads/2021/03/f60ea32cf4664b41922431f4ea015621.jpg # 'url':'https://www.crifan.org/files/pic/uploads/2021/03/f60ea32cf4664b41922431f4ea015621-1.jpg' isValid = utils.isValidImageUrl( generatedImgeUrl, proxies=self.wordpress.requestsProxies) if isValid: logging.info("Found existed image %s", generatedImgeUrl) isUploadImgOk = True respInfo["url"] = generatedImgeUrl isNeedUpload = False if isNeedUpload: isUploadImgOk, respInfo = self.wordpress.createMedia( imgMime, imgeFilename, imgBytes) logging.debug("%s to upload resource %s to wordpress", isUploadImgOk, imgGuid) return isUploadImgOk, respInfo
def uploadImageToWordpress(self, imgResource): """Upload image resource to wordpress Args: imgResource (Resouce): evernote image Resouce Returns: (bool, dict) Raises: """ imgData = imgResource.data imgBytes = imgData.body imgDataSize = imgData.size # guid:'f6956c30-ef0b-475f-a2b9-9c2f49622e35' imgGuid = imgResource.guid logging.debug("imgGuid=%s, imgDataSize=%s", imgGuid, imgDataSize) curImg = utils.bytesToImage(imgBytes) logging.debug("curImg=%s", curImg) # # for debug # curImg.show() imgFormat = curImg.format # 'PNG' imgSuffix = utils.ImageFormatToSuffix[imgFormat] # 'png' imgMime = utils.ImageSuffixToMime[imgSuffix] # 'image/png' # curDatetimeStr = utils.getCurDatetimeStr() # '20200307_173141' processedGuid = imgGuid.replace( "-", "") # 'f6956c30ef0b475fa2b99c2f49622e35' # imgeFilename = "%s.%s" % (curDatetimeStr, imgSuffix) # '20200307_173141.png' imgeFilename = "%s.%s" % (processedGuid, imgSuffix ) # 'f6956c30ef0b475fa2b99c2f49622e35.png' isUploadImgOk, respInfo = self.wordpress.createMedia( imgMime, imgeFilename, imgBytes) logging.debug("%s to upload resource %s to wordpress", isUploadImgOk, imgGuid) return isUploadImgOk, respInfo
def updateNoteImageResouces(noteDetail, newResList): """Update note resources and content, with new resource and updated <en-media> content Args: noteDetail (Note): Evernote note with details Returns: updated note detail Raises: """ validNewResList = [] originResList = noteDetail.resources # originContent = noteDetail.content # soup = BeautifulSoup(originContent, 'html.parser') soup = crifanEvernote.noteContentToSoup(noteDetail) # # for debug: try restore not show image # resSoupList = soup.find_all("en-media") for curIdx, curRes in enumerate(originResList): foundMediaNode = crifanEvernote.findResourceSoup(curRes, soup=soup) # # for debug: try restore not show image # if not foundMediaNode: # foundMediaNode = resSoupList[curIdx] if foundMediaNode: newRes = newResList[curIdx] # 'image/jpeg' validNewResList.append(newRes) newMime = newRes.mime # newHashBytes = newRes.data.bodyHash # b'\xb8\xe8\xbb\xcc\xca\xc1\xdf&J\xbeV\xe2`\xa6K\xb7' newResBytes = newRes.data.body newHashStr = utils.calcMd5( newResBytes) # '3da990710b87fcd56a84d644202b12c2' foundMediaNode["type"] = newMime foundMediaNode["hash"] = newHashStr noteAttrs = foundMediaNode.attrs # {'hash': '3da990710b87fcd56a84...44202b12c2', 'type': 'image/jpeg'} # {'hash': 'f9fc018211046ccc0eb6...5f67d0c8d6', 'type': 'image/jpeg', 'width': '385'} # special: # <en-media hash="0bbf1712d4e9afe725dd51e701c7fae6" style="width: 788px; height: auto;" type="image/jpeg"></en-media> # -> # {'hash': '0bbf1712d4e9afe725dd51e701c7fae6', 'type': 'image/jpeg', 'width': '788', 'height': 'auto'} ? hasWidthAttr = "width" in noteAttrs hasHeightAttr = "height" in noteAttrs if hasWidthAttr or hasHeightAttr: newImg = utils.bytesToImage(newResBytes) # newImgWidth = newImg.width # newImgHeight = newImg.height newImgWidth, newImgHeight = newImg.size if hasWidthAttr: attrWidthStr = noteAttrs["width"] if attrWidthStr.isdigit(): curWidth = int(attrWidthStr) if curWidth >= newImgWidth: # old size is bigger than resized image size, so need remove old size del foundMediaNode["width"] else: # is 'auto' ? -> keep not changed ? logging.warning( "not support ? img width value: %s", attrWidthStr) if hasHeightAttr: attrHeightStr = noteAttrs["height"] if attrHeightStr.isdigit(): curHeight = int(attrHeightStr) if curHeight >= newImgHeight: # old size is bigger than resized image size, so need remove old size del foundMediaNode["height"] else: # is 'auto' ? -> keep not changed ? logging.warning( "not support ? img height value: %s", attrHeightStr) else: # logging.warning("Not found resource: type=%s, hash=%s", curMime, curHashStr) logging.warning("Not found resource: guid=%s, mime=%s", curRes.guid, curRes.mime) # not add to validNewResList # newContent = soup.prettify() # newContent = str(soup) newContent = crifanEvernote.soupToNoteContent(soup) noteDetail.content = newContent # noteDetail.resources = newResList noteDetail.resources = validNewResList return noteDetail
def blurImageResource(self, imgRes, sensitiveInfoList): """Blur image resource Note: NOT process gif image Args: imgRes (Resource): Note image resource Returns: new image resouce Raises: """ isBlurred = False # isGif = "gif" in imgRes.mime # if isGif: # logging.info("Omit process gif image: %s", imgRes.attributes.fileName) # return imgRes imgBytes = imgRes.data.body matchResultDict = self.baiduOcr.isStrInImage(sensitiveInfoList, imgBytes=imgBytes, isMatchMultiple=True, isRespShortInfo=False) curImg = utils.bytesToImage(imgBytes) for eachSensitiveInfo, eachMatchResult in matchResultDict.items(): isMatch, matchResultList = eachMatchResult if isMatch: for eachMatchResultDict in matchResultList: matchStr, matchLocation = eachMatchResultDict # ' limao@xx1 ~/dev/crifan/gitbookgitbook_template/books/gitbook_demo master .make install', (49, 0, 72, 16) logging.debug("matchStr=%s, matchLocation=%s", matchStr, matchLocation) posX, posY, posW, posH = matchLocation # (49, 0, 72, 16) # matchBox = [posX, posY, posX + posW, posY + posH] # [49, 0, 121, 16] # Note: add extrac width (and height) to makesure all sensitve info blured extraWidth = 5 extraHeight = 2 matchBox = [ posX, posY, posX + posW + extraWidth, posY + posH + extraHeight ] cropBoxImg = curImg.crop( matchBox ) # <PIL.Image.Image image mode=RGBA size=72x16 at 0x101B9CA90> # Use GaussianBlur directly to blur the image 10 times blurImg = cropBoxImg.filter( ImageFilter.GaussianBlur(radius=10) ) # <PIL.Image.Image image mode=RGBA size=72x16 at 0x103530DF0> curImg.paste(blurImg, matchBox) # # for debug # curImg.show() isBlurred = True if isBlurred: logging.info("Blurred image: %s", imgRes.attributes.fileName) # # for debug # curImg.show() newImgBytes = utils.imageToBytes(curImg) newImgRes = crifanEvernote.genNewImgRes(imgRes, newImgBytes) return newImgRes