Ejemplo n.º 1
0
def getUserStoreInstance(authToken):
	print "Getting UserStore instance..."
	userStoreUri = "%s/edam/user" % EN_URL
	userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
	userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
	userStore = UserStore.Client(userStoreProtocol)
	return userStore
Ejemplo n.º 2
0
 def __init__(self, config):
     self.config = config
     self.authToken = config["access_token"]
     '''
     Initial development is performed on our sandbox server. 
     To use the production service, 
     change "sandbox.evernote.com" to "www.evernote.com" and replace your
     developer token above with a token from 
     https://www.evernote.com/api/DeveloperToken.action
     '''
     self.evernoteHost = "sandbox.evernote.com"
     userStoreUri = "https://" + self.evernoteHost + "/edam/user"
     userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
     userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
         userStoreHttpClient)
     self.userStore = UserStore.Client(userStoreProtocol)
     '''
     Get the URL used to interact with the contents of the user's account
     When your application authenticates using OAuth, the NoteStore URL will
     be returned along with the auth token in the final OAuth request.
     In that case, you don't need to make this call.
     '''
     noteStoreUrl = self.userStore.getNoteStoreUrl(self.authToken)
     #print noteStoreUrl
     noteStoreHttpClient = THttpClient.THttpClient(noteStoreUrl)
     noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(
         noteStoreHttpClient)
     self.noteStore = NoteStore.Client(noteStoreProtocol)
Ejemplo n.º 3
0
 def get_user_store(self):
     user_store_uri = self._get_endpoint("/edam/user")
     store = Store(self.token, UserStore.Client, user_store_uri)
     if not store:  # Trick for PyDev code completion
         store = UserStore.Client()
         raise Exception('Should never reach here')
     return store
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
0
    def __init__(self, username, password, consumerKey, consumerSecret,
                 userStoreUri, noteStoreUriBase):
        self.username = username
        self.password = password

        # setup the basic connection stuff
        userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        userStore = UserStore.Client(userStoreProtocol)

        versionOK = userStore.checkVersion(
            "Python EDAMTest", UserStoreConstants.EDAM_VERSION_MAJOR,
            UserStoreConstants.EDAM_VERSION_MINOR)
        if not versionOK:
            raise ErrorConstants.EDAMSystemException(
                ErrorConstants.EDAMErrorCode.UNKNOWN,
                "EDAM protocol version not up to date ")

        authResult = userStore.authenticate(username, password, consumerKey,
                                            consumerSecret)
        user = authResult.user
        self.authToken = authResult.authenticationToken

        noteStoreUri = noteStoreUriBase + user.shardId
        noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
        noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            noteStoreHttpClient)

        self.noteStore = NoteStore.Client(noteStoreProtocol)

        self.getNotebooks(None)
Ejemplo n.º 6
0
    def getUserStore(self):
        userStoreHttpClient = THttpClient.THttpClient(
            self.EN_USERSTORE_URIBASE)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        userStore = UserStore.Client(userStoreProtocol)

        return userStore
Ejemplo n.º 7
0
def get_note_store():
    userStoreHttpClient = THttpClient.THttpClient(config.user_store_uri)
    userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
    userStore = UserStore.Client(userStoreProtocol)
    noteStoreUrl = userStore.getNoteStoreUrl(config.auth_token)
    noteStoreHttpClient = THttpClient.THttpClient(noteStoreUrl)
    noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
    return NoteStore.Client(noteStoreProtocol)
Ejemplo n.º 8
0
 def recv_getNoteStoreUrl(self, ):
     (fname, mtype, rseqid) = self._iprot.readMessageBegin()
     if mtype == TMessageType.EXCEPTION:
         x = UserStore.TApplicationException()
         x.read(self._iprot)
         self._iprot.readMessageEnd()
         raise x
     result = getNoteStoreUrl_result()
     result.read(self._iprot)
     self._iprot.readMessageEnd()
     if result.success is not None:
         return result.success
     if result.userException is not None:
         raise result.userException
     if result.systemException is not None:
         raise result.systemException
     raise UserStore.TApplicationException(
         UserStore.TApplicationException.MISSING_RESULT,
         "getNoteStoreUrl failed: unknown result")
Ejemplo n.º 9
0
def get_note_store():
    user_store_uri         = "https://" + evernote_host + "/edam/user"
    user_store_http_client = THttpClient.THttpClient(user_store_uri)
    user_store_protoclo    = TBinaryProtocol.TBinaryProtocol(user_store_http_client)
    user_store             = UserStore.Client(user_store_protoclo)

    note_store_url         = user_store.getNoteStoreUrl(auth_token)
    note_store_http_client = THttpClient.THttpClient(note_store_url)
    note_store_protocol    = TBinaryProtocol.TBinaryProtocol(note_store_http_client)
    return NoteStore.Client(note_store_protocol)
Ejemplo n.º 10
0
def get_user_store(auth_token=None):
    if not auth_token:
        auth_token = get_auth_token()
    user_store_uri = "https://" + HOST + "/edam/user"

    user_store_http_client = THttpClient.THttpClient(
        user_store_uri,
        http_proxy=get_proxy_config(urlparse(user_store_uri).scheme))
    user_store_protocol = TBinaryProtocol.TBinaryProtocol(
        user_store_http_client)
    return UserStore.Client(user_store_protocol)
Ejemplo n.º 11
0
    def wraps(*args, **kwargs):
        user_store_http_client = THttpClient(settings.EVERNOTE_STORE_URI)
        user_store_protocol = TBinaryProtocol(user_store_http_client)
        user_store = UserStore.Client(user_store_protocol)

        note_store_url = user_store.getNoteStoreUrl(
            settings.EVERNOTE_DEVELOPER_TOKEN)
        note_store_http_client = THttpClient(note_store_url)
        note_store_protocol = TBinaryProtocol(note_store_http_client)
        note_store = NoteStore.Client(note_store_protocol)

        kwargs['user_store'] = user_store
        kwargs['note_store'] = note_store
        return func(*args, **kwargs)
Ejemplo n.º 12
0
    def create_user_store(self):
        uri = "https://" + Evernote.HOST + "/edam/user"

        http_client = THttpClient.THttpClient(uri)
        protocol = TBinaryProtocol.TBinaryProtocol(http_client)

        user_store = UserStore.Client(protocol)

        version_ok = user_store.checkVersion(
            "Python EDAMTest", UserStoreConstants.EDAM_VERSION_MAJOR,
            UserStoreConstants.EDAM_VERSION_MINOR)
        if not version_ok:
            raise StandardError("EDAM version error")

        return user_store
Ejemplo n.º 13
0
 def getAuth(self, user, password):
     userStoreHttpClient = THttpClient.THttpClient(self.userStoreUri)
     userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
     userStore = UserStore.Client(userStoreProtocol)
 
     versionOK = userStore.checkVersion("Python EDAMTest",
                                    UserStoreConstants.EDAM_VERSION_MAJOR,
                                    UserStoreConstants.EDAM_VERSION_MINOR)
     if not versionOK:
         print "Old EDAM version"
         exit(1)
     authResult = userStore.authenticate(user, password,
                         self.consumerKey, self.consumerSecret)
     user = authResult.user
     self.authToken = authResult.authenticationToken
     self.authResult = authResult
     return self.authToken
Ejemplo n.º 14
0
def login_evernote_token(request):
    """ as get_evernote_token(), but logs the user in as well
    """
    everAuth = EvernoteAPI()
    credentials = everAuth.get_user_token(request)
    if not credentials:
        return HttpResponseRedirect(
            reverse('account.views.login_page', args=[]))
    if request.user.is_authenticated():
        user = request.user
    else:
        evernoteHost = settings.EVERNOTE_HOST
        userStoreUri = "https://" + evernoteHost + "/edam/user"
        userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        userStore = UserStore.Client(userStoreProtocol)
        evernoteUser = userStore.getUser(credentials['oauth_token'])
        user = authenticate(username=evernoteUser.username,
                            password=str(evernoteUser.id))
        if not user:
            newUser = User.objects.create_user(evernoteUser.username,
                                               evernoteUser.email,
                                               str(evernoteUser.id))
            names = evernoteUser.name.split() if evernoteUser.name else None
            newUser.first_name = names[0] if names and len(names) > 0 else ""
            newUser.last_name = names[1] if names and len(names) > 1 else ""
            newUser.save()
            user = authenticate(username=evernoteUser.username,
                                password=str(evernoteUser.id))
        login(request, user)

    try:
        expires_time = datetime.fromtimestamp(int(credentials['expires']))
    except TypeError:
        logging.error("Error parsing token expires time")
        expires_time = datetime.now()

    profile = UserProfile()
    profile.user = user
    profile.evernote_token = credentials['oauth_token']
    profile.evernote_token_expires_time = expires_time
    profile.evernote_note_store_url = credentials['edam_noteStoreUrl']
    profile.save()
    return HttpResponseRedirect(reverse('base.views.index', args=[]))
Ejemplo n.º 15
0
    def post(self):
        # check if we have session info
        session = get_current_session()
        if not (session.is_active()) or not (session["done"]):
            raise Exception("Session is not active")

        post = self.request.get("post")
        loc_lat = self.request.get("loc_lat")
        loc_long = self.request.get("loc_long")
        local_time_stamp = self.request.get("time")

        # set up the thrift userStore
        userStoreHttpClient = THttpClient.THttpClient(USERSTORE_URI)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        userStore = UserStore.Client(userStoreProtocol)
        # check EDAM version
        versionOK = userStore.checkVersion(
            "Python EDAMTest", UserStoreConstants.EDAM_VERSION_MAJOR,
            UserStoreConstants.EDAM_VERSION_MINOR)
        if not (versionOK):
            raise Exception("EDAM versions do not match")

        # unwrap the session variables
        oauth_token = session['oauth_token']
        shard = session['shard']
        user_id = session['user']
        user = EvernoteUser.get_by_key_name(user_id)
        notebook_id = user.notebook_id

        # set up the thrift protocol for noteStore
        noteStoreUri = NOTESTORE_URI_BASE + shard
        noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
        noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            noteStoreHttpClient)
        noteStore = NoteStore.Client(noteStoreProtocol)

        log = logging.getLogger(__name__)

        # get or make the notebook
        pensievr_nb = None
        if not (notebook_id):
            # first look for the notebook
            try:
                notebooks = noteStore.listNotebooks(oauth_token)
            except Errors.EDAMUserException, e:
                # save the note
                note = EvernoteNote(user=user,
                                    note=post,
                                    loc_lat=loc_lat,
                                    loc_long=loc_long)
                note.put()
                # clear out the bad session
                session.terminate()
                self.redirect("/")
            for notebook in notebooks:
                if notebook.name == NOTEBOOK_NAME:
                    pensievr_nb = notebook
            # if we don't have a pensievr notebook, make one
            tmp_nb = Types.Notebook()
            tmp_nb.name = NOTEBOOK_NAME
            tmp_nb.defaultNotebook = False
            tmp_nb.published = False
            if not (pensievr_nb):
                try:
                    pensievr_nb = noteStore.createNotebook(oauth_token, tmp_nb)
                except Errors.EDAMUserException, e:
                    if e.errorCode != 9:  # INVALID_AUTH
                        raise
                    # save the note
                    note_obj = EvernoteNote(user=user,
                                            note=post,
                                            loc_lat=loc_lat,
                                            loc_long=loc_long)
                    note_obj.put()
                    # clear out the bad session
                    session.terminate()
                    self.redirect("/")
Ejemplo n.º 16
0
def get_userstore():
    """Return an instance of the Evernote UserStore."""
    userStoreHttpClient = THttpClient.THttpClient(EN_USERSTORE_URIBASE)
    userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
    userStore = UserStore.Client(userStoreProtocol)
    return userStore
Ejemplo n.º 17
0
password = sys.argv[2]

#
# NOTE: You must change the consumer key and consumer secret to the
#       key and secret that you received from Evernote
#
consumerKey = "westine"
consumerSecret = "277e8b3a8cc49b2d"

evernoteHost = "www.evernote.com"  #this is production service
userStoreUri = "https://" + evernoteHost + "/edam/user"
noteStoreUriBase = "https://" + evernoteHost + "/edam/note/"

userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
userStore = UserStore.Client(userStoreProtocol)

versionOK = userStore.checkVersion("Python EDAMTest",
                                   UserStoreConstants.EDAM_VERSION_MAJOR,
                                   UserStoreConstants.EDAM_VERSION_MINOR)

print "Is my EDAM protocol version up to date? ", str(versionOK)
print ""
if not versionOK:
    exit(1)

# Authenticate the user
try:
    authResult = userStore.authenticate(username, password, consumerKey,
                                        consumerSecret)
except Errors.EDAMUserException, e:
Ejemplo n.º 18
0
 def __setUserStore(self):  # {{{
     """ setup userStore. """
     userStoreHttpClient = THttpClient.THttpClient(USERSTORE_URI)
     userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
     self.userStore = UserStore.Client(userStoreProtocol)
Ejemplo n.º 19
0
    def __init__(self):
        username = settings.EVERNOTE_USER
        password = settings.EVERNOTE_PW
        consumerKey = settings.EVERNOTE_CONSUMER_KEY
        consumerSecret = settings.EVERNOTE_CONSUMER_SECRET
        evernoteHost = settings.EVERNOTE_HOST
        userStoreUri = "https://" + evernoteHost + "/edam/user"
        noteStoreUriBase = "https://" + evernoteHost + "/edam/note/"

        userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        userStore = UserStore.Client(userStoreProtocol)

        versionOK = userStore.checkVersion(
            "Python EDAMTest", UserStoreConstants.EDAM_VERSION_MAJOR,
            UserStoreConstants.EDAM_VERSION_MINOR)

        if not versionOK:
            raise Exception("Invalid EDAM Version")

        # Authenticate the user
        try:
            authResult = userStore.authenticate(username, password,
                                                consumerKey, consumerSecret)

        except Errors.EDAMUserException as e:
            # See http://www.evernote.com/about/developer/api/ref/UserStore.html#Fn_UserStore_authenticate
            parameter = e.parameter
            errorCode = e.errorCode
            errorText = Errors.EDAMErrorCode._VALUES_TO_NAMES[errorCode]

            print "Authentication failed (parameter: " + parameter + " errorCode: " + errorText + ")"

            if errorCode == Errors.EDAMErrorCode.INVALID_AUTH:
                if parameter == "consumerKey":
                    if consumerKey == "en-edamtest":
                        print "You must replace the variables consumerKey and consumerSecret with the values you received from Evernote."
                    else:
                        print "Your consumer key was not accepted by", evernoteHost
                        print "This sample client application requires a client API key. If you requested a web service API key, you must authenticate using OAuth."
                    print "If you do not have an API Key from Evernote, you can request one from http://www.evernote.com/about/developer/api"
                elif parameter == "username":
                    print "You must authenticate using a username and password from", evernoteHost
                    if evernoteHost != "www.evernote.com":
                        print "Note that your production Evernote account will not work on", evernoteHost
                        print "You must register for a separate test account at https://" + evernoteHost + "/Registration.action"
                elif parameter == "password":
                    print "The password that you entered is incorrect"

            print ""
            exit(1)

        userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
        userStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            userStoreHttpClient)
        self.userStore = UserStore.Client(userStoreProtocol)

        versionOK = self.userStore.checkVersion(
            "Python EDAMTest", UserStoreConstants.EDAM_VERSION_MAJOR,
            UserStoreConstants.EDAM_VERSION_MINOR)
        if not versionOK:
            print "Old EDAM version"
            exit(1)
        authResult = self.userStore.authenticate(username, password,
                                                 consumerKey, consumerSecret)
        self.authToken = authResult.authenticationToken

        noteStoreUri = noteStoreUriBase + authResult.user.shardId
        noteStoreHttpClient = THttpClient.THttpClient(noteStoreUri)
        noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(
            noteStoreHttpClient)
        self.noteStore = NoteStore.Client(noteStoreProtocol)
Ejemplo n.º 20
0
def show_entry(page):
    authToken = ''
    if authToken == "wrong":
        print "Please fill in your developer token"
        print "To get a developer token, visit https://sandbox.evernote.com/api/DeveloperToken.action"
        exit(1)

    # https://www.evernote.com/api/DeveloperToken.action
    evernoteHost = "www.evernote.com"
    userStoreUri = "https://" + evernoteHost + "/edam/user"

    userStoreHttpClient = THttpClient.THttpClient(userStoreUri)
    userStoreProtocol = TBinaryProtocol.TBinaryProtocol(userStoreHttpClient)
    userStore = UserStore.Client(userStoreProtocol)

    versionOK = userStore.checkVersion("Evernote EDAMTest (Python)",
                                       UserStoreConstants.EDAM_VERSION_MAJOR,
                                       UserStoreConstants.EDAM_VERSION_MINOR)
    if not versionOK:
        exit(1)

    noteStoreUrl = userStore.getNoteStoreUrl(authToken)
    noteStoreHttpClient = THttpClient.THttpClient(noteStoreUrl)
    noteStoreProtocol = TBinaryProtocol.TBinaryProtocol(noteStoreHttpClient)
    noteStore = NoteStore.Client(noteStoreProtocol)

    #get access to default notebook
    notebooks = noteStore.listNotebooks(authToken)
    for notebook in notebooks:
        print notebook.name
        if notebook.name == "Blog":
            filter = NoteStore.NoteFilter()
            filter.notebookGuid = notebook.guid
            filter.order = Types.NoteSortOrder.UPDATED
            filter.ascending = False

            notes = noteStore.findNotes(authToken, filter, 0, 10000)
            for note in notes.notes:
                note = noteStore.getNote(authToken,
                                         note.guid,
                                         withContent=True,
                                         withResourcesData=True,
                                         withResourcesRecognition=False,
                                         withResourcesAlternateData=False)
                g.db.execute(
                    'insert or replace into entries (created, title, text) values (?, ?, ?)',
                    [note.created, note.title,
                     note.content.decode('utf-8')])
                g.db.commit()
    #Show all entries
    cur = g.db.execute(
        'select created, title, text from entries order by created desc')
    entries = []
    for row in cur.fetchall():
        readable = time.ctime(row[0] / 1000)
        content = row[2]
        entry = dict(created=readable, title=row[1], text=row[2])
        entries.append(entry)
    count = len(entries)
    final_values = page - 1
    smaller = page - 1
    larger = page + 1
    previous = True
    after = False
    if final_values == 0:
        previous = False
    if final_values + PER_PAGE < count:
        final_entries = entries[final_values:PER_PAGE]
    else:
        final_entries = entries[final_values:count]
        after = False
    if not entries and page != 1:
        abort(404)
    return render_template('show_entries.html', entries = final_entries, previous = previous, after = after, \
            smaller = smaller, larger = larger)
Ejemplo n.º 21
0
 def get_user_store(self):
     if self.user_store is None:
         client = THttpClient.THttpClient(USER_STORE_URI)
         protocol = TBinaryProtocol.TBinaryProtocol(client)
         self.user_store = UserStore.Client(protocol)
     return self.user_store