Beispiel #1
0
def sync(local_dir, notebook_name):
    if not os.path.exists(local_dir):
        error("{} doesn't exist".format(local_dir))
    with utils.chdir(local_dir):
        client = EvernoteClient(service_host=service_host, token=dev_token)
        store = client.get_note_store()
        notebook = tset_notebook(store, notebook_name)
        files = utils.filter_files('.', supported_file_exts)
        log('Found {} note files under {}'.format(len(files), os.getcwd()))
        result = upsert_notes(store, files, notebook)
    log('''Sync completed.
    Created:{created}
    Updated:{updated}
    Removed:{removed}'''.format(**result))
Beispiel #2
0
def tset_notebook(store, name):
    """Return notebook with specific name.
    Create a new notebook if there isn't an existing one.
    """
    notebooks = store.listNotebooks()
    notebook = [nb for nb in notebooks if nb.name == name]
    if notebook:
        log('Found notebook {}'.format(name))
        return notebook[0]
    else:
        notebook = Notebook()
        notebook.name = name
        notebook.defaultNotebook = False
        log('Create new notebook {}'.format(name))
        return store.createNotebook(dev_token, notebook)
Beispiel #3
0
def tset_notebook(store, name):
    """Return notebook with specific name.
    Create a new notebook if there isn't an existing one.
    """
    notebooks = store.listNotebooks()
    notebook = [nb for nb in notebooks if nb.name == name]
    if notebook:
        log('Found notebook {}'.format(name))
        return notebook[0]
    else:
        notebook = Notebook()
        notebook.name = name
        notebook.defaultNotebook = False
        log('Create new notebook {}'.format(name))
        return store.createNotebook(dev_token, notebook)
Beispiel #4
0
def sync(local_dir, notebook_name):
    if not os.path.exists(local_dir):
        error("{} doesn't exist".format(local_dir))
    with utils.chdir(local_dir):
        client = EvernoteClient(service_host=service_host, token=dev_token)
        store = client.get_note_store()
        notebook = tset_notebook(store, notebook_name)
        files = utils.filter_files('.', supported_file_exts)
        log('Found {} note files under {}'.format(
            len(files), os.getcwd()))
        result = upsert_notes(store, files, notebook)
    log('''Sync completed.
    Created:{created}
    Updated:{updated}
    Removed:{removed}'''.format(**result))
Beispiel #5
0
def main():
    if not dev_token:
        log('Please set your evernote developer token in EVERNOTE_DEV_TOKEN.')
        sys.exit(1)
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("--dir", default='.', help="Local directory path (default: .)")
    parser.add_argument("--notebook", default="Eversync",
                        help="Notebook in Evernote that local files would be synced to (default: Eversync) ")
    parser.add_argument("--force", default=False, action="store_true",
                        help="Force sync with evernote even there's no change after last sync (default: False)")
    parser.add_argument("--debug", default=False, action="store_true",
                        help="Print debug information")
    args = parser.parse_args()
    if args.debug:
        debug_mode(True)

    if args.force or changed_since_last_sync(args.dir, args.notebook):
        sync(args.dir, args.notebook)
        save_sync_time(args.dir, args.notebook)
    else:
        log("No changes found after last sync time.".format(
            last_sync_time(args.dir, args.notebook)
        ))
Beispiel #6
0
def main():
    if not dev_token:
        log('Please set your evernote developer token in EVERNOTE_DEV_TOKEN.')
        sys.exit(1)
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("--dir",
                        default='.',
                        help="Local directory path (default: .)")
    parser.add_argument(
        "--notebook",
        default="Eversync",
        help=
        "Notebook in Evernote that local files would be synced to (default: Eversync) "
    )
    parser.add_argument(
        "--force",
        default=False,
        action="store_true",
        help=
        "Force sync with evernote even there's no change after last sync (default: False)"
    )
    parser.add_argument("--debug",
                        default=False,
                        action="store_true",
                        help="Print debug information")
    args = parser.parse_args()
    if args.debug:
        debug_mode(True)

    if args.force or changed_since_last_sync(args.dir, args.notebook):
        sync(args.dir, args.notebook)
        save_sync_time(args.dir, args.notebook)
    else:
        log("No changes found after last sync time.".format(
            last_sync_time(args.dir, args.notebook)))
Beispiel #7
0
def upsert_notes(store, files, notebook):
    """Create or update notes from content in local files.
    Technical notes:
    We store file path in sourceURL attribute of a note. And
    use this sourceURL to match notes and files.
    """
    result = Counter({'created': 0, 'updated': 0, 'removed': 0})
    note_filter = NoteStore.NoteFilter()
    note_filter.words = 'notebook:{}'.format(notebook.name)
    existing_notes = store.findNotes(dev_token, note_filter, 0,
                                     max_note_count).notes
    # Remove notes
    file_urls = set(utils.path_to_source_url(notebook, f) for f in files)
    removed_notes = [
        n for n in existing_notes if n.attributes.sourceURL not in file_urls
    ]
    for n in removed_notes:
        log('Removed note [{}]({})'.format(n.title, notebook.name))
        store.deleteNote(dev_token, n.guid)
    result.update({'removed': len(removed_notes)})
    # Create or update notes
    url_to_notes = {n.attributes.sourceURL: n for n in existing_notes}
    for f in files:
        url = utils.path_to_source_url(notebook, f)
        note = url_to_notes.get(url)
        last_modified = os.path.getmtime(f)
        if note:
            if last_modified * 1000 > note.updated:
                debug('{} has been modified since last sync.'.format(f))
                update_note(store, note, f)
                log('Updated [{}](Notebook:{}) from {}'.format(
                    note.title, notebook.name, f))
                result.update({'updated': 1})
        else:
            debug('{} is a new note file.'.format(f))
            note = create_note(store, f, notebook)
            log('Created [{}](Notebook:{}) from {}'.format(
                note.title, notebook.name, f))
            result.update({'created': 1})
    return result
Beispiel #8
0
def upsert_notes(store, files, notebook):
    """Create or update notes from content in local files.
    Technical notes:
    We store file path in sourceURL attribute of a note. And
    use this sourceURL to match notes and files.
    """
    result = Counter({'created':0, 'updated':0, 'removed':0})
    note_filter = NoteStore.NoteFilter()
    note_filter.words = 'notebook:{}'.format(notebook.name)
    existing_notes = store.findNotes(dev_token, note_filter, 0, max_note_count).notes
    # Remove notes
    file_urls = set(utils.path_to_source_url(notebook, f) for f in files)
    removed_notes = [n for n in existing_notes if n.attributes.sourceURL not in file_urls]
    for n in removed_notes:
        log('Removed note [{}]({})'.format(n.title, notebook.name))
        store.deleteNote(dev_token, n.guid)
    result.update({'removed': len(removed_notes)})
    # Create or update notes
    url_to_notes = {n.attributes.sourceURL: n for n in existing_notes}
    for f in files:
        url = utils.path_to_source_url(notebook, f)
        note = url_to_notes.get(url)
        last_modified = os.path.getmtime(f)
        if note:
            if last_modified * 1000 > note.updated:
                debug('{} has been modified since last sync.'.format(f))
                update_note(store, note, f)
                log('Updated [{}](Notebook:{}) from {}'.format(
                    note.title, notebook.name, f))
                result.update({'updated': 1})
        else:
            debug('{} is a new note file.'.format(f))
            note = create_note(store, f, notebook)
            log('Created [{}](Notebook:{}) from {}'.format(
                note.title, notebook.name, f))
            result.update({'created': 1})
    return result