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 getResource(self, filename): data = Data() data.body = sys.stdin.read() data.size = len(data.body) data.bodyHash = hashlib.md5(data.body).hexdigest() resource = Resource() resource.mime = mimetypes.guess_type(filename)[0] resource.data = data attr = ResourceAttributes() attr.fileName = filename resource.attributes = attr return resource
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 test_sync_add_note_with_res(cli_invoker, mock_evernote_client, fake_storage): mock_evernote_client.fake_notebooks.append( Notebook( guid="nbid1", name="name1", stack="stack1", serviceUpdated=1000, ), ) test_note = Note( guid="id1", title="title1", content="body1", notebookGuid="nbid1", active=True, contentLength=100, resources=[ Resource( guid="rid2", noteGuid="id1", data=Data(bodyHash=md5(b"000").digest(), size=3, body=b"000"), ) ], ) mock_evernote_client.fake_notes.append(test_note) cli_invoker("sync", "--database", "fake_db") result_notes = list(fake_storage.notes.iter_notes("nbid1")) assert result_notes == [test_note]
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 _resources_for_note(self, note): return 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, )))
def create_resource(path): # Initializing variables hash = None binary_data = None resource = None data = None # Checking if the file exists if os.path.isfile(path) == False: return None # Reading the file content with open(path,"rb") as f: binary_data = f.read() # Calculating hash md5 = hashlib.md5() md5.update(binary_data) hash = md5.digest() # Initializing the data data = Data() data.size = len(binary_data) data.bodyHash = hash data.body = binary_data # Adding data to resource resource = Resource() resource.mime = getMime(os.path.basename(path)) resource.data = data # Creating attributes attributes = ResourceAttributes() attributes.fileName = os.path.basename(path) resource.attributes = attributes return resource
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
active=True, updateSequenceNum=6711, notebookGuid="c2ab541f-b704-4051-a2fa-40805e0fbf74", tagGuids=[ "a51d61c3-8ff6-475f-b7ac-d72caf2ec84d", "9e7d0ea5-9ff8-46c7-9b43-ccc468ba1adb", ], resources=[ Resource( guid="fe747857-92ea-4633-b415-6b9946f67519", noteGuid="7473cb3f-411e-4545-9df4-5eb731de4358", mime="image/png", width=403, height=613, active=True, recognition=Data(bodyHash=b"1234", size=4332, body=b"1234"), data=Data(bodyHash=b"1234", size=58387, body=b"1234"), updateSequenceNum=6461, attributes=ResourceAttributes( fileName="test.png", attachment=True, ), ) ], attributes=NoteAttributes( author="*****@*****.**", source="desktop.win", sourceURL="https://www.example.com/page?category=blog&post_id=123", sourceApplication="evernote.win32", ), tagNames=["test1", "test2"],
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)
def download_page_resources(access_token, content, guid): # Getting note title and content soup = BeautifulSoup(content, "html.parser") # Getting note resources attachments = [] images = soup.find_all("img") for image in images: attachments.append({ "name": stringMD5(image['src']), "type": image['data-src-type'], "link": image['src'] }) objects = soup.find_all("object") for obj in objects: attachments.append({ "name": obj['data-attachment'], "type": obj['type'], "link": obj['data'] }) # Downloading all resources into tmp folder binary_data = None hash = None resources = [] tmp_file = os.path.join(safeglobals.path_tmp, "tmp.download") headers = {"Authorization": "Bearer " + access_token} for attachment in attachments: if resource_exists(guid, attachment['name']) == False: r = requests.get(attachment['link'], headers=headers, stream=True) if r.ok: # Writing data to temporary file with open(tmp_file, "wb") as f: for block in r.iter_content(512): f.write(block) # Calculating hash with open(tmp_file, "rb") as f: binary_data = f.read() md5 = hashlib.md5() md5.update(binary_data) hash = md5.digest() # Initializing the data data = Data() data.size = len(binary_data) data.bodyHash = hash data.body = binary_data # Adding data to resource resource = Resource() resource.mime = attachment['type'] resource.data = data # Creating attributes attributes = ResourceAttributes() attributes.fileName = os.path.basename(attachment['name']) resource.attributes = attributes resources.append(resource) else: log_message(safeglobals.ERROR_FILE_DOWNLOAD % (str(r.status_code))) return resources