Send Code to evernote by my specify notebook
阅读原文时间:2023年07月16日阅读:1

#coding:utf-8
import sys
sys.path.append("lib")
import thrift.protocol.TBinaryProtocol as TBinaryProtocol
import thrift.transport.THttpClient as THttpClient
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 html import XHTML
import sublime,sublime_plugin

consumer_key = 'jamiesun-2467'
consumer_secret ='7794453e92251986'
evernoteHost = "www.evernote.com"
userStoreUri = "https://" + evernoteHost + "/edam/user"

settings = sublime.load_settings("SublimeEvernote.sublime-settings")

class SendToEvernoteCommand(sublime_plugin.TextCommand):
def __init__(self,view):
self.view = view
self.window = sublime.active_window()

def connect(self,callback,\*\*kwargs):  
    sublime.status\_message("authenticate..., please wait...")  
    def \_connect(username,password):  
        try:  
            userStoreHttpClient = THttpClient.THttpClient(userStoreUri)  
            userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)  
            userStore = UserStore.Client(userStoreProtocol)  
            authresult = userStore.authenticate(username,password,consumer\_key,consumer\_secret)  
            if authresult:  
               token = authresult.authenticationToken  
               noteStoreUrl = authresult.noteStoreUrl  
               if not settings.get("password") and sublime.ok\_cancel\_dialog("Remember password?"):  
                   settings.set("password",password)  
               settings.set("username",username)  
               settings.set("authToken",token)  
               settings.set("noteStoreUrl",noteStoreUrl)  
               sublime.save\_settings('SublimeEvernote.sublime-settings')  
               sublime.status\_message("authenticate ok")  
               callback(\*\*kwargs)  
            else:  
                raise Exception("authenticate failure")  
        except Exception,e:  
            sublime.error\_message("error:%s"%e)  

    def on\_username(username):  
        def on\_passwd(password):  
            if  username and  password:  
                \_connect(username,password)  
        self.window.show\_input\_panel("password (required)::","",on\_passwd,None,None) 

    iusername = settings.get("username")  
    ipassword = settings.get("password")  
    if not iusername or not ipassword:  
        self.window.show\_input\_panel("username (required)::","",on\_username,None,None)  
    else:  
        \_connect(iusername,ipassword)        

def send\_note(self,\*\*kwargs):  
    authToken = settings.get("authToken")  
    noteStoreUrl = settings.get('noteStoreUrl')  
    noteStoreHttpClient = THttpClient.THttpClient(noteStoreUrl)  
    noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)  
    noteStore = NoteStore.Client(noteStoreProtocol)  
    region = sublime.Region(0L, self.view.size())  
    content = self.view.substr(region)  

    def sendnote(title,tags):  
        xh =  XHTML()  
        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><pre>%s'%xh.p(content.encode('utf-8'))  
        note.content += '</pre></en-note>'  
        note.tagNames = tags and tags.split(",") or \[\]

        #set note's parent notebook guid  
        note.notebookGuid  = "eeb43022-0fd4-4c29-894b-03e3d5fa2f45"

        try:  
            sublime.status\_message("please wait...")  
            cnote = noteStore.createNote(authToken, note)  
            sublime.status\_message("send success guid:%s"%cnote.guid)  
            sublime.message\_dialog("success")  
        except Errors.EDAMUserException,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,e:  
            sublime.error\_message('error %s'%e)

    def on\_title(title):  
        def on\_tags(tags):  
            sendnote(title,tags)  
        self.window.show\_input\_panel("Tags (Optional)::","",on\_tags,None,None) 

    if not  kwargs.get("title"):  
        self.window.show\_input\_panel("Title (required)::","",on\_title,None,None)  
    else:  
        sendnote(kwargs.get("title"),kwargs.get("tags")) 

def run(self, edit):  
    if not settings.get("authToken"):  
        self.connect(self.send\_note)  
    else:  
        self.send\_note()

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章