forked from alexksikes/evernote-classify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgnsync.py
executable file
·274 lines (206 loc) · 7.63 KB
/
gnsync.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys
import argparse
import glob
import logging
import markdown
from geeknote import GeekNote
from storage import Storage
import editor
import tools
# set default logger (write log to file)
def_logpath = os.path.join(os.getenv('USERPROFILE') or os.getenv('HOME'), 'GeekNoteSync.log')
formatter = logging.Formatter('%(asctime)-15s : %(message)s')
handler = logging.FileHandler(def_logpath)
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
def log(func):
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception, e:
logger.error("%s", str(e))
return wrapper
@log
def reset_logpath(logpath):
"""
Reset logpath to path from command line
"""
global logger
if not logpath:
return
# remove temporary log file if it's empty
if os.path.isfile(def_logpath):
if os.path.getsize(def_logpath) == 0:
os.remove(def_logpath)
# save previous handlers
handlers = logger.handlers
# remove old handlers
for handler in handlers:
logger.removeHandler(handler)
# try to set new file handler
handler = logging.FileHandler(logpath)
handler.setFormatter(formatter)
logger.addHandler(handler)
class GNSync:
notebook_name = None
path = None
mask = None
notebook_guid = None
all_set = False
@log
def __init__(self, notebook_name, path, mask, format):
# check auth
if not Storage().getUserToken():
raise Exception("Auth error. There is not any oAuthToken.")
#set path
if not path:
raise Exception("Path to sync directories does not select.")
if not os.path.exists(path):
raise Exception("Path to sync directories does not exist.")
self.path = path
#set mask
if not mask:
mask = "*.*"
self.mask = mask
#set format
if not format:
format = "plain"
self.format = format
logger.info('Sync Start')
#set notebook
self.notebook_guid, self.notebook_name = self._get_notebook(notebook_name, path)
# all is Ok
self.all_set = True
@log
def sync(self):
"""
Synchronize files to notes
"""
if not self.all_set:
return
files = self._get_files()
notes = self._get_notes()
for f in files:
has_note = False
for n in notes:
if f['name'] == n.title:
has_note = True
if f['mtime'] > n.updated:
self._update_note(f, n)
break
if not has_note :
self._create_note(f)
logger.info('Sync Complite')
@log
def _update_note(self, file_note, note):
"""
Updates note from file
"""
content = self._get_file_content(file_note['path'])
result = GeekNote().updateNote(
guid=note.guid,
title=note.title,
content=content,
notebook=self.notebook_guid)
if result:
logger.info('Note "{0}" was updated'.format(note.title))
else:
raise Exception('Note "{0}" was not updated'.format(note.title))
return result
@log
def _create_note(self, file_note):
"""
Creates note from file
"""
content = self._get_file_content(file_note['path'])
if content is None:
return
result = GeekNote().createNote(
title=file_note['name'],
content=content,
notebook=self.notebook_guid)
if result:
logger.info('Note "{0}" was created'.format(file_note['name']))
else:
raise Exception('Note "{0}" was not created'.format(file_note['name']))
return result
@log
def _get_file_content(self, path):
"""
Get file content.
"""
content = open(path, "r").read()
content = editor.textToENML(content=content, raise_ex=True)
if content is None:
logger.warning("File {0}. Content must be an UTF-8 encode.".format(path))
return None
return content
@log
def _get_notebook(self, notebook_name, path):
"""
Get notebook guid and name. Takes default notebook if notebook's name does not
select.
"""
notebooks = GeekNote().findNotebooks()
if not notebook_name:
notebook_name = os.path.basename(os.path.realpath(path))
notebook = [item for item in notebooks if item.name == notebook_name]
guid = None
if notebook:
guid = notebook[0].guid
if not guid:
notebook = GeekNote().createNotebook(notebook_name)
if(notebook):
logger.info('Notebook "{0}" was created'.format(notebook_name))
else:
raise Exception('Notebook "{0}" was not created'.format(notebook_name))
guid = notebook.guid
return (guid, notebook_name)
@log
def _get_files(self):
"""
Get files by self.mask from self.path dir.
"""
file_paths = glob.glob(os.path.join(self.path, self.mask))
files = []
for f in file_paths:
if os.path.isfile(f):
file_name = os.path.basename(f)
file_name = os.path.splitext(file_name)[0]
mtime = int(os.path.getmtime(f) * 1000)
files.append({'path': f,'name': file_name, 'mtime': mtime})
return files
@log
def _get_notes(self):
"""
Get notes from evernote.
"""
keywords = 'notebook:"{0}"'.format(tools.strip(self.notebook_name))
return GeekNote().findNotes(keywords, 10000).notes
def main():
try:
parser = argparse.ArgumentParser()
parser.add_argument('--path', '-p', action='store', help='Path to synchronize directory')
parser.add_argument('--mask', '-m', action='store', help='Mask of files to synchronize. Default is "*.*"')
parser.add_argument('--format', '-f', action='store', default='plain', choices=['plain', 'markdown'], help='The format of the file contents. Default is "plain". Valid values are "plain" and "markdown"')
parser.add_argument('--notebook', '-n', action='store', help='Notebook name for synchronize. Default is default notebook')
parser.add_argument('--logpath', '-l', action='store', help='Path to log file. Default is GeekNoteSync in home dir')
args = parser.parse_args()
path = args.path if args.path else None
mask = args.mask if args.mask else None
format = args.format if args.format else None
notebook = args.notebook if args.notebook else None
logpath = args.logpath if args.logpath else None
reset_logpath(logpath)
GNS = GNSync(notebook, path, mask, format)
GNS.sync()
except (KeyboardInterrupt, SystemExit, tools.ExitException):
pass
except Exception, e:
logger.error(str(e));
if __name__ == "__main__":
main()