def processListIndent(curNote): """process list (ul/ol/...) indent Args: curNote (Note): evernote Note Returns: Note Raises: """ # soup = utils.htmlToSoup(curNote.content) # enNoteSoup = soup.find("en-note") # soup = crifanEvernote.noteContentToSoup(curNote) # Note: makesure removed top html, then following can search out ul and ul list with recursive=False soup = crifanEvernote.noteContentToSoup(curNote, isKeepTopHtml=False) # allSubUlSoupList = soup.find_all("ul") # allSubUlSoupNum = len(allSubUlSoupList) # logging.info("Found %d all sub level ul list", allSubUlSoupNum) topUlSoupList = soup.find_all("ul", recursive=False) topOlSoupList = soup.find_all("ol", recursive=False) # for debug: need debug to make sure support ol if topOlSoupList: logging.info("Found ol list") topListSoupList = [] topListSoupList.extend(topUlSoupList) topListSoupList.extend(topOlSoupList) if topListSoupList: directUlSoupNum = len(topListSoupList) logging.info("Found %d top level ul/ol list", directUlSoupNum) for curUlIdx, eachListSoup in enumerate(topListSoupList): logging.info("%s %s %s", "-" * 20, curUlIdx, "-" * 20) logging.debug("before eachListSoup=%s", eachListSoup) crifanEvernoteToWordpress.removeDivInUlOl(eachListSoup) crifanEvernoteToWordpress.processSpanInUlOl(eachListSoup) logging.debug("after eachListSoup=%s", eachListSoup) # soup changed, write back to note content updatedNoteHtml = crifanEvernote.soupToNoteContent(soup) curNote.content = updatedNoteHtml else: logging.info("No ul/ol list for %s", curNote.title) return curNote
def syncNoteImage(self, curNoteDetail, curResource, uploadedImgUrl, curResList=None): """Sync uploaded image url into Evernote Note content, replace en-media to img Args: curNoteDetail (Note): evernote Note curResource (Resource): evernote Note Resource uploadedImgUrl (str): uploaded imge url, previously is Evernote Resource curResList (list): evernote Note Resource list Returns: updated note detail Raises: """ if not curResList: curResList = curNoteDetail.resources soup = crifanEvernote.noteContentToSoup(curNoteDetail) curEnMediaSoup = crifanEvernote.findResourceSoup(curResource, soup=soup) logging.debug("curEnMediaSoup=%s", curEnMediaSoup) # curEnMediaSoup=<en-media hash="0bbf1712d4e9afe725dd51e701c7fae6" style="width: 788px; height: auto;" type="image/jpeg"></en-media> if curEnMediaSoup: curImgSoup = curEnMediaSoup curImgSoup.name = "img" curImgSoup.attrs = {"src": uploadedImgUrl} logging.debug("curImgSoup=%s", curImgSoup) # curImgSoup=<img src="https://www.crifan.com/files/pic/uploads/2020/11/c8b16cafe6484131943d80267d390485.jpg"></img> # new content string updatedContent = crifanEvernote.soupToNoteContent(soup) logging.debug("updatedContent=%s", updatedContent) curNoteDetail.content = updatedContent else: logging.warning( "Not found en-media node for guid=%s, mime=%s, fileName=%s", curResource.guid, curResource.mime, curResource.attributes.fileName) # here even not found, still consider as processed, later will remove it # remove resource from resource list # oldResList = curNoteDetail.resources # Note: avoid side-effect: alter pass in curNoteDetail object's resources list # which will cause caller curNoteDetail.resources loop terminated earlier than expected ! # oldResList = copy.deepcopy(curNoteDetail.resources) # oldResList.remove(curResource) # workable # newResList = oldResList # Note 20201206: has update above loop, so should directly update curNoteDetail.resources # curNoteDetail.resources.remove(curResource) # newResList = curNoteDetail.resources curResList.remove(curResource) newResList = curResList # # for debug # if not newResList: # logging.info("empty resources list") syncParamDict = { # mandatory "noteGuid": curNoteDetail.guid, "noteTitle": curNoteDetail.title, # optional "newContent": curNoteDetail.content, "newResList": newResList, } respNote = self.evernote.syncNote(**syncParamDict) # logging.info("Completed sync image %s to evernote note %s", uploadedImgUrl, curNoteDetail.title) logging.info("Completed sync image %s", uploadedImgUrl) return respNote
def batchUploadAndSyncImage(self, curNoteDetail, isCheckExisted): """Batch upload image to wordpress and sync to note (replace en-media to img) Args: curNoteDetail (Note): evernote Note isCheckExisted (bool): whether check image is uploaded or not Returns: resp Note Raises: """ originResList = copy.deepcopy(curNoteDetail.resources) totalResNum = len(originResList) logging.info("Total resources: %d", totalResNum) latestResList = copy.deepcopy(curNoteDetail.resources) soup = crifanEvernote.noteContentToSoup(curNoteDetail) for curResIdx, eachResource in enumerate(originResList): curResNum = curResIdx + 1 logging.info("%s resource %d/%d %s", "-" * 20, curResNum, totalResNum, "-" * 20) curResInfoStr = crifanEvernote.genResourceInfoStr(eachResource) if self.evernote.isValidImageResource(eachResource): curEnMediaSoup = crifanEvernote.findResourceSoup(eachResource, soup=soup) if not curEnMediaSoup: logging.warning( "NOT upload for not found related <en-media> node for %s", curResInfoStr) continue isUploadOk, respInfo = self.uploadImageToWordpress( eachResource, isCheckExisted) if isUploadOk: uploadedImgUrl = respInfo["url"] logging.info("Uploaded image url %s", uploadedImgUrl) curImgSoup = curEnMediaSoup curImgSoup.name = "img" curImgSoup.attrs = {"src": uploadedImgUrl} latestResList.remove(eachResource) else: logging.error( "Failed to upload image resource=%s, respInfo=%s", curResInfoStr, respInfo) else: logging.warning("NOT upload for non-image resource: %s", curResInfoStr) updatedContent = crifanEvernote.soupToNoteContent(soup) newResList = latestResList syncParamDict = { # mandatory "noteGuid": curNoteDetail.guid, "noteTitle": curNoteDetail.title, # optional "newContent": updatedContent, "newResList": newResList, } respNote = self.evernote.syncNote(**syncParamDict) return respNote