Esempio n. 1
0
class EvernoteCli(object):
    def __init__(self, default_notebook, api=None):
        if api:
            self.api = api
        else:
            self.api = EvernoteApi()

        self.default_notebook = default_notebook

    def _get_notebook_name(self, notebook_name):
        if not notebook_name:
            return self.default_notebook
        else:
            return notebook_name

    def _get_note_content(self, note):
        return HTML2Text().handle(unicode(note.content, 'utf_8'))


    def list_notes(self, notebook_name=None):
        notebook_name = self._get_notebook_name(notebook_name)
        notes = self.api.list_notes(notebook_name)

        return notes

    def list_notebooks(self):
        return self.api.list_notebooks()

    def print_notebooks(self):
        notebooks = self.list_notebooks()
        if not notebooks:
            return

        for notebook in notebooks:
            print notebook.name

    def print_notes(self, notebook_name):
        notes = self.list_notes(notebook_name)
        if not notes:
            return

        notes.reverse()
        for note in notes:
            print note.title

    def print_note(self, note_title, notebook_name):
        notebook_name = self._get_notebook_name(notebook_name)
        note = self.api.get_note(note_title, notebook_name)

        if note is None:
            print 'Note {0} does not exist'.format(note_title)
            return

        print self._get_note_content(note)

    def edit_or_add(self, note_title, notebook_name):
        #TODO: do something to deal with duplicate notes
        notebook_name = self._get_notebook_name(notebook_name)

        temp_file = tempfile.NamedTemporaryFile(delete=False)
        note = self.api.get_note(note_title, notebook_name)

        if note is not None:
            markdown_content = self._get_note_content(note)
            temp_file.write(markdown_content.encode('utf8'))
            temp_file = self.edit_file(temp_file)
            content = temp_file.read()
            self.api.update_note(note_title, content, notebook_name)
        else:
            temp_file = self.edit_file(temp_file) 
            content = temp_file.read()
            self.api.create_note(note_title, content, notebook_name)

        temp_file.close()

    def edit_file(self, file_object):
        file_object.close()
        os.system('vim {0}'.format(file_object.name))
        return open(file_object.name, 'r')

    def refresh(self):
        self.api.refresh_cache()
Esempio n. 2
0
def get_api_and_refresh_cache():
    api = EvernoteApi()
    assert_equal('True', api.config.get('testing', 'use_sandbox'))
    api.refresh_cache()
    return api