forked from CarlLee/ENML_PY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
92 lines (79 loc) · 2.76 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/python
# -*- coding: utf-8 -*-
import os
from bs4 import BeautifulSoup
MIME_TO_EXTESION_MAPPING = {
'image/png': '.png',
'image/jpg': '.jpg',
'image/jpeg': '.jpg',
'image/gif': '.gif'
}
def ENMLToHTML(content, pretty=True, **kwargs):
"""
converts ENML string into HTML string
"""
soup = BeautifulSoup(content)
todos = soup.find_all('en-todo')
for todo in todos:
checkbox = soup.new_tag('input')
checkbox['type'] = 'checkbox'
checkbox['disabled'] = 'true'
if todo.has_attr('checked'):
checkbox['checked'] = todo['checked']
todo.replace_with(checkbox)
if 'media_store' in kwargs:
store = kwargs['media_store']
all_media = soup.find_all('en-media')
for media in all_media:
resource_url = store.save(media['hash'], media['type'])
# TODO: use different tags for different mime-types
new_tag = soup.new_tag('img')
new_tag['src'] = resource_url
media.replace_with(new_tag)
note = soup.find('en-note')
if note:
body = soup.new_tag('body')
html = soup.new_tag('html')
html.append(note)
note.name = 'body'
output = html.prettify().encode('utf-8') if pretty else str(html)
return output
return content
class MediaStore(object):
def __init__(self, note_store, note_guid):
"""
note_store: NoteStore object from EvernoteSDK
note_guid: Guid of the note in which the resouces exist
"""
self.note_store = note_store
self.note_guid = note_guid
def _get_resource_by_hash(self, hash_str):
"""
get resource by its hash
"""
hash_bin = hash_str.decode('hex')
resource = self.note_store.getResourceByHash(self.note_guid, hash_bin, True, False, False);
return resource.data.body
def save(self, hash_str, mime_type):
pass
class FileMediaStore(MediaStore):
def __init__(self, note_store, note_guid, path):
"""
note_store: NoteStore object from EvernoteSDK
note_guid: Guid of the note in which the resouces exist
path: The path to store media file
"""
super(FileMediaStore, self).__init__(note_store, note_guid)
self.path = os.path.abspath(path)
def save(self, hash_str, mime_type):
"""
save the specified hash and return the saved file's URL
"""
if not os.path.exists(self.path):
os.makedirs(self.path)
data = self._get_resource_by_hash(hash_str)
file_path = self.path + '/' + hash_str + MIME_TO_EXTESION_MAPPING[mime_type]
f = open(file_path, "w")
f.write(data)
f.close()
return "file://" + file_path