keep_exporter/export.py
def main(
    directory: str,
    user: str,
    password: Optional[str],
    token: Optional[str],
    header: bool,
    delete_local: bool,
    rename_local: bool,
    date_format: str,
    skip_existing_media: bool,
):
    """A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header."""
    notepath = pathlib.Path(directory).resolve()
    mediapath = notepath.joinpath("media/")

    click.echo(f"Notes directory: {notepath}")
    click.echo(f"Media directory: {mediapath}")

    keep = login(user, password, token)

    if not notepath.exists():
        click.echo("Notes directory does not exist, creating.")
        notepath.mkdir(parents=True)

    if not mediapath.exists():
        click.echo("Media directory does not exist, creating.")
        mediapath.mkdir(parents=True)

    click.echo("Indexing local files.")
    local_index = index_existing_files(notepath)

    click.echo("Indexing remote notes.")
    keep_notes = dict([(note.id, note) for note in keep.all()])

    skipped_notes, updated_notes, new_notes = 0, 0, 0
    downloaded_media = 0
    deleted_notes, deleted_media = delete_local_only_files(
        local_index, keep_notes, delete_local
    )

    for note in keep_notes.values():  # type: gkeepapi._node.Note
        local_note = local_index.get(note.id)
        if not local_note:
            click.echo(f"Downloading new note {note.id}")
            new_notes += 1

        target_path = build_note_unique_path(notepath, note, date_format, local_index)

        local_path = local_index.get(note.id, LocalNote(note.id)).path
        if local_path:
            if rename_local and local_path != target_path:
                target_path = try_rename_note(local_index[note.id], target_path)
            else:
                target_path = local_path

        # decide to skip after the rename (due to date format change) has a chance
        if local_note:
            if local_note.timestamp_updated == note.timestamps.updated:
                skipped_notes += 1
                continue
            else:
                updated_notes += 1
                click.echo(f"Updating existing file for note {note.id}")

        images, downloaded = download_media(keep, note, mediapath, skip_existing_media)
        markdown = build_markdown(note, images)

        downloaded_media += downloaded

        with target_path.open("wb+") as f:
            if header:
                fmatter = build_frontmatter(note, markdown)
                frontmatter.dump(fmatter, f)
            else:
                f.write(markdown.encode("utf-8"))

    click.echo("Finished syncing.")
    click.echo(
        f"Notes: {skipped_notes} unchanged, {updated_notes} updated, {new_notes} new, {deleted_notes} deleted"
    )
    click.echo(f"Media: {downloaded_media} downloaded, {deleted_media} deleted")
A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header.
A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header.
A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header .
def main(
    directory: str,
    user: str,
    password: Optional[str],
    token: Optional[str],
    header: bool,
    delete_local: bool,
    rename_local: bool,
    date_format: str,
    skip_existing_media: bool,
):
    notepath = pathlib.Path(directory).resolve()
    mediapath = notepath.joinpath("media/")
    click.echo(f"Notes directory: {notepath}")
    click.echo(f"Media directory: {mediapath}")
    keep = login(user, password, token)
    if not notepath.exists():
        click.echo("Notes directory does not exist, creating.")
        notepath.mkdir(parents=True)
    if not mediapath.exists():
        click.echo("Media directory does not exist, creating.")
        mediapath.mkdir(parents=True)
    click.echo("Indexing local files.")
    local_index = index_existing_files(notepath)
    click.echo("Indexing remote notes.")
    keep_notes = dict([(note.id, note) for note in keep.all()])
    skipped_notes, updated_notes, new_notes = 0, 0, 0
    downloaded_media = 0
    deleted_notes, deleted_media = delete_local_only_files(
        local_index, keep_notes, delete_local
    )
    for note in keep_notes.values():  
        local_note = local_index.get(note.id)
        if not local_note:
            click.echo(f"Downloading new note {note.id}")
            new_notes += 1
        target_path = build_note_unique_path(notepath, note, date_format, local_index)
        local_path = local_index.get(note.id, LocalNote(note.id)).path
        if local_path:
            if rename_local and local_path != target_path:
                target_path = try_rename_note(local_index[note.id], target_path)
            else:
                target_path = local_path
        if local_note:
            if local_note.timestamp_updated == note.timestamps.updated:
                skipped_notes += 1
                continue
            else:
                updated_notes += 1
                click.echo(f"Updating existing file for note {note.id}")
        images, downloaded = download_media(keep, note, mediapath, skip_existing_media)
        markdown = build_markdown(note, images)
        downloaded_media += downloaded
        with target_path.open("wb+") as f:
            if header:
                fmatter = build_frontmatter(note, markdown)
                frontmatter.dump(fmatter, f)
            else:
                f.write(markdown.encode("utf-8"))
    click.echo("Finished syncing.")
    click.echo(
        f"Notes: {skipped_notes} unchanged, {updated_notes} updated, {new_notes} new, {deleted_notes} deleted"
    )
    click.echo(f"Media: {downloaded_media} downloaded, {deleted_media} deleted")
def main ( directory : str , user : str , password : Optional [ str ] , token : Optional [ str ] , header : bool , delete_local : bool , rename_local : bool , date_format : str , skip_existing_media : bool , ) : notepath = pathlib . Path ( directory ) . resolve ( ) mediapath = notepath . joinpath ( "media/" ) click . echo ( f"Notes directory: {notepath}" ) click . echo ( f"Media directory: {mediapath}" ) keep = login ( user , password , token ) if not notepath . exists ( ) : click . echo ( "Notes directory does not exist, creating." ) notepath . mkdir ( parents = True ) if not mediapath . exists ( ) : click . echo ( "Media directory does not exist, creating." ) mediapath . mkdir ( parents = True ) click . echo ( "Indexing local files." ) local_index = index_existing_files ( notepath ) click . echo ( "Indexing remote notes." ) keep_notes = dict ( [ ( note . id , note ) for note in keep . all ( ) ] ) skipped_notes , updated_notes , new_notes = 0 , 0 , 0 downloaded_media = 0 deleted_notes , deleted_media = delete_local_only_files ( local_index , keep_notes , delete_local ) for note in keep_notes . values ( ) : local_note = local_index . get ( note . id ) if not local_note : click . echo ( f"Downloading new note {note.id}" ) new_notes += 1 target_path = build_note_unique_path ( notepath , note , date_format , local_index ) local_path = local_index . get ( note . id , LocalNote ( note . id ) ) . path if local_path : if rename_local and local_path != target_path : target_path = try_rename_note ( local_index [ note . id ] , target_path ) else : target_path = local_path if local_note : if local_note . timestamp_updated == note . timestamps . updated : skipped_notes += 1 continue else : updated_notes += 1 click . echo ( f"Updating existing file for note {note.id}" ) images , downloaded = download_media ( keep , note , mediapath , skip_existing_media ) markdown = build_markdown ( note , images ) downloaded_media += downloaded with target_path . open ( "wb+" ) as f : if header : fmatter = build_frontmatter ( note , markdown ) frontmatter . dump ( fmatter , f ) else : f . write ( markdown . encode ( "utf-8" ) ) click . echo ( "Finished syncing." ) click . echo ( f"Notes: {skipped_notes} unchanged, {updated_notes} updated, {new_notes} new, {deleted_notes} deleted" ) click . echo ( f"Media: {downloaded_media} downloaded, {deleted_media} deleted" )
A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header.
A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header .
"""A simple utility to export google keep notes to markdown files with metadata stored as a frontmatter header.""" # type: gkeepapi._node.Note # decide to skip after the rename (due to date format change) has a chance
directory
str
user
str
password
Optional[str]
token
Optional[str]
header
bool
delete_local
bool
rename_local
bool
date_format
str
skip_existing_media
bool