def login(self): response = self.loadPage(self.url['base'], self.url['login'], "GET", {'oauth_token': self.tmpOAuthToken}) if response.status != 200: logging.error("Unexpected response status on login 200 != %s", response.status) tools.exit() if not self.cookies.has_key('JSESSIONID'): logging.error("Not found value JSESSIONID in the response cookies") tools.exit() # get login/password self.username, self.password = out.GetUserCredentials() self.postData['login']['username'] = self.username self.postData['login']['password'] = self.password self.postData['login']['targetUrl'] = self.url['oauth']%self.tmpOAuthToken response = self.loadPage(self.url['base'], self.url['login']+";jsessionid="+self.cookies['JSESSIONID'], "POST", self.postData['login']) if not response.location and response.status == 200: if self.incorrectLogin < 3: out.preloader.stop() out.printLine('Sorry, incorrect login or password') out.preloader.setMessage('Authorize...') self.incorrectLogin += 1 return self.login() else: logging.error("Incorrect login or password") if not response.location: logging.error("Target URL was not found in the response on login") tools.exit() logging.debug("Success authorize, redirect to access page")
def settings(self, editor=None): storage = self.getStorage() if editor: if editor == "#GET#": editor = storage.getUserprop("editor") if not editor: editor = config.DEF_WIN_EDITOR if sys.platform == "win32" else config.DEF_UNIX_EDITOR out.successMessage("Current editor is: %s" % editor) else: storage.setUserprop("editor", editor) out.successMessage("Changes have been saved.") else: settings = ( "Geeknote", "*" * 30, "Version: %s" % config.VERSION, "App dir: %s" % config.APP_DIR, "Error log: %s" % config.ERROR_LOG, "Current editor: %s" % storage.getUserprop("editor"), ) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]["info"] settings += ("*" * 30, "Username: %s" % user.username, "Id: %s" % user.id, "Email: %s" % user.email) out.printLine("\n".join(settings))
def settings(self, editor=None): storage = self.getStorage() if editor: if editor == '#GET#': editor = storage.getUserprop('editor') if not editor: editor = config.DEF_WIN_EDITOR if sys.platform == 'win32' else config.DEF_UNIX_EDITOR out.successMessage("Current editor is: %s" % editor) else: storage.setUserprop('editor', editor) out.successMessage("Changes have been saved.") else: settings = ('Geeknote', '*' * 30, 'Version: %s' % config.VERSION, 'App dir: %s' % config.APP_DIR, 'Error log: %s' % config.ERROR_LOG, 'Current editor: %s' % storage.getUserprop('editor')) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]['info'] settings += ('*' * 30, 'Username: %s' % user.username, 'Id: %s' % user.id, 'Email: %s' % user.email) out.printLine('\n'.join(settings))
def printErrorArgument(self, errorArg, errorVal=None): if errorVal is None: out.printLine('Unexpected argument "%s" ' 'for command "%s"' % (errorArg, self.CMD)) else: out.printLine('Unexpected value "%s" ' 'for argument "%s"' % (errorVal, errorArg)) self.printHelp()
def login(self): response = self.loadPage( self.url["base"], self.url["login"], "GET", {"oauth_token": self.tmpOAuthToken}, ) # parse hpts and hptsh from page content hpts = re.search('.*\("hpts"\)\.value.*?"(.*?)"', response.data) hptsh = re.search('.*\("hptsh"\)\.value.*?"(.*?)"', response.data) if response.status != 200: logging.error( "Unexpected response status " "on login 200 != %s", response.status ) tools.exitErr() if "JSESSIONID" not in self.cookies: logging.error("Not found value JSESSIONID in the response cookies") tools.exitErr() # get login/password self.username, self.password = out.GetUserCredentials() self.postData["login"]["username"] = self.username self.postData["login"]["password"] = self.password self.postData["login"]["targetUrl"] = self.url["oauth"] % self.tmpOAuthToken self.postData["login"]["hpts"] = hpts and hpts.group(1) or "" self.postData["login"]["hptsh"] = hptsh and hptsh.group(1) or "" response = self.loadPage( self.url["base"], self.url["login"] + ";jsessionid=" + self.cookies["JSESSIONID"], "POST", self.postData["login"], ) if not response.location and response.status == 200: if self.incorrectLogin < 3: out.preloader.stop() out.printLine("Sorry, incorrect login or password") out.preloader.setMessage("Authorize...") self.incorrectLogin += 1 return self.login() else: logging.error("Incorrect login or password") if not response.location: logging.error("Target URL was not found in the response on login") tools.exitErr() if response.status == 302: # the user has enabled two factor auth return self.handleTwoFactor() logging.debug("Success authorize, redirect to access page")
def login(self): response = self.loadPage(self.url['base'], self.url['login'], "GET", {'oauth_token': self.tmpOAuthToken}) # parse hpts and hptsh from page content hpts = re.search('.*\("hpts"\)\.value.*?"(.*?)"', response.data) hptsh = re.search('.*\("hptsh"\)\.value.*?"(.*?)"', response.data) if response.status != 200: logging.error("Unexpected response status " "on login 200 != %s", response.status) tools.exitErr() if 'JSESSIONID' not in self.cookies: logging.error("Not found value JSESSIONID in the response cookies") tools.exitErr() # get login/password self.username, self.password = out.GetUserCredentials() self.postData['login']['username'] = self.username self.postData['login']['password'] = self.password self.postData['login']['targetUrl'] = self.url['oauth'] % self.tmpOAuthToken self.postData['login']['hpts'] = hpts and hpts.group(1) or "" self.postData['login']['hptsh'] = hptsh and hptsh.group(1) or "" response = self.loadPage(self.url['base'], self.url['login'] + ";jsessionid=" + self.cookies['JSESSIONID'], "POST", self.postData['login']) if not response.location and response.status == 200: if self.incorrectLogin < 3: out.preloader.stop() out.printLine('Sorry, incorrect login or password') out.preloader.setMessage('Authorize...') self.incorrectLogin += 1 return self.login() else: logging.error("Incorrect login or password") if not response.location: logging.error("Target URL was not found in the response on login") tools.exitErr() # @todo response.status is always 302, regardless of whether TwoFactor is enabled # if response.status == 302: # # the user has enabled two factor auth # return self.handleTwoFactor() logging.debug("Success authorize, redirect to access page")
def login(self): response = self.loadPage(self.url['base'], self.url['login'], "GET", {'oauth_token': self.tmpOAuthToken}) # parse hpts and hptsh from page content hpts = re.search('.*\("hpts"\)\.value.*?"(.*?)"', response.data) hptsh = re.search('.*\("hptsh"\)\.value.*?"(.*?)"', response.data) if response.status != 200: logging.error("Unexpected response status " "on login 200 != %s", response.status) tools.exitErr() if 'JSESSIONID' not in self.cookies: logging.error("Not found value JSESSIONID in the response cookies") tools.exitErr() # get login/password self.username, self.password = out.GetUserCredentials() self.postData['login']['username'] = self.username self.postData['login']['password'] = self.password self.postData['login']['targetUrl'] = self.url['oauth'] % self.tmpOAuthToken self.postData['login']['hpts'] = hpts and hpts.group(1) or "" self.postData['login']['hptsh'] = hptsh and hptsh.group(1) or "" response = self.loadPage(self.url['base'], self.url['login'] + ";jsessionid=" + self.cookies['JSESSIONID'], "POST", self.postData['login']) if not response.location and response.status == 200: if self.incorrectLogin < 3: out.preloader.stop() out.printLine('Sorry, incorrect login or password') out.preloader.setMessage('Authorize...') self.incorrectLogin += 1 return self.login() else: logging.error("Incorrect login or password") if not response.location: logging.error("Target URL was not found in the response on login") tools.exitErr() if response.status == 302: # the user has enabled two factor auth return self.handleTwoFactor() logging.debug("Success authorize, redirect to access page")
def settings(self, editor=None, extras=None, note_ext=None): storage = self.getStorage() if editor: if editor == '#GET#': out.successMessage("Current editor is: %s" % getEditor(storage)) else: storage.setUserprop('editor', editor) out.successMessage("Changes saved.") if extras: if extras == '#GET#': out.successMessage("Current markdown2 extras is : %s" % getExtras(storage)) else: storage.setUserprop('markdown2_extras', extras.split(',')) out.successMessage("Changes saved.") if note_ext: if note_ext == '#GET#': out.successMessage("Default note extension is: %s" % getNoteExt(storage)) else: if len(note_ext.split(',')) == 2: storage.setUserprop('note_ext', note_ext.replace(" ","")) out.successMessage("Changes saved.") else: out.failureMessage("Error in note extension, format is '.markdown, .org'") if all([not editor, not extras, not note_ext]): editor = getEditor(storage) extras = getExtras(storage) note_ext = getNoteExt(storage) settings = ('Geeknote', '*' * 30, 'Version: %s' % __version__, 'App dir: %s' % config.APP_DIR, 'Error log: %s' % config.ERROR_LOG, 'Current editor: %s' % editor, 'Markdown2 Extras: %s' % (extras if extras is None else ','.join(extras)), 'Note extension: %s' % note_ext) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]['info'] settings += ('*' * 30, 'Username: %s' % user.username, 'Id: %s' % user.id, 'Email: %s' % user.email) out.printLine('\n'.join(settings))
def settings(self, editor=None, extras=None, note_ext=None): storage = self.getStorage() if editor: if editor == '#GET#': out.successMessage("Current editor is: %s" % getEditor(storage)) else: storage.setUserprop('editor', editor) out.successMessage("Changes saved.") if extras: if extras == '#GET#': out.successMessage("Current markdown2 extras is : %s" % getExtras(storage)) else: storage.setUserprop('markdown2_extras', extras.split(',')) out.successMessage("Changes saved.") if note_ext: if note_ext == '#GET#': out.successMessage("Default note extension is: %s" % getNoteExt(storage)) else: if len(note_ext.split(',')) == 2: storage.setUserprop('note_ext', note_ext.replace(" ", "")) out.successMessage("Changes saved.") else: out.failureMessage( "Error in note extension, format is '.markdown, .org'") if all([not editor, not extras, not note_ext]): editor = getEditor(storage) extras = getExtras(storage) note_ext = getNoteExt(storage) settings = ('Geeknote', '*' * 30, 'Version: %s' % __version__, 'App dir: %s' % config.APP_DIR, 'Error log: %s' % config.ERROR_LOG, 'Current editor: %s' % editor, 'Markdown2 Extras: %s' % (extras if extras is None else ','.join(extras)), 'Note extension: %s' % note_ext) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]['info'] settings += ('*' * 30, 'Username: %s' % user.username, 'Id: %s' % user.id, 'Email: %s' % user.email) out.printLine('\n'.join(settings))
def settings(self, editor=None, extras=None, note_ext=None): storage = self.getStorage() if editor: if editor == "#GET#": out.successMessage("Current editor is: %s" % getEditor(storage)) else: storage.setUserprop("editor", editor) out.successMessage("Changes saved.") if extras: if extras == "#GET#": out.successMessage("Current markdown2 extras is : %s" % getExtras(storage)) else: storage.setUserprop("markdown2_extras", extras.split(",")) out.successMessage("Changes saved.") if note_ext: if note_ext == "#GET#": out.successMessage("Default note extension is: %s" % getNoteExt(storage)) else: if len(note_ext.split(",")) == 2: storage.setUserprop("note_ext", note_ext.replace(" ", "")) out.successMessage("Changes saved.") else: out.failureMessage("Error in note extension, format is '.markdown, .org'") if all([not editor, not extras, not note_ext]): editor = getEditor(storage) extras = getExtras(storage) note_ext = getNoteExt(storage) settings = ( "Geeknote", "*" * 30, "Version: %s" % __version__, "App dir: %s" % config.APP_DIR, "Error log: %s" % config.ERROR_LOG, "Current editor: %s" % editor, "Markdown2 Extras: %s" % (extras if extras is None else ",".join(extras)), "Note extension: %s" % note_ext, ) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]["info"] settings += ("*" * 30, "Username: %s" % user.username, "Id: %s" % user.id, "Email: %s" % user.email) out.printLine("\n".join(settings))
def handleTwoFactor(self): self.code = out.GetUserAuthCode() self.postData['tfa']['code'] = self.code response = self.loadPage(self.url['base'], self.url['tfa'] + ";jsessionid=" + self.cookies['JSESSIONID'], "POST", self.postData['tfa']) if not response.location and response.status == 200: if self.incorrectCode < 3: out.preloader.stop() out.printLine('Sorry, incorrect two factor code') out.preloader.setMessage('Authorize...') self.incorrectCode += 1 return self.handleTwoFactor() else: logging.error("Incorrect two factor code") if not response.location: logging.error("Target URL was not found in the response on login") tools.exitErr()
def login(self): response = self.loadPage(self.url['base'], self.url['login'], "GET", {'oauth_token': self.tmpOAuthToken}) if response.status != 200: logging.error("Unexpected response status " "on login 200 != %s", response.status) tools.exitErr() if 'JSESSIONID' not in self.cookies: logging.error("Not found value JSESSIONID in the response cookies") tools.exitErr() # get login/password self.username, self.password = out.GetUserCredentials() self.postData['login']['username'] = self.username self.postData['login']['password'] = self.password self.postData['login']['targetUrl'] = self.url['oauth'] % self.tmpOAuthToken response = self.loadPage(self.url['base'], self.url['login'] + ";jsessionid=" + self.cookies['JSESSIONID'], "POST", self.postData['login']) if not response.location and response.status == 200: if self.incorrectLogin < 3: out.preloader.stop() out.printLine('Sorry, incorrect login or password') out.preloader.setMessage('Authorize...') self.incorrectLogin += 1 return self.login() else: logging.error("Incorrect login or password") if not response.location: logging.error("Target URL was not found in the response on login") tools.exitErr() if response.status == 302: # the user has enabled two factor auth return self.handleTwoFactor() logging.debug("Success authorize, redirect to access page")
def settings(self, editor=None, extras=None, note_ext=None): storage = self.getStorage() if editor: if editor == '#GET#': editor = storage.getUserprop('editor') if not editor: editor = os.environ.get("editor") if not editor: editor = os.environ.get("EDITOR") if not editor: editor = config.DEF_WIN_EDITOR if sys.platform == 'win32' else config.DEF_UNIX_EDITOR out.successMessage("Current editor is: %s" % editor) else: storage.setUserprop('editor', editor) out.successMessage("Changes have been saved.") if note_ext: if note_ext == '#GET#': note_ext = storage.getUserprop('note_ext') if not note_ext or not storage.getUserprop('note_ext'): note_ext = config.DEF_NOTE_EXT out.successMessage("Default note extension is: %s" % note_ext) else: storage.setUserprop('note_ext', note_ext) out.successMessage("Changes have been saved.") if all([not editor, not note_ext]): settings = ('Geeknote', '*' * 30, 'Version: %s' % config.VERSION, 'App dir: %s' % config.APP_DIR, 'Error log: %s' % config.ERROR_LOG, 'Current editor: %s' % storage.getUserprop('editor'), 'Note extension: %s' % storage.getUserprop('note_ext')) user_settings = storage.getUserprops() if user_settings: user = user_settings[1]['info'] settings += ('*' * 30, 'Username: %s' % user.username, 'Id: %s' % user.id, 'Email: %s' % user.email) out.printLine('\n'.join(settings))
def backup(self, notebook, outputFormat='console'): logging.debug("Backup to %s" % str(outputFormat)) notebook = self._searchNotebook(notebook) notes = self.getEvernote().loadNotes(notebook) if outputFormat == 'file': if not os.path.exists(notebook.name): os.mkdir(notebook.name) else: out.failureMessage("Folder %s already exist" % notebook.name) for n in notes: note = self.getEvernote().getNote(n.guid) if outputFormat == 'console': out.showNote(note) out.printLine('======================') if outputFormat == 'file': with opened(notebook.name + os.sep + note.title, "w") as f: with stdout_redirected(f): out.showNote(note)
def backup(self, notebook, outputFormat='console'): logging.debug("Backup to %s" % str(outputFormat)) notebook = self._searchNotebook(notebook) notes = self.getEvernote().loadNotes(notebook) if outputFormat=='file': if not os.path.exists(notebook.name): os.mkdir(notebook.name) else: out.failureMessage("Folder %s already exist" % notebook.name) for n in notes: note = self.getEvernote().getNote(n.guid) if outputFormat=='console': out.showNote(note) out.printLine('======================') if outputFormat=='file': with opened(notebook.name + os.sep + note.title, "w") as f: with stdout_redirected(f): out.showNote(note)
def dedup(self, search=None, tags=None, notebooks=None, date=None, exact_entry=None, content_search=None, with_url=None, count=None, ): request = self._createSearchRequest(search, tags, notebooks, date, exact_entry, content_search) if not count: count = 20 else: count = int(count) logging.debug("Search count: %s", count) createFilter = True if search == "*" else False notes = [] evernote = self.getEvernote() stillDownloadingResults = True while stillDownloadingResults: offset = len(notes) result = evernote.findNotes(request, count, createFilter, offset) notes += result.notes total = result.totalNotes limit = min(total, count) stillDownloadingResults = len(notes) < total and len(notes) < count out.printLine("Downloaded metadata for " + len(result.notes) + " notes (" + len(notes) + "/" + limit + " of " + count + ")") if total == 0: out.failureMessage("Notes have not been found.") return tools.exitErr() notes_dict = {} for note in notes: noteId = note.title + ":" + note.contentHash if noteId in notes_dict: notes_dict[noteId].append(note) out.printLine("found dup! \"" + note.title + "\" with guid " + note.guid + ", duplicated " + len(notes_dict[noteId])) else: notes_dict[noteId] = [note] out.printLine("new note \"" + note.title + "\" with guid " + note.guid) all_dups = [dups for id, dups in notes_dict.iteritems() if len(dups) > 1] # list of lists total_dups = sum(map(len, all_dups)) # count total removed_total = 0 for dup_group in all_dups: group_size = len(dup_group) out.printLine("Deleting " + group_size + " notes titled \"" + dup_group[0].title + "\"") for note in dup_group: removed_total += 1 out.printLine("Deleting \"" + note.title + "\" created " + out.printDate(note.created) + " with guid " + note.guid + " (" + removed_total + "/" + total_dups + ")") evernote.removeNote(note.guid) out.printLine("removed " + removed_total + "duplicates")
def printHelp(self): if self.CMD is None or self.CMD not in self.COMMANDS: tab = len(max(self.COMMANDS.keys(), key=len)) out.printLine("Available commands:") for cmd in sorted(self.COMMANDS): out.printLine( "%s : %s" % (cmd.rjust(tab, " "), self.COMMANDS[cmd]["help"])) else: tab = len( max(self.CMD_ARGS.keys() + self.CMD_FLAGS.keys(), key=len)) out.printLine("Options for: %s" % self.CMD) out.printLine("Available arguments:") for arg in self.CMD_ARGS: out.printLine("%s : %s%s" % ( arg.rjust(tab, " "), "[default] " if "firstArg" in self.COMMANDS[self.CMD] and self.COMMANDS[self.CMD]["firstArg"] == arg else "", self.CMD_ARGS[arg]["help"], )) if self.CMD_FLAGS: out.printLine("Available flags:") for flag in self.CMD_FLAGS: out.printLine( "%s : %s" % (flag.rjust(tab, " "), self.CMD_FLAGS[flag]["help"]))
def printHelp(self): if self.CMD is None or not self.COMMANDS.has_key(self.CMD): tab = len(max(self.COMMANDS.keys(), key=len)) out.printLine("Available commands:") for cmd in self.COMMANDS: out.printLine("%s : %s" % (cmd.rjust(tab, " "), self.COMMANDS[cmd]['help'])) else: tab = len(max(self.CMD_ARGS.keys()+self.CMD_FLAGS.keys(), key=len)) out.printLine("Options for: %s" % self.CMD) out.printLine("Available arguments:") for arg in self.CMD_ARGS: out.printLine("%s : %s%s" % ( arg.rjust(tab, " "), '[default] ' if self.COMMANDS[self.CMD].has_key('firstArg') and self.COMMANDS[self.CMD]['firstArg'] == arg else '', self.CMD_ARGS[arg]['help'])) if self.CMD_FLAGS: out.printLine("Available flags:") for flag in self.CMD_FLAGS: out.printLine("%s : %s" % (flag.rjust(tab, " "), self.CMD_FLAGS[flag]['help']))
def printErrorReqArgument(self, errorArg): out.printLine('Not found required argument "%s" for command "%s" ' % (errorArg, self.CMD)) self.printHelp()
def printErrorCommand(self): out.printLine('Unexpected command "%s"' % (self.CMD)) self.printHelp()
def printGrid(self, list): out.printLine(" ".join(list))
def dedup(self, search=None, tag=None, notebook=None, date=None, exact_entry=None, content_search=None, with_url=None, count=None): request = self._createSearchRequest(search, tag, notebook, date, exact_entry, content_search) if not count: count = 20 else: count = int(count) logging.debug("Search count: %s", count) createFilter = True if search == "*" else False notes = [] evernote = self.getEvernote() stillDownloadingResults = True while stillDownloadingResults: offset = len(notes) result = evernote.findNotes(request, count, createFilter, offset) notes += result.notes total = result.totalNotes limit = min(total, count) stillDownloadingResults = len(notes) < total and len(notes) < count out.printLine("Downloaded metadata for " + str(len(result.notes)) + " notes (" + str(len(notes)) + "/" + str(limit) + " of " + str(count) + ")") if total == 0: out.failureMessage("Notes have not been found.") return tools.exitErr() notes_dict = {} for note in notes: noteId = note.title + ":" + note.contentHash if noteId in notes_dict: notes_dict[noteId].append(note) out.printLine("found dup! \"" + note.title + "\" with guid " + note.guid + ", duplicated " + len(notes_dict[noteId])) else: notes_dict[noteId] = [note] out.printLine("new note \"" + note.title + "\" with guid " + note.guid) all_dups = [ dups for id, dups in notes_dict.iteritems() if len(dups) > 1 ] # list of lists total_dups = sum(map(len, all_dups)) # count total removed_total = 0 for dup_group in all_dups: group_size = len(dup_group) out.printLine("Deleting " + group_size + " notes titled \"" + dup_group[0].title + "\"") for note in dup_group: removed_total += 1 out.printLine("Deleting \"" + note.title + "\" created " + out.printDate(note.created) + " with guid " + note.guid + " (" + str(removed_total) + "/" + str(total_dups) + ")") evernote.removeNote(note.guid) out.printLine("removed " + removed_total + "duplicates")