This repository was archived by the owner on Aug 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathsublime_evernote.py
129 lines (110 loc) · 4.93 KB
/
sublime_evernote.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
#coding:utf-8
import sys
sys.path.insert(0,"lib")
import evernote.edam.userstore.UserStore as UserStore
import evernote.edam.notestore.NoteStore as NoteStore
import evernote.edam.type.ttypes as Types
import evernote.edam.error.ttypes as Errors
from evernote.api.client import EvernoteClient
import sublime,sublime_plugin
import markdown2
import webbrowser
consumer_key = 'oparrish-4096'
consumer_secret ='c112c6417738f06a'
evernoteHost = "www.evernote.com"
callbackUrl = "http://127.0.0.1/sublimeevernote/callback"
settings = sublime.load_settings("SublimeEvernote.sublime-settings")
def get_evernote_client(token=None):
if token:
return EvernoteClient(token=token,service_host=evernoteHost, sandbox=False)
else:
return EvernoteClient(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
service_host=evernoteHost,
sandbox=False
)
class SendToEvernoteCommand(sublime_plugin.TextCommand):
def __init__(self,view):
self.view = view
self.window = sublime.active_window()
def to_markdown_html(self):
encoding = self.view.encoding()
if encoding == 'Undefined':
encoding = 'utf-8'
elif encoding == 'Western (Windows 1252)':
encoding = 'windows-1252'
sels = self.view.sel()
contents = ''
if sels:
for sel in sels: contents += self.view.substr(sel) + '\n\n'
if not contents.strip():
region = sublime.Region(0L, self.view.size())
contents = self.view.substr(region)
markdown_html = markdown2.markdown(contents, extras=['footnotes', 'fenced-code-blocks', 'cuddled-lists', 'code-friendly', 'metadata'])
return markdown_html
def connect(self,callback,**kwargs):
sublime.status_message("authenticate..., please wait...")
client = get_evernote_client()
request_token = client.get_request_token(callbackUrl)
def on_verifier(verifier):
access_token = client.get_access_token(request_token['oauth_token'],request_token['oauth_token_secret'],verifier)
settings.set('access_token',access_token)
sublime.save_settings('SublimeEvernote.sublime-settings')
sublime.status_message("authenticate ok")
callback(**kwargs)
webbrowser.open(client.get_authorize_url(request_token))
self.window.show_input_panel("type Verifier (required):",'',on_verifier,None,None)
def send_note(self,**kwargs):
access_token = settings.get('access_token')
client,noteStore = None,None
if access_token :
client = get_evernote_client(token=access_token)
else:
return self.connect(self.send_note,**kwargs)
try:
noteStore = client.get_note_store()
except Exception as e:
if sublime.ok_cancel_dialog('error %s! retry?'%e):
self.connect(self.send_note,**kwargs)
markdown_html = self.to_markdown_html()
def sendnote(title,tags):
note = Types.Note()
note.title = title.encode('utf-8')
note.content = '<?xml version="1.0" encoding="UTF-8"?>'
note.content += '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>%s'%markdown_html.encode('utf-8')
note.content += '</en-note>'
note.tagNames = tags and tags.split(",") or []
try:
sublime.status_message("please wait...")
cnote = noteStore.createNote(access_token, note)
sublime.status_message("send success guid:%s"%cnote.guid)
sublime.message_dialog("success")
except Errors.EDAMUserException as e:
args = dict(title=title,tags=tags)
if e.errorCode == 9:
self.connect(self.send_note,**args)
else:
if sublime.ok_cancel_dialog('error %s! retry?'%e):
self.connect(self.send_note,**args)
except Exception as e:
sublime.error_message('error %s'%e)
def on_title(title):
def on_tags(tags):
sendnote(title,tags)
if not 'tags' in markdown_html.metadata:
self.window.show_input_panel("Tags (Optional)::","",on_tags,None,None)
else:
sendnote(title, markdown_html.metadata['tags'])
if not(kwargs.get("title") or 'title' in markdown_html.metadata):
self.window.show_input_panel("Title (required)::","",on_title,None,None)
elif not kwargs.get("tags"):
on_title(markdown_html.metadata['title'])
else:
sendnote(kwargs.get("title"),kwargs.get("tags"))
def run(self, edit):
if not settings.get("access_token"):
self.connect(self.send_note)
else:
self.send_note()