|
@@ -2,9 +2,10 @@
|
|
|
import datetime
|
|
import datetime
|
|
|
import mimetypes
|
|
import mimetypes
|
|
|
import pathlib
|
|
import pathlib
|
|
|
-from typing import Dict, List, NamedTuple, Optional, Set, Tuple, Union, ValuesView
|
|
|
|
|
|
|
+from typing import Dict, List, NamedTuple, Optional, Set, Tuple, Union, ValuesView, Any
|
|
|
|
|
|
|
|
import click
|
|
import click
|
|
|
|
|
+import click_config_file
|
|
|
import frontmatter
|
|
import frontmatter
|
|
|
import gkeepapi
|
|
import gkeepapi
|
|
|
from gkeepapi.node import NodeAudio, NodeDrawing, NodeImage
|
|
from gkeepapi.node import NodeAudio, NodeDrawing, NodeImage
|
|
@@ -14,14 +15,29 @@ from pathvalidate import sanitize_filename
|
|
|
mimetypes.add_type("audio/3gpp", ".3gp")
|
|
mimetypes.add_type("audio/3gpp", ".3gp")
|
|
|
|
|
|
|
|
|
|
|
|
|
-def login(user_email: str, password: str) -> gkeepapi.Keep:
|
|
|
|
|
|
|
+def login(
|
|
|
|
|
+ user_email: str, password: Optional[str], token: Optional[str]
|
|
|
|
|
+) -> gkeepapi.Keep:
|
|
|
keep = gkeepapi.Keep()
|
|
keep = gkeepapi.Keep()
|
|
|
- try:
|
|
|
|
|
- keep.login(user_email, password)
|
|
|
|
|
- except gkeepapi.exception.LoginException as ex:
|
|
|
|
|
- raise click.BadParameter(f"Login failed: {str(ex)}")
|
|
|
|
|
-
|
|
|
|
|
- return keep
|
|
|
|
|
|
|
+ if token:
|
|
|
|
|
+ try:
|
|
|
|
|
+ click.echo("Logging in with token")
|
|
|
|
|
+ keep.resume(user_email, token)
|
|
|
|
|
+ print(keep.getMasterToken())
|
|
|
|
|
+ return keep
|
|
|
|
|
+ except gkeepapi.exception.LoginException as ex:
|
|
|
|
|
+ raise click.BadParameter(f"Token login (resume) failed: {str(ex)}")
|
|
|
|
|
+
|
|
|
|
|
+ if password:
|
|
|
|
|
+ try:
|
|
|
|
|
+ click.echo("Logging in with password")
|
|
|
|
|
+ keep.login(user_email, password)
|
|
|
|
|
+ print(keep.getMasterToken())
|
|
|
|
|
+ return keep
|
|
|
|
|
+ except gkeepapi.exception.LoginException as ex:
|
|
|
|
|
+ raise click.BadParameter(f"Password login failed: {str(ex)}")
|
|
|
|
|
+
|
|
|
|
|
+ raise click.BadParameter(f"Neither password nor token provided to login.")
|
|
|
|
|
|
|
|
|
|
|
|
|
def all_note_media(
|
|
def all_note_media(
|
|
@@ -398,149 +414,3 @@ def delete_local_only_files(
|
|
|
|
|
|
|
|
return (deleted_notes, deleted_media)
|
|
return (deleted_notes, deleted_media)
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-@click.command(
|
|
|
|
|
- context_settings={"max_content_width": 120, "help_option_names": ["-h", "--help"]}
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--user",
|
|
|
|
|
- "-u",
|
|
|
|
|
- prompt=True,
|
|
|
|
|
- required=True,
|
|
|
|
|
- envvar="GKEEP_USER",
|
|
|
|
|
- show_envvar=True,
|
|
|
|
|
- help="Google account email (prompt if empty)",
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--password",
|
|
|
|
|
- "-p",
|
|
|
|
|
- prompt=True,
|
|
|
|
|
- required=True,
|
|
|
|
|
- envvar="GKEEP_PASSWORD",
|
|
|
|
|
- show_envvar=True,
|
|
|
|
|
- help="Google account password (prompt if empty)",
|
|
|
|
|
- hide_input=True,
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--directory",
|
|
|
|
|
- "-d",
|
|
|
|
|
- default="./gkeep-export",
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Output directory for exported notes",
|
|
|
|
|
- type=click.Path(file_okay=False, dir_okay=True, writable=True),
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--header/--no-header",
|
|
|
|
|
- default=True,
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Choose to include or exclude the frontmatter header",
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--delete-local/--no-delete-local",
|
|
|
|
|
- default=False,
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Choose to delete or leave as-is any notes that exist locally but not in Google Keep",
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--rename-local/--no-rename-local",
|
|
|
|
|
- default=False,
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Choose to rename or leave as-is any notes that change titles in Google Keep",
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--date-format",
|
|
|
|
|
- default="%Y-%m-%d",
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Date format to use for the prefix of the note filenames. Reflects the created date of the note.",
|
|
|
|
|
-)
|
|
|
|
|
-@click.option(
|
|
|
|
|
- "--skip-existing-media/--no-skip-existing-media",
|
|
|
|
|
- default=True,
|
|
|
|
|
- show_default=True,
|
|
|
|
|
- help="Skip existing media if it appears unchanged from the local copy.",
|
|
|
|
|
-)
|
|
|
|
|
-def main(
|
|
|
|
|
- directory: str,
|
|
|
|
|
- user: str,
|
|
|
|
|
- password: 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}")
|
|
|
|
|
-
|
|
|
|
|
- click.echo("Logging in.")
|
|
|
|
|
- keep = login(user, password)
|
|
|
|
|
-
|
|
|
|
|
- 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")
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-if __name__ == "__main__":
|
|
|
|
|
- main()
|
|
|