Esempio n. 1
0
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
Esempio n. 2
0
 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
Esempio n. 3
0
 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
Esempio n. 4
0
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
Esempio n. 5
0
 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,
             )))
Esempio n. 6
0
    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
Esempio n. 8
0
    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
Esempio n. 9
0
        "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"],
)

expected = """  <note>
    <title>Test Title</title>
    <created>20210209T203437Z</created>
Esempio n. 10
0
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