Beispiel #1
0
    def _create_remote_note(self, note_title, note_guid):
        """Create remote note"""
        remote_note = ttypes.Note(
            title=note_title,
            guid=note_guid,
            content='<en-note></en-note>',
            notebookGuid=self.notebook.guid,
            attributes=ttypes.NoteAttributes(),
            updated=1,
            resources=[
                ttypes.Resource(
                    guid='file',
                    mime='text',
                    attributes=ttypes.ResourceAttributes(fileName='file', ),
                    data=ttypes.Data(
                        body='',
                        bodyHash='',
                    ),
                )
            ],
        )

        search_result = MagicMock()
        search_result.totalNotes = 1
        search_result.startIndex = 0
        search_result.notes = [remote_note]
        self.note_store.findNotes.return_value = search_result
        self.note_store.getNote.return_value = remote_note

        return remote_note
Beispiel #2
0
def file2evernote(filename,
                  data,
                  mimetype,
                  notebook=None,
                  tags=[],
                  title=None):
    print("Creating note for file {0} in notebook {1}".format(
        filename, notebook))

    guid = get_notebook_by_name(notebook).guid

    if not title:
        title = filename

    resource = Types.Resource()
    resource.data = Types.Data()
    resource.data.body = data
    resource.attributes = Types.ResourceAttributes(fileName=filename,
                                                   attachment=False)
    resource.mime = mimetype
    hash = hashlib.md5()
    hash.update(resource.data.body)
    attachment = '<en-media type="{0}" hash="{1}" />\n'.format(
        resource.mime, hash.hexdigest())

    content = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>""" + attachment + "</en-note>"

    note = Types.Note(title=title,
                      content=content,
                      tagNames=tags,
                      resources=[resource],
                      notebookGuid=guid)
    return noteStore.createNote(note)
Beispiel #3
0
 def getSchemaResource(self):
     if not os.path.exists(base_path):
         os.mkdir(base_path)
     if not os.path.exists(base_path + str(self.guid)):
         os.mkdir(base_path + str(self.guid))
     #create knowledge.json
     file_name = base_path + str(self.guid) + '/knowledge.json'
     if not os.path.exists(file_name):
         with open(file_name, 'wb') as f:
             json.dump({}, f)
     #compute hash value
     raw = open(file_name, 'rb').read()
     md5 = hashlib.md5()
     md5.update(raw)
     hash = md5.digest()
     #buid Data
     data = Types.Data()
     data.size = len(raw)
     data.bodyHash = hash
     data.body = raw
     #build attribute
     attr = Types.ResourceAttributes()
     attr.fileName = "knowledge.json"
     attr.clientWillIndex = False
     attr.attachment = True
     #build Resources
     resource = Types.Resource()
     resource.mime = 'text/json'
     resource.data = data
     resource.attributes = attr
     return resource
def create_new_note(file_path, mime):
    # import pdb; pdb.set_trace()
    file_name = os.path.basename(file_path)
    file_cont = open(file_path, "rb").read()
    md5 = hashlib.md5()
    md5.update(file_cont)
    hash = md5.digest()
    
    data = Types.Data()
    data.size = len(file_cont)
    data.bodyHash = hash
    data.body = file_cont

    attr = Types.ResourceAttributes()
    attr.fileName = file_name

    resource = Types.Resource()
    resource.mime = mime
    resource.data = data
    resource.attributes = attr

    note = Types.Note()
    note.title = file_name
    note.resources = [resource]
    note.notebookGuid = notebook_id
    note.content = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note>
<ul>
<li></li>
</ul>
<hr/>
<en-media type="%(mime)s" hash="%(hash)s" />
</en-note>""" % {"mime":mime, "hash":binascii.hexlify(hash)}
    note_store.createNote(auth_token, note)
Beispiel #5
0
 def create_note(self, noteFullPath, content=None, fileDir=None):
     if self.get(noteFullPath): return False
     if '/' in noteFullPath:
         notebook = noteFullPath.split('/')[0]
         title = noteFullPath.split('/')[1]
     else:
         notebook = self.storage.defaultNotebook
         title = noteFullPath
     note = Types.Note()
     note.title = title
     note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
     note.content += '<en-note>'
     note.content += content or ''
     if self.get(notebook) is None: self.create_notebook(notebook)
     note.notebookGuid = self.get(notebook).guid
     if not fileDir is None:
         with open(fileDir, 'rb') as f: fileBytes = f.read()
         fileData = Types.Data()
         fileData.bodyHash = self._md5(fileBytes)
         fileData.size = len(fileBytes)
         fileData.body = fileBytes
         fileAttr = Types.ResourceAttributes()
         fileAttr.fileName = title + '.md'
         fileAttr.attachment = True
         fileResource = Types.Resource()
         fileResource.data = fileData
         fileResource.mime = 'application/octet-stream'
         fileResource.attributes = fileAttr
         note.resources = [fileResource]
         note.content += '<en-media type="application/octet-stream" hash="%s"/>' % fileData.bodyHash
     note.content += '</en-note>'
     note = self.noteStore.createNote(note)
     self.storage.create_note(note, notebook)
     return True
Beispiel #6
0
 def test_remote_resources(self):
     """Test syncing remote resources"""
     remote = self._create_remote_note(
         resources=[ttypes.Resource(
             data=ttypes.Data(body=open(resource_path).read()),
             mime='image/png',
             attributes=ttypes.ResourceAttributes(
                 fileName='test.png',
             )
         )],
     )
     self.sync.notes_remote()
     resource = self.sq(Note).filter(
         Note.guid == remote.guid,
     ).one().resources[0]
     self.assertEqual(
         'test.png', resource.file_name,
     )
     remote.resources = None
     self.note_store.updateNote(self.auth_token, remote)
     self.sync.notes_remote()
     with self.assertRaises(NoResultFound):
         # fetch synchronized not very genius, but we don't need that
         get_db_session().query(Resource).filter(
             Resource.guid == resource.guid,
         ).one()
Beispiel #7
0
        def make_resource(filename):
            try:
                mtype = mimetypes.guess_type(filename)[0]
                    
                if mtype.split('/')[0] == "text":
                    rmode = "r"
                else:
                    rmode = "rb"

                with open(filename, rmode) as f:
                    """ file exists """
                    resource = Types.Resource()
                    resource.data = Types.Data()

                    data = f.read()
                    md5 = hashlib.md5()
                    md5.update(data)
                    
                    resource.data.bodyHash = md5.hexdigest() 
                    resource.data.body = data
                    resource.data.size = len(data) 
                    resource.mime = mtype
                    resource.attributes = Types.ResourceAttributes()
                    resource.attributes.fileName = os.path.basename(filename)
                    return resource
            except IOError:
                msg = "The file '%s' does not exist." % filename
                out.failureMessage(msg)
                raise IOError(msg)
Beispiel #8
0
 def open_note(self, guid, insert_in_content=True, filename=None, prompt=False, **unk_args):
     import hashlib, mimetypes
     if filename is None:
         view = self.view
         if view is None:
             sublime.error_message("Evernote plugin could not open the file you specified!")
             return
         filename = view.file_name() or ""
         contents = view.substr(sublime.Region(0, view.size())).encode('utf8')
     else:
         filename = os.path.abspath(filename)
         if prompt:
             self.window.show_input_panel(
                 "Filename of attachment: ", filename,
                 lambda x: self.open_note(guid, insert_in_content, filename, prompt=False, **unk_args),
                 None, None)
             return
         try:
             with open(filename, 'rb') as content_file:
                 contents = content_file.read()
         except Exception as e:
             sublime.error_message("Evernote plugin could not open the file you specified!")
             print(e)
             return
     try:
         noteStore = self.get_note_store()
         note = noteStore.getNote(self.token(), guid, True, False, False, False)
         mime = mimetypes.guess_type(filename)[0]
         LOG(mime)
         h = hashlib.md5(contents)
         if not isinstance(mime, str):
             mime = "text/plain"
         attachment = Types.Resource(
             # noteGuid=guid,
             mime=mime,
             data=Types.Data(body=contents, size=len(contents), bodyHash=h.digest()),
             attributes=Types.ResourceAttributes(
                 fileName=os.path.basename(filename),
                 attachment=True))
         resources = note.resources or []
         resources.append(attachment)
         if insert_in_content and note.content.endswith("</en-note>"):  # just a precaution
             builtin = note.content.find(SUBLIME_EVERNOTE_COMMENT_BEG, 0, 150)
             if builtin >= 0:
                 builtin_end = note.content.find(SUBLIME_EVERNOTE_COMMENT_END, builtin)
                 content = note.content[0:builtin]+note.content[builtin_end+len(SUBLIME_EVERNOTE_COMMENT_END)+1:]
             else:
                 content = note.content
             note.content = content[0:-10] + \
                 '<en-media type="%s" hash="%s"/></en-note>' % (mime, h.hexdigest())
         note.resources = resources
         def do():
             noteStore.updateNote(self.token(), note)
             self.message("Successfully attached to note '%s'" % note.title)
         async_do(do, "Uploading attachment")
     except Exception as e:
         sublime.error_message(explain_error(e))
Beispiel #9
0
 def clone_resource(self, resource):
     """Return a cloned Resource instance from given resource instance."""
     data = copy.deepcopy(resource.data)
     if not data.body:
         logger.info(u'Fetching resource data for cloning')
         data.body = self._get_resource_data(resource.guid)
     return Types.Resource(
         data=data, mime=resource.mime, attributes=
         Types.ResourceAttributes(fileName=resource.attributes.fileName))
Beispiel #10
0
    def create_note(self,
                    auth_token,
                    text,
                    title=None,
                    files=None,
                    notebook_guid=None):
        if files is None:
            files = []
        sdk = EvernoteSdk(token=auth_token, sandbox=self.sandbox)
        # user_store = sdk.get_user_store()
        note_store = sdk.get_note_store()
        note = Types.Note()
        note.title = title or ('%s...' % text[:25] if len(text) > 30 else text)
        if notebook_guid is not None:
            note.notebookGuid = notebook_guid

        attachments = []
        for filename, mime_type in files:
            with open(filename, 'rb') as f:
                data_bytes = f.read()
                md5 = hashlib.md5()
                md5.update(data_bytes)

                data = Types.Data()
                data.size = len(data_bytes)
                data.bodyHash = md5.digest()
                data.body = data_bytes

                resource = Types.Resource()
                resource.mime = mime_type
                resource.data = data
                resource.attributes = Types.ResourceAttributes(
                    fileName=basename(filename))

                # Now, add the new Resource to the note's list of resources
                note.resources = [resource]

            attachments.append(
                '<br /><en-media type="%(mime_type)s" hash="%(md5)s" />' % {
                    'mime_type': mime_type,
                    'md5': md5.hexdigest(),
                })

        note.content = '<?xml version="1.0" encoding="UTF-8"?>' \
            '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' \
            '<en-note>%(text)s<br />%(attachments)s</en-note>' % {
                'text': text,
                'attachments': ''.join(attachments),
            }
        try:
            created_note = note_store.createNote(note)
        except Exception as e:
            print(e)
            logging.getLogger().error(traceback.format_exc())
        return created_note.guid
    def add_resource(self, filename):
        """
        Adds a resource (image or pdf at this stage) to the note. Adds a new line at the end of the attachment
        :param filename: name of the file to add to the note
        :type filename: string
        :return:
        """
        accepted_mimes = {
            'gif': 'image/gif',
            'jpg': 'image/jpeg',
            'jpeg': 'image/jpeg',
            'png': 'image/png',
            'pdf': 'application/pdf',
            'html': 'text/html'
        }
        # ^these are mime types that can be displayed inline in the client. Also the only type that will be directly
        # downloaded by the downloader. Will need more testing. Others can be added later.
        extention = filename.split('.')[-1]
        mime_type = accepted_mimes[extention]
        try:
            with open(filename, 'rb') as f:
                attachment = f.read()
        except OSError:
            print("Unable to open file")
            return

        # hashing the attachment
        md5 = hashlib.md5()
        md5.update(attachment)
        hashed_resource = md5.digest()
        # adding the attachment as data Type
        data = Types.Data()
        data.size = len(attachment)
        data.bodyHash = hashed_resource
        data.body = attachment
        # adding the data to the note.
        resource = Types.Resource()
        resource.mime = mime_type
        resource.data = data
        resource.attributes = Types.ResourceAttributes()
        resource.attributes.fileName = filename
        # resource.attachment = True

        if not self.note.resources:
            self.note.resources = [resource]
        else:
            self.note.resources += [resource]

        hash_hex = binascii.hexlify(hashed_resource)
        hash_str = hash_hex.decode("UTF-8")

        self.note.content += '<en-media type="{}" hash="{}"/><br/>'.format(
            mime_type, hash_str)
        return
Beispiel #12
0
    def tagsMap_creater(self):
        #creat in local
        #tags=self.noteStore.listTags(self.token)
        #f=open('advancedTagsMap.csv','w+')
        #for tag in tags: f.write(str(tag.updateSequenceNum)+','+tag.guid+','+tag.name+','+str(tag.parentGuid)+'\n')
        #f.close()

        #1 prepare resourse:tagmapcsv
        ##create in memory
        tagmapcsv = Types.Resource()
        tagmapcsv.data = Types.Data()
        ##import data.body
        tags = self.noteStore.listTags(self.token)
        tagmapcsv.data.body = ""
        for tag in tags:
            bodyline = str(tag.updateSequenceNum
                           ) + ',' + tag.guid + ',' + tag.name + ',' + str(
                               tag.parentGuid) + '\n'
            tagmapcsv.data.body += bodyline
        ##calculate data.hash: and hexhash
        m2 = hashlib.md5()
        m2.update(tagmapcsv.data.body)
        tagmapcsv.data.bodyHash = m2.digest()
        hexhash = binascii.hexlify(tagmapcsv.data.bodyHash)
        ##set the file
        tagmapcsv.mime = 'application/csv'
        ratt = Types.ResourceAttributes()
        ratt.fileName = "Advanced Tags Mapping.csv"
        ratt.attachment = True
        tagmapcsv.attributes = ratt
        #tagmapcsv.attributes.append(ratt)

        #2 add resource to main content
        note = Types.Note()
        note.title = 'Advanced Tags Mapping'
        note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
        note.content += "<en-note>"
        note.content += "<br /><en-media hash=\"%s\" type=\"%s\" /><br />" % (
            hexhash, tagmapcsv.mime)
        note.content += "</en-note>"

        #3 add resource to resource list
        if note.resources == None: note.resources = []
        note.resources.append(tagmapcsv)

        #4 upload note
        note = self.noteStore.createNote(self.token, note)

        #print("")
        return note
Beispiel #13
0
def getResource(filepath):
    filename = os.path.basename(filepath)
    data = Types.Data()
    data.body = open(filepath, 'r').read()
    data.size = len(data.body)
    data.bodyHash = hashlib.md5(data.body).hexdigest()
    resource = Types.Resource()
    resource.mime = mimetypes.guess_type(filename)[0]
    resource.data = data
    attr = Types.ResourceAttributes()
    attr.fileName = filename
    resource.attributes = attr
    # print('attachment: ' + filename)
    return resource
Beispiel #14
0
 def _prepare_resources(self, note):
     """Prepare note resources"""
     return map(
         lambda resource: ttypes.Resource(
             noteGuid=note.guid,
             data=ttypes.Data(body=open(resource.file_path).read()),
             mime=resource.mime,
             attributes=ttypes.ResourceAttributes(
                 fileName=resource.file_name.encode('utf8'), ),
         ),
         self.session.query(models.Resource).filter(
             (models.Resource.note_id == note.id)
             & (models.Resource.action != const.ACTION_DELETE)),
     )
Beispiel #15
0
 def open_note(self, guid, insert_in_content=True, **unk_args):
     import hashlib, mimetypes
     view = self.window.active_view()
     filename = view.file_name() or ""
     if view is None:
         self.message("You need to open the file to be attached first!")
         return
     contents = view.substr(sublime.Region(0, view.size())).encode('utf8')
     try:
         noteStore = self.get_note_store()
         note = noteStore.getNote(self.token(), guid, True, False, False,
                                  False)
         mime = mimetypes.guess_type(filename)
         h = hashlib.md5(contents)
         if not isinstance(mime, str):
             mime = "text/plain"
         attachment = Types.Resource(
             # noteGuid=guid,
             mime=mime,
             data=Types.Data(body=contents,
                             size=len(contents),
                             bodyHash=h.digest()),
             attributes=Types.ResourceAttributes(
                 fileName=os.path.basename(filename), attachment=True))
         resources = note.resources or []
         resources.append(attachment)
         if insert_in_content and note.content.endswith(
                 "</en-note>"):  # just a precaution
             builtin = note.content.find(SUBLIME_EVERNOTE_COMMENT_BEG, 0,
                                         150)
             if builtin >= 0:
                 builtin_end = note.content.find(
                     SUBLIME_EVERNOTE_COMMENT_END, builtin)
                 content = note.content[0:builtin] + note.content[
                     builtin_end + len(SUBLIME_EVERNOTE_COMMENT_END) + 1:]
             else:
                 content = note.content
             note.content = content[0:-10] + \
                 '<en-media hash="%s" type="%s"/></en-note>' % (h.hexdigest(), mime)
         note.resources = resources
         noteStore.updateNote(self.token(), note)
         self.message("Succesfully attached to note '%s'" % note.title)
     except Errors.EDAMNotFoundException as e:
         sublime.error_message(
             "The note with the specified guid could not be found.")
     except Errors.EDAMUserException:
         sublime.error_message(
             "The specified note could not be found.\nPlease check the guid is correct."
         )
Beispiel #16
0
 def _create_evernote_resource(attachment_filename,
                               byte_data,
                               source_url=None):
     data = Types.Data(
         bodyHash=hashlib.md5(byte_data).hexdigest(),
         size=len(byte_data),
         body=byte_data,
     )
     return Types.Resource(
         data=data,
         mime=mimetypes.guess_type(attachment_filename)[0],
         attributes=Types.ResourceAttributes(
             sourceURL=source_url,
             fileName=attachment_filename,
         ),
     )
    def _make_resource(self, filename, mime_type):
        with open(filename, 'rb') as f:
            data_bytes = f.read()
            md5 = hashlib.md5()
            md5.update(data_bytes)

            data = Types.Data()
            data.size = len(data_bytes)
            data.bodyHash = md5.digest()
            data.body = data_bytes

            resource = Types.Resource()
            resource.mime = mime_type
            resource.data = data
            short_name = basename(filename)
            resource.attributes = Types.ResourceAttributes(fileName=short_name)
        return resource, md5.hexdigest()
Beispiel #18
0
def create_resource(pdf, typ='application/pdf'):
    m5hash = hashlib.md5()
    resource = ttypes.Resource()
    rattr = ttypes.ResourceAttributes()
    rattr.fileName = os.path.basename(pdf)
    resource.mime = typ
    resource.attributes = rattr
    data = ttypes.Data()
    with open(pdf, 'rb') as f:
        contents = f.read()
    m5hash.update(contents)
    h = m5hash.hexdigest()
    data.bodyHash = h
    data.size = len(contents)
    data.body = contents
    resource.data = data
    return resource, h
Beispiel #19
0
    def resourse_append(self, note_in, resource_str, resource_name):
        #0 preparation
        note = Types.Note
        note.guid = ""
        if type(note_in) == type(note):
            note = note_in
            print("append and update")
        if type(note_in) == type(note.guid):
            note.guid = note_in
            note = self.noteStore.getNote(self.token, note.guid, True, True,
                                          True, True)
            print("load, append and update")

        #1 prepare resource
        ## create in memory
        resource = Types.Resource()
        resource.data = Types.Data()
        ## import data.body
        resource.data.body = resource_str
        ## calculate data.hash: and hexhash
        m2 = hashlib.md5()
        m2.update(resource.data.body)
        resource.data.bodyHash = m2.digest()
        hexhash = binascii.hexlify(resource.data.bodyHash)
        ## set the file
        resource.mime = 'application/octet-stream'
        ratt = Types.ResourceAttributes()
        ratt.fileName = resource_name
        ratt.attachment = True
        resource.attributes = ratt
        #resource.attributes.append(ratt)

        #2 add resource tag to the end of main content
        resource_tag_start = note.content.rfind("</en-note>")
        newcontent = note.content[0:resource_tag_start]
        newcontent += "<br /><en-media hash=\"%s\" type=\"%s\" /><br />" % (
            hexhash, resource.mime)
        newcontent += note.content[resource_tag_start:]
        note.content = newcontent
        #3 add resource to list
        if note.resources == None: note.resources = []
        note.resources.append(resource)

        #4 update note
        self.noteStore.updateNote(self.token, note)
        print("appended one resource at the end of note page")
Beispiel #20
0
    def _create_evernote_note(self, notebook, filename):
        # Create the new note
        note = Types.Note()
        note.title = os.path.basename(filename)
        note.notebookGuid = notebook.guid
        note.content = '<?xml version="1.0" encoding="UTF-8"?>' \
            '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
        note.content += '<en-note>Uploaded by PyPDFOCR <br/>'

        logging.debug("Loading PDF")
        md5 = hashlib.md5()
        with open(filename, 'rb') as f:
            pdf_bytes = f.read()

        logging.debug("Calculating md5 checksum of pdf")
        md5.update(pdf_bytes)
        md5hash = md5.hexdigest()

        logging.debug("Uploading note")

        # Create the Data type for evernote that goes into a resource
        pdf_data = Types.Data()
        pdf_data.bodyHash = md5hash
        pdf_data.size = len(pdf_bytes)
        pdf_data.body = pdf_bytes

        # Add a link in the evernote boy for this content
        link = '<en-media type="application/pdf" hash="%s"/>' % md5hash
        logging.debug(link)
        note.content += link
        note.content += '</en-note>'

        resource_list = []
        pdf_resource = Types.Resource()
        pdf_resource.data = pdf_data
        pdf_resource.mime = "application/pdf"
        # TODO: Enable filename
        # Make a attributes for this resource
        pdf_resource.attributes = Types.ResourceAttributes()
        pdf_resource.attributes.fileName = os.path.basename(filename)
        resource_list.append(pdf_resource)

        note.resources = resource_list

        return note
Beispiel #21
0
 def update_note(self, noteFullPath, content=None, fileDict={}):
     note = self.get(noteFullPath)
     if note is None:
         return self.create_note(noteFullPath, content or '', fileDict)
     if 1 < len(noteFullPath):
         notebook = noteFullPath[0]
         title = noteFullPath[1]
     else:
         notebook = self.storage.defaultNotebook
         title = noteFullPath[0]
     oldContent = self.get_content(noteFullPath)
     content = content or oldContent
     header = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
     guid = note.guid
     content = re.sub('<en-media.*?/>', '', content)
     note = Types.Note()
     note.guid = guid
     note.title = title
     note.content = header
     note.content += '<en-note>'
     note.content += content
     if fileDict:
         note.resources = []
         for fileName, fileBytes in fileDict.items():
             fileData = Types.Data()
             fileData.bodyHash = self._md5(fileBytes)
             fileData.size = len(fileBytes)
             fileData.body = fileBytes
             fileAttr = Types.ResourceAttributes()
             fileAttr.fileName = fileName
             fileAttr.attachment = True
             fileResource = Types.Resource()
             fileResource.data = fileData
             fileResource.mime = mimetypes.guess_type(
                 fileName)[0] or 'application/octet-stream'
             fileResource.attributes = fileAttr
             note.resources.append(fileResource)
             note.content += '<en-media type="%s" hash="%s"/>' % (
                 fileResource.mime, fileData.bodyHash)
     note.content += '</en-note>'
     self.noteStore.updateNote(self.token, note)
     self.storage.delete_note(noteFullPath)
     self.storage.create_note(note, notebook)
     return True
Beispiel #22
0
 def makeResource(src_file, filename, mime=None):
     if not mime:
         mime = mimetypes.guess_type(filename)[0]
     if not mime:
         mime = u'application/octet-stream'
         logger.warning(u'Failed guessing mimetype for "%s" '
                         '(defaulting to "%s")' % (filename, mime))
     # mimetype workarounds:
     if 'image/x-png' == mime:
         # seems like Evernote (Windows) will display
         #  the image inline in the note only this way.
         mime = 'image/png'
     if 'image/pjpeg' == mime:
         # seems like Evernote (Windows) will display
         #  the image inline in the note only this way.
         mime = 'image/jpeg'
     body = src_file.read()
     data = Types.Data(body=body, size=len(body),
                       bodyHash=hashlib.md5(body).digest())
     attr = Types.ResourceAttributes(fileName=filename.encode('utf-8'))
     resource = Types.Resource(data=data, mime=mime, attributes=attr)
     return resource, EvernoteApiWrapper.get_resource_tag(resource)
    def make_resource(self, file_info):
        with open(file_info['path'], 'rb') as f:
            data_bytes = f.read()
        md5 = hashlib.md5()
        md5.update(data_bytes)

        data = Types.Data()
        data.size = len(data_bytes)
        data.bodyHash = md5.digest()
        data.body = data_bytes

        name = file_info['name']
        extension = name.split('.')[-1]
        mime_type = mimetypes.types_map.get('.{}'.format(extension), 'application/octet-stream')
        resource = Types.Resource()
        resource.mime = mime_type
        resource.data = data
        resource.attributes = Types.ResourceAttributes(fileName=name)
        return {
            'resource': resource,
            'mime_type': mime_type,
            'md5': md5.hexdigest(),
        }
Beispiel #24
0
 def upload_async():
     try:
         guid = self.view.settings().get("$evernote_guid")
         noteStore = self.get_note_store()
         note = noteStore.getNote(self.token(), guid, False, False, False, False)
         mime = mimetypes.guess_type(filename)[0] or "application/octet-stream"
         h = hashlib.md5(filecontents)
         attachment = Types.Resource(
             # noteGuid=guid,
             mime=mime,
             data=Types.Data(body=filecontents, size=len(filecontents), bodyHash=h.digest()),
             attributes=Types.ResourceAttributes(attachment=not insert_in_content, **attr))
         resources = note.resources or []
         resources.append(attachment)
         note.resources = resources
         noteStore.updateNote(self.token(), note)
         if insert_in_content:
             tag = '<en-media type="%s" hash="%s"/>' % (mime, h.hexdigest())
             view.run_command('insert', {'characters': tag})
             sublime.set_timeout(lambda: view.run_command("save_evernote_note"), 10)
     except Exception as e:
         sublime.error_message(
             "Evernote plugin cannot insert the attachment.\n" +
             explain_error(e))
Beispiel #25
0
    def do_run(self,
               edit,
               insert_in_content=True,
               filename=None,
               prompt=False):
        import hashlib, mimetypes
        view = self.view
        if filename is None or prompt:
            view.window().show_input_panel(
                "Filename or URL of attachment: ", filename or "",
                lambda x: view.run_command(
                    "evernote_insert_attachment", {
                        'insert_in_content': insert_in_content,
                        "filename": x,
                        "prompt": False
                    }), None, None)
            return
        filename = filename.strip()
        attr = {}
        try:
            if filename.startswith("http://") or \
               filename.startswith("https://"):
                # download
                import urllib.request
                response = urllib.request.urlopen(filename)
                filecontents = response.read()
                attr = {"sourceURL": filename}
            else:
                datafile = os.path.expanduser(filename)
                with open(datafile, 'rb') as content_file:
                    filecontents = content_file.read()
                attr = {"fileName": os.path.basename(datafile)}
        except Exception as e:
            sublime.error_message(
                "Evernote plugin has troubles locating the specified file/URL.\n"
                + explain_error(e))
            return

        try:
            guid = self.view.settings().get("$evernote_guid")
            noteStore = self.get_note_store()
            note = noteStore.getNote(self.token(), guid, False, False, False,
                                     False)
            mime = mimetypes.guess_type(
                filename)[0] or "application/octet-stream"
            h = hashlib.md5(filecontents)
            attachment = Types.Resource(
                # noteGuid=guid,
                mime=mime,
                data=Types.Data(body=filecontents,
                                size=len(filecontents),
                                bodyHash=h.digest()),
                attributes=Types.ResourceAttributes(
                    attachment=not insert_in_content, **attr))
            resources = note.resources or []
            resources.append(attachment)
            note.resources = resources
            self.message("Uploading attachment...")
            noteStore.updateNote(self.token(), note)
            if insert_in_content:
                view.insert(
                    edit,
                    view.sel()[0].a,
                    '<en-media type="%s" hash="%s"/>' % (mime, h.hexdigest()))
                sublime.set_timeout(
                    lambda: view.run_command("save_evernote_note"), 10)
        except Exception as e:
            sublime.error_message(
                "Evernote plugin cannot insert the attachment.\n" +
                explain_error(e))
Beispiel #26
0
def lambda_handler(event, context):

    #共通処理 事前定義
    def return200(res_body):
        return {'statusCode': 200, 'body': json.dumps(res_body)}

    def return400(message_str):
        body = {'errorMessage': message_str}
        return {'statusCode': 400, 'body': json.dumps(body)}

    global noteobject

    #共通処理 Evernote認証
    access_token = boto3.client('kms').decrypt(CiphertextBlob=b64decode(
        os.environ['en_access_token']))['Plaintext'].decode()
    client = EvernoteClient(token=access_token, sandbox=False)
    try:
        note_store = client.get_note_store()
    except Exception as e:
        return (return400(str(e)))

    #<ノート検索・複数取得><ノート個別取得>向け処理
    nhead_e1 = r'^<\?xml version="1.0" encoding="UTF-8"\?>(\n)?'
    nhead_e2 = r'^<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">(\n)?'
    nhead_e3 = r'^<en-note.*?>(\n)?'

    #API:<ノート検索・複数取得>
    if event['httpMethod'] == 'GET' and event['resource'] == '/notes':

        #<ノート検索・複数取得>向け処理 リクエストからパラメータ値を取得してセット
        req_query = event['queryStringParameters']
        if req_query is None:
            return (return400("パラメータが指定されていません"))
        req_searchword = req_query.get('q', '')
        req_notebookguid = req_query.get('notebookguid', '')
        try:
            req_searchtype = int(req_query.get('searchtype', 0))
            req_includecontent = int(req_query.get('includecontent', 0))
            req_maxnotescount = int(req_query.get('maxnotescount', 1))
        except:
            return (return400('不正な型のパラメータ'))

        filter = NoteFilter(notebookGuid=req_notebookguid)
        resultSpec = NotesMetadataResultSpec(includeTitle=True,
                                             includeNotebookGuid=True)
        metalist = note_store.findNotesMetadata(filter, 0, 250, resultSpec)
        notelist = []

        for meta in metalist.notes:
            noteobject = ''
            if req_searchtype == 0:  #タイトル部分一致(既定)
                if str(req_searchword) in meta.title:
                    if req_includecontent == 0:  #コンテンツを含まない
                        noteobject = {
                            'noteguid': meta.guid,
                            'title': meta.title,
                            'notebookguid': meta.notebookGuid
                        }
                    if req_includecontent == 1:  #コンテンツを含む
                        content = note_store.getNoteContent(
                            access_token, meta.guid)
                        content = re.sub(nhead_e1, '', content)
                        content = re.sub(nhead_e2, '', content)
                        content = re.sub(nhead_e3, '', content)
                        content = re.sub(r'(\n)?</en-note>$', '', content)
                        noteobject = {
                            'noteguid': meta.guid,
                            'title': meta.title,
                            'notebookguid': meta.notebookGuid,
                            'content': content,
                        }
            if req_searchtype == 1:  #タイトル完全一致
                if req_searchword == meta.title:
                    if req_includecontent == 0:  #コンテンツを含まない
                        noteobject = {
                            'noteguid': meta.guid,
                            'title': meta.title,
                            'notebookguid': meta.notebookGuid
                        }
                    if req_includecontent == 1:  #コンテンツを含む
                        content = note_store.getNoteContent(
                            access_token, meta.guid)
                        content = re.sub(nhead_e1, '', content)
                        content = re.sub(nhead_e2, '', content)
                        content = re.sub(nhead_e3, '', content)
                        content = re.sub(r'(\n)?</en-note>$', '', content)
                        noteobject = {
                            'noteguid': meta.guid,
                            'title': meta.title,
                            'notebookguid': meta.notebookGuid,
                            'content': content
                        }
            if req_searchtype == 2:  #タイトル・本文部分一致
                pass  #作成中
            if noteobject != '':
                notelist.append(noteobject)
            if len(notelist) == req_maxnotescount:
                break

        return (return200(notelist))

    #API:<ノート個別取得>
    if event['httpMethod'] == 'GET' and event['resource'] == '/note/{noteguid}':

        #リクエストからパラメータ値を取得してセット
        try:
            req_noteguid = json.loads(event['pathParameters']).get('noteguid')
        except:
            req_noteguid = event['pathParameters'].get('noteguid')
        if req_noteguid is None:
            return (return400("{noteguid}パス未指定"))

        n = note_store.getNote(access_token, req_noteguid, False, False, False,
                               False)
        content = note_store.getNoteContent(access_token, req_noteguid)
        content = re.sub(nhead_e1, '', content)
        content = re.sub(nhead_e2, '', content)
        content = re.sub(nhead_e3, '', content)
        content = re.sub(r'(\n)?</en-note>$', '', content)
        noteobject = {
            'noteguid': n.guid,
            'title': n.title,
            'content': content,
            'notebookguid': n.notebookGuid
        }

        return (return200(noteobject))

    #<ノート作成><ノート更新><ノート削除>向け処理
    n = Types.Note()
    nhead_e = '<?xml version="1.0" encoding="UTF-8"?>'
    nhead_e += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'

    #<ノート作成><ノート更新><ノート削除>向け処理 リクエストボディから値を取得してセット
    try:
        req_body_dict = json.loads(event['body'])
    except:
        return (return400('不正な形式のデータ'))
    req_notecontent = req_body_dict.get('notecontent', '')
    req_resource_ary = req_body_dict.get('resource_ary', [])
    req_notetitle = req_body_dict.get('notetitle')
    req_notebookguid = req_body_dict.get('notebookguid', '')
    if req_notetitle == None:
        return (return400('notetitleが未指定'))
    else:
        n.title = req_notetitle
    if req_notebookguid != '':
        n.notebookGuid = req_notebookguid
    else:
        pass

    #API:<ノート作成>
    if event['httpMethod'] == 'POST' and event['resource'] == '/note':
        n.content = nhead_e + '<en-note>' + req_notecontent + '</en-note>'
        if len(req_resource_ary) != 0:
            resources = []
            for index, item in enumerate(req_resource_ary):
                data = Types.Data()
                data.body = b64decode(item['databody'])
                data.size = len(data.body)
                data.bodyHash = item['bodyhash']
                resource = Types.Resource()
                resource.mime = item['mimetype']
                resource.data = data
                attr = Types.ResourceAttributes()
                attr.fileName = 'img' + str([index - 1]) + '.jpg'
                resource.attributes = attr
                resources.append(resource)
            if len(resources) != 0:
                n.resources = resources
        n = note_store.createNote(n)
        noteobject = {
            'noteguid': n.guid,
            'title': n.title,
            'notebookguid': n.notebookGuid
        }
        return (return200(noteobject))

    #<ノート更新><ノート削除>向け処理 リクエストからパラメータ値を取得してセット
    try:
        req_noteguid = json.loads(event['pathParameters']).get('noteguid')
    except:
        req_noteguid = event['pathParameters'].get('noteguid')
    if req_noteguid is None:
        return (return400("{noteguid}パスが未指定"))

    #API:<ノート更新>
    if event['httpMethod'] == 'PATCH' and event[
            'resource'] == '/note/{noteguid}':
        n.guid = req_noteguid
        if req_notecontent != '':
            n.content = nhead_e + '<en-note>' + req_notecontent + '</en-note>'
        if len(req_resource_ary) != 0:
            pass  #作成中
        n = note_store.updateNote(access_token, n)
        noteobject = {
            'noteguid': n.guid,
            'title': n.title,
            'notebookguid': n.notebookGuid
        }
        return (return200(noteobject))

    #API:<ノート削除>
    if event['httpMethod'] == 'DELETE':
        pass  #作成中

    return (return400('不正なメソッドおよびリソースの指定 %s, %s' %
                      (event['resource'], event['httpMethod'])))
    def createNote(self,notebook,notename):
        isexist = False;  # 是否存在当天的笔记  目前认为不可能存在当天的笔记, 如果有一定要删除
        noteStore = Client_Production.client.get_note_store();
        NOTE_SUFFIX = '</en-note>';
        NOTE_HEADER = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">';
        f = NoteStore.NoteFilter()

        msg_content='<en-note>';
        #根据路径,得到txt
        notetxtpath=self.RootPath+'/'+notebook.name+'/'+notebook.name+notename+"/"+notebook.name+notename+'.txt'
        # notetxtpath=notetxtpath.encode()
        print(notetxtpath)
        if os.path.exists(notetxtpath.decode('utf-8')):
            txtobj=open(notetxtpath.decode('utf-8'),'rb');
            flag = 'attachmentFlag:'
            if txtobj:
                resources = []
                for line in open(notetxtpath.decode('utf-8')):
                    line = line.strip('\n')
                    isattach = flag in line
                    # 读取的内容,如果包含attachmentFlag: 截取后面的内容
                    if isattach:
                        flagPos = line.index(flag)
                        filename = line[flagPos + len(flag):]
                        data = Types.Data()
                        filepath = self.RootPath + '/' + notebook.name + '/' +  notebook.name+notename + filename;
                        #
                        filename = os.path.basename(filepath.encode('utf-8'))
                        data = Types.Data()
                        # try:
                        # 必须保证文件名在生成的时候是gbk, 在读取的时候也是gbk , 没有在linux测试过
                        # print(os.path.exists(filepath.decode('utf-8')))
                        # D:\wxhistory\2017 - 12 - 29\2017 - 12 - 29【联盟家长微课:郭宛灵微课\resources\mp4
                        # 因为抬头已经用utf-8作为编码,open要求传入的path必须是unicode
                        if os.path.exists(filepath.decode('utf-8')):
                            data.body = open(filepath.decode('utf-8'), 'rb').read()
                            data.size = len(data.body)
                            data.bodyHash = hashlib.md5(data.body).hexdigest()
                            resource = Types.Resource()
                            resource.mime = mimetypes.guess_type(filename)[0]
                            resource.data = data
                            attr = Types.ResourceAttributes()
                            attr.fileName = filename
                            resource.attributes = attr
                            hexhash = resource.data.bodyHash
                            # gif肯定是表情符号,限制大小以免影响到阅读
                            minetype = resource.mime
                            msg_content += line[0:flagPos] + '<br />'
                            if ('gif' in minetype):
                                msg_content += "<div style='max-width:20px;max-height:20px'><en-media  type=\"%s\" " \
                                               "hash=\"%s\" /><br /></div>" % \
                                               (resource.mime, hexhash);
                            else:
                                msg_content += "<en-media  type=\"%s\" hash=\"%s\" /><br " \
                                               "/>" % \
                                               (resource.mime, hexhash);
                            # 昵称 默认红色显示
                            resources.append(resource)

                    else:
                        # p = re.compile(r'([\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f])')
                        result, number =re.subn("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "",line)
                        # msg_content += line.decode().encode() + '<br />'
                        msg_content += result + '<br />'
            note = Types.Note();
            note.notebookGuid = notebook.guid
            note.title = notename.encode()
            note.content = NOTE_HEADER;
            #过滤非法字符
            print("过滤前:");
            print(msg_content)
            msg_content=re.sub(u"[\x00-\x08\x0b-\x0c\x0e-\x1f]+",u"",msg_content);
            errhref='<a href=""></a>';
            msg_content=msg_content.replace(errhref,'#爬虫过滤错误信息#')
            note.content += msg_content.encode('utf-8')
            note.content += NOTE_SUFFIX
            note.resources = resources
            print("过滤后:")
            print(note.content)
            print('将要创建' +notebook.name+'//'+ notename)
            note = noteStore.createNote(Client_Production.token, note);
            print('创建完成')
        else:
            return
Beispiel #28
0
def cmd_add(args):
    """add [[:tag1] [:tag2] ...]
       [+<notebook>]
       <title>
       [resource1] [resource2] ..
       < <content>
    add a new note
       with the specified tags (or none if unspecified)
       in the specified notebook (or your current notebook if unspecified)
       with the specified title (required)
       adding the specified files as resources to that note (or none if unspecified)
       and content from stdin
    """

    # Parse args
    tags = []
    title = None
    resource_names = []
    for arg in args:
        if arg.startswith('+'):
            set_current_notebook_name(arg[1:])
        elif arg.startswith(':'):
            tags.append(arg[1:])
        elif title is None:
            title = arg
        else:
            resource_names.append(arg)

    if title is None:
        print("A title must be specified.")
        raise SyntaxError

    print("making note titled '%s' with tags'%s' and resources named '%s'" %
          (title, repr(tags), repr(resource_names)))

    nb_guid = get_current_notebook().guid

    resources = []
    attachments = ""
    for filename in resource_names:
        resource = Types.Resource()
        resource.data = Types.Data()
        resource.data.body = open(filename, 'r').read()
        resource.attributes = Types.ResourceAttributes(fileName=filename,
                                                       attachment=False)
        mime = mimetypes.guess_type(filename)[0]
        resource.mime = mime or ''
        hash = hashlib.md5()
        hash.update(resource.data.body)
        attachments += '<en-media type="%s" hash="%s" />\n' % (
            resource.mime, hash.hexdigest())
        resources.append(resource)

    content = wrap_content(sys.stdin.read() + attachments)

    note = Types.Note(title=title,
                      content=content,
                      tagNames=tags,
                      resources=resources,
                      notebookGuid=nb_guid)
    note = _retry(lambda: get_note_store().createNote(note))

    print("Note created!")