def notes_local(self): """Send local notes changes to server""" for note in self.sq(models.Note).filter( and_( models.Note.action != ACTION_NONE, models.Note.action != ACTION_NOEXSIST, models.Note.action != ACTION_CONFLICT, )): self.app.log('Note %s local' % note.title) content = (u""" <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> <en-note>%s</en-note> """ % sanitize(html=note.content[:EDAM_NOTE_CONTENT_LEN_MAX]) ).strip().encode('utf8') soup = BeautifulStoneSoup(content, selfClosingTags=[ 'img', 'en-todo', 'en-media', 'br', 'hr', ]) kwargs = dict( title=note.title[:EDAM_NOTE_TITLE_LEN_MAX].strip().encode( 'utf8'), content=soup.prettify(), tagGuids=map( lambda tag: tag.guid, note.tags, ), ) if note.notebook: kwargs['notebookGuid'] = note.notebook.guid if note.guid: kwargs['guid'] = note.guid nt = Note(**kwargs) try: next_action = ACTION_NONE if note.action == ACTION_CHANGE: nt.resources = self._resources_for_note(note) nt = self.note_store.updateNote(self.auth_token, nt) elif note.action == ACTION_CREATE: nt.resources = self._resources_for_note(note) nt = self.note_store.createNote(self.auth_token, nt) note.guid = nt.guid elif note.action == ACTION_DELETE: self.note_store.deleteNote(self.auth_token, nt.guid) self.session.delete(note) except EDAMUserException as e: next_action = ACTION_NONE self.app.log('Note %s failed' % note.title) self.app.log(e) note.action = next_action self.session.commit()
def notes_local(self): """Send loacl notes changes to server""" for note in self.sq(models.Note).filter(and_( models.Note.action != ACTION_NONE, models.Note.action != ACTION_NOEXSIST, )): self.app.log('Note %s local' % note.title) content = (u""" <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> <en-note>%s</en-note> """ % sanitize( html=note.content[:EDAM_NOTE_CONTENT_LEN_MAX] )).strip().encode('utf8') soup = BeautifulStoneSoup(content, selfClosingTags=[ 'img', 'en-todo', 'en-media', 'br', 'hr', ]) kwargs = dict( title=note.title[:EDAM_NOTE_TITLE_LEN_MAX].strip().encode('utf8'), content=soup.prettify(), tagGuids=map( lambda tag: tag.guid, note.tags, ), ) if note.notebook: kwargs['notebookGuid'] = note.notebook.guid if note.guid: kwargs['guid'] = note.guid nt = Note(**kwargs) try: next_action = ACTION_NONE if note.action == ACTION_CHANGE: nt.resources = self._resources_for_note(note) nt = self.note_store.updateNote(self.auth_token, nt) elif note.action == ACTION_CREATE: nt.resources = self._resources_for_note(note) nt = self.note_store.createNote(self.auth_token, nt) note.guid = nt.guid elif note.action == ACTION_DELETE: self.note_store.deleteNote(self.auth_token, nt.guid) self.session.delete(note) except EDAMUserException as e: next_action = note.action self.app.log('Note %s failed' % note.title) print soup.prettify() print e note.action = next_action self.session.commit()
def init_note(guid, content): # Initializing the note object note = Note() note.content = content note.guid = guid # Getting note title soup = BeautifulSoup(content, "html.parser") note.title = soup.title.text # Initializing variables resources = [] resource = None data = None binaryData = None hash = None # Getting note resources path = safeglobals.path_note % (guid, "") for filename in os.listdir(path): if "content.json" not in filename: # Reading binary data with open(os.path.join(path, filename), "rb") as f: binaryData = f.read() # Calculating hash md5 = hashlib.md5() md5.update(binaryData) hash = md5.digest() # Creating data data = Data() data.size = len(binaryData) data.bodyHash = hash data.body = binaryData # Create a new resource resource = Resource() resource.mime = getMime(filename) resource.data = data # Creating attributes attributes = ResourceAttributes() attributes.fileName = filename # Adding the resource to resource collection resource.attributes = attributes resources.append(resource) # Adding resources to the specified note note.resources = resources return note
def create_note(accessToken,title,content,notebookGuid,files,tags,password): # Generating note body content = safeglobals.ENCRYPTED_PREFIX+stringMD5(content)+"__"+encryptNote(content,password)+safeglobals.ENCRYPTED_SUFFIX nBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" nBody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" nBody += "<en-note>%s</en-note>" % content tagNames = [] # Creating note object note = Note() note.title = title.strip() note.content = nBody note.notebookGuid = notebookGuid for tag in tags: tagNames.append(str(tag).strip()) note.tagNames = tagNames # Processing resources resources = [] for file in files: if (file.get('name') and file.get('mime')): with open("static/tmp/"+file.get('name'),"rb") as f: # Calculating hash binaryData = encryptData(f.read(),password) md5 = hashlib.md5() md5.update(binaryData) hash = md5.digest() # Creating Evernote data object data = Data() data.size = len(binaryData) data.bodyHash = hash data.body = binaryData # Creating resource resource = Resource() resource.mime = file.get('mime') resource.data = data # Creating attributes attributes = ResourceAttributes() attributes.fileName = file.get('name') # Adding the resource to resource collection resource.attributes = attributes resources.append(resource) note.resources = resources return note
def createNote(self, title, resource=None): user_store = self.client.get_user_store() note_store = self.client.get_note_store() try: note = Note() note.tagNames = self.tag note.notebookGuid = self.bookguid if resource is not None: note.resources = [resource] self.content += "<span><en-media type=\"%s\" hash=\"%s\"/></span>" % ( resource.mime, resource.data.bodyHash) note.title = title note.content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" note.content += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" note.content += "<en-note>%s</en-note>" % self.content created_note = note_store.createNote(note) except: return False note_share_url = None note_share_resource_url = None if self.share: note_share_url = ToEver.getNoteShareUrl( sys_config.evernote_url, user_store.getUser().shardId, created_note.guid, note_store.shareNote(self.token, created_note.guid)) if resource is not None: for x in created_note.resources: note_share_resource_url = note_share_url + "/res/%s/%s" % ( x.guid, x.attributes.fileName) message = None if not self.hide: message = "Created note title is '" + title + "'" message += " [" + ToEver.getUserUploadState( note_store.getSyncState().uploaded, user_store.getUser().accounting.uploadLimitNextMonth) + "]" if note_share_url is not None: message += "\n" + "share link --> " + note_share_url if note_share_resource_url is not None: message += "\n" + "share attachment link --> " + note_share_resource_url elif note_share_url is not None: message = note_share_url if note_share_resource_url is not None: message += "\n" + note_share_resource_url if message is not None: print(textui.colored.blue(message)) return True
def make_note(client, noteTitle, noteBody, resources=[], guid=''): ## Build body of note body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" body += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" body += "<en-note>%s" % noteBody ourNote = Note() ourNote.title = noteTitle if len(resources) > 0: ourNote.resources = [] body += "<br />" * 2 for res in resources: src = res['src'] file = StringIO.StringIO(urllib.urlopen(src).read()) img = Image.open(file) # img = Image.open(file).resize((120,120)) output = io.BytesIO() img.save(output, format='png') im = output.getvalue() md5 = hashlib.md5() md5.update(im) hash = md5.digest() data = Types.Data() data.size = res['size'] data.bodyHash = hash data.body = im resource = Types.Resource() resource.mime = res['type'] resource.data = data ourNote.resources.append(resource) hash_hex = binascii.hexlify(hash) insert = "<br /><en-media type=\"%s\" hash=\"%s\" /><br />" % (resource.mime, hash_hex) body = body.replace('<p id="'+res['name']+'"></p>', insert) body += "</en-note>" ourNote.content = body token = client.token ourNote.notebookGuid = guid try: client = get_evernote_client(token=token) note_store = client.get_note_store() note = note_store.createNote(token, ourNote) except Errors.EDAMUserException, edue: print "EDAMUserException:", edue return None
def createNote(self, title, resource=None): user_store = self.client.get_user_store() note_store = self.client.get_note_store() try: note = Note() note.tagNames = self.tag note.notebookGuid = self.bookguid if resource is not None: note.resources = [resource] self.content += "<span><en-media type=\"%s\" hash=\"%s\"/></span>" % (resource.mime, resource.data.bodyHash) note.title = title note.content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" note.content += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" note.content += "<en-note>%s</en-note>" % self.content created_note = note_store.createNote(note) except: return False note_share_url = None note_share_resource_url = None if self.share: note_share_url = ToEver.getNoteShareUrl( sys_config.evernote_url, user_store.getUser().shardId, created_note.guid, note_store.shareNote(self.token, created_note.guid) ) if resource is not None: for x in created_note.resources: note_share_resource_url = note_share_url + "/res/%s/%s" % (x.guid, x.attributes.fileName) message = None if not self.hide: message = "Created note title is '" + title + "'" message += " [" + ToEver.getUserUploadState(note_store.getSyncState().uploaded, user_store.getUser().accounting.uploadLimitNextMonth) + "]" if note_share_url is not None: message += "\n" + "share link --> " + note_share_url if note_share_resource_url is not None: message += "\n" + "share attachment link --> " + note_share_resource_url elif note_share_url is not None: message = note_share_url if note_share_resource_url is not None: message += "\n" + note_share_resource_url if message is not None: print(textui.colored.blue(message)) return True
def notes_local(self): """Send loacl notes changes to server""" for note in self.sq(models.Note).filter( models.Note.action != ACTION_NONE, ): kwargs = dict( title=note.title[:EDAM_NOTE_TITLE_LEN_MAX].strip().encode('utf8'), content= (u""" <!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"> <en-note>%s</en-note> """ % note.content[:EDAM_NOTE_CONTENT_LEN_MAX]).strip().encode('utf8'), tagGuids=map( lambda tag: tag.guid, note.tags, ), ) if note.notebook: kwargs['notebookGuid'] = note.notebook.guid if note.guid: kwargs['guid'] = note.guid nt = Note(**kwargs) if note.action == ACTION_CHANGE: nt.resources = map(lambda res: Resource( noteGuid=note.guid, data=Data(body=open(res.file_path).read()), mime=res.mime, attributes=ResourceAttributes( fileName=res.file_name.encode('utf8'), ), ), self.sq(models.Resource).filter(and_( models.Resource.note_id == note.id, models.Resource.action != models.ACTION_DELETE, ))) nt = self.note_store.updateNote(self.auth_token, nt) elif note.action == ACTION_CREATE: nt = self.note_store.createNote(self.auth_token, nt) note.guid = nt.guid elif note.action == ACTION_DELETE: try: self.note_store.deleteNote(self.auth_token, nt.guid) self.session.delete(note) except EDAMUserException: pass note.action = ACTION_NONE self.session.commit()
def create_note_with_image(note_store, title, content, image_path): # To include an attachment such as an image in a note, first create a Resource # for the attachment. The Resource must contain the binary attachment # data, an MD5 hash of such data, and the attachment MIME type. # read image data with open(image_path, 'rb') as f: img = f.read() # create image data hash md5 = hashlib.md5() md5.update(img) h = md5.hexdigest() # now create an Evernote's Data object... data = Data() data.size = len(img) data.bodyHash = h data.body = img # ...that gets packed into a Resource object img_resource = Resource() img_resource.mime = 'image/png' img_resource.data = data # Now, create ENML content for the note enml_content = enml_for_note_with_attachments(content, [img_resource]) # Create and post a new note object note = Note() note.title = title note.content = enml_content note.resources = [img_resource] # don't forget to attach resources! created_note = None try: created_note = note_store.createNote(note) except EDAMUserException as e: print('Wrong ENML body: {}'.format(e)) finally: return created_note
def make_note_with_image(self, image_string): note = Note() note.title = 'Note created with ...' data = Data() data.body = image_string hash_md5 = hashlib.md5() hash_md5.update(data.body) data.bodyHash = hash_md5.digest() data.size = len(image_string) resource = Resource() resource.data = data resource.mime = 'image/jpeg' resource.width = 4160 resource.height = 3120 resource_attr = ResourceAttributes() resource_attr.attachment = False resource.attributes = resource_attr note.resources = [resource] hexhash = binascii.hexlify(resource.data.bodyHash) note.content = HackzurichEvernoteClient.NOTE_WITH_IMAGE.format( '', hexhash, 'image/jpeg', hexhash) note.notebookGuid = self.get_notebook_guid() try: created_note = self._note_store.createNote(note) except EDAMUserException as edue: print "EDAMUserException:", edue return None except EDAMNotFoundException: print "EDAMNotFoundException: Invalid parent notebook GUID" return None return created_note
def imglist2note(notestore, imglist, noteguid, notetitle, neirong=''): """ 更新note内容为图片列表 :param notestore: :param imglist: :param noteguid: :param notetitle: :param neirong:object :return: """ # global log note = Note() # print(type(note)) note.guid = noteguid note.title = notetitle # To include an attachment such as an image in a note, first create a Resource # for the attachment. At a minimum, the Resource contains the binary attachment # data, an MD5 hash of the binary data, and the attachment MIME type. # It can also include attributes such as filename and location. # Now, add the new Resource to the note's list of resources # print(len(note.resources)) # print(noteguid) # note.resources = notestore.getNote(token, noteguid, True, True, True,True).resources # evernoteapijiayi() # if not note.resources: # note.resources = [] note.resources = [] # print(len(note.resources)) # for img, imgtitle in imglist: for img in imglist: image = open(img, 'rb').read() md5 = hashlib.md5() md5.update(image) imghash = md5.digest() data = Data() # 必须要重新构建一个Data(),否则内容不会变化 data.size = len(image) data.bodyHash = imghash data.body = image resource = Resource() resource.mime = 'image/png' resource.data = data note.resources.append(resource) # The content of an Evernote note is represented using Evernote Markup Language # (ENML). The full ENML specification can be found in the Evernote API Overview # at http://dev.evernote.com/documentation/cloud/chapters/ENML.php nbody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" nbody += "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">" nbody += "<en-note>" if note.resources: # To display the Resource as part of the note's content, include an <en-media> # tag in the note's ENML content. The en-media tag identifies the corresponding # Resource using the MD5 hash. # nBody += "<br />" * 2 for resource in note.resources: if resource.guid or True: hexhash = binascii.hexlify(resource.data.bodyHash) str1 = "%s" % hexhash # b'cd34b4b6c8d9279217b03c396ca913df' # print (str1) str1 = str1[2:-1] # cd34b4b6c8d9279217b03c396ca913df # print (str1) nbody += "<en-media type=\"%s\" hash=\"%s\" align=\"center\" /><br />" % ( resource.mime, str1) nbody += neirong nbody += "</en-note>" note.content = nbody # print (note.content) # Finally, send the new note to Evernote using the updateNote method # The new Note object that is returned will contain server-generated # attributes such as the new note's unique GUID. @trycounttimes2('evernote服务器') def updatenote(notesrc): updated_note = get_notestore().updateNote(notesrc) evernoteapijiayi() log.info('成功更新了笔记《%s》,guid:%s。' % (updated_note.title, updated_note.guid)) updatenote(note)