예제 #1
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("directory", default=None, nargs="?",
                        help="Directory of your notes are stored. Default to notes/")
    parser.add_argument("--debug", action="store_true",
                        help="Help debug by opening the browser in the foreground. Note that the "
                             "git repository will not be updated with that option.")
    parser.add_argument("--database", default=None,
                        help="If you have multiple Roam databases, select the one you want to save."
                             "Can also be configured with env variable ROAMRESEARCH_DATABASE.")
    parser.add_argument("--skip-git", action="store_true",
                        help="Consider the repository as just a directory, and don't do any "
                             "git-related action.")
    parser.add_argument("--skip-push", action="store_true",
                        help="Don't git push after commit.")
    parser.add_argument("--skip-fetch", action="store_true",
                        help="Do not download the data from Roam, just update the formatting.")
    parser.add_argument("--sleep-duration", type=float, default=2.,
                        help="Duration to wait for the interface. We wait 100x that duration for"
                             "Roam to load. Increase it if Roam servers are slow, but be careful"
                             "with the free tier of Github Actions.")
    args = parser.parse_args()

    patch_pyppeteer()
    if args.directory is None:
        git_path = Path("notes").absolute()
    else:
        git_path = Path(args.directory).absolute()

    if (git_path / ".env").exists():
        logger.info("Loading secrets from {}", git_path / ".env")
        load_dotenv(git_path / ".env", override=True)
    else:
        logger.debug("No secret found at {}", git_path / ".env")
    if "ROAMRESEARCH_USER" not in os.environ or "ROAMRESEARCH_PASSWORD" not in os.environ:
        logger.error("Please define ROAMRESEARCH_USER and ROAMRESEARCH_PASSWORD, "
                     "in the .env file of your notes repository, or in environment variables")
        sys.exit(1)
    config = Config(args.database, debug=args.debug, sleep_duration=float(args.sleep_duration))

    if args.skip_git:
        repo = None
    else:
        repo = git.Repo(git_path)
        assert not repo.bare  # Fail fast if it's not a repo

    reset_git_directory(git_path / "formatted")
    if not args.skip_fetch:
        reset_git_directory(git_path / "json")
        reset_git_directory(git_path / "markdown")

        with tempfile.TemporaryDirectory() as markdown_zip_path, \
                tempfile.TemporaryDirectory() as json_zip_path:
            markdown_zip_path = Path(markdown_zip_path)
            json_zip_path = Path(json_zip_path)

            scrap(markdown_zip_path, json_zip_path, config)
            if config.debug:
                logger.debug("waiting for the download...")
                time.sleep(20)
                return
            #raws = unzip_markdown_archive(markdown_zip_path)
            #save_markdowns(git_path / "markdown", raws)
            unzip_and_save_json_archive(json_zip_path, git_path / "json")

    #formatted = format_markdown(read_markdown_directory(git_path / "markdown"))
    #save_markdowns(git_path / "formatted", formatted)

    if repo is not None:
        commit_git_directory(repo)
        if not args.skip_push:
            push_git_repository(repo)
예제 #2
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "directory",
        default=None,
        nargs="?",
        help="Directory of your notes are stored. Default to notes/")
    parser.add_argument(
        "--debug",
        action="store_true",
        help=
        "Help debug by opening the browser in the foreground. Note that the "
        "git repository will not be updated with that option.")
    parser.add_argument(
        "--database",
        default=None,
        help=
        "If you have multiple Roam databases, select the one you want to save."
        "Can also be configured with env variable ROAMRESEARCH_DATABASE.")
    parser.add_argument(
        "--skip-git",
        action="store_true",
        help="Consider the repository as just a directory, and don't do any "
        "git-related action.")
    parser.add_argument("--skip-push",
                        action="store_true",
                        help="Don't git push after commit.")
    parser.add_argument(
        "--sleep-duration",
        type=float,
        default=2.,
        help="Duration to wait for the interface. We wait 100x that duration for"
        "Roam to load. Increase it if Roam servers are slow, but be careful"
        "with the free tier of Github Actions.")
    parser.add_argument("--browser-arg",
                        help="Flags to pass through to launched browser.",
                        action='append')
    parser.add_argument(
        "--formats",
        "-f",
        action=ExtendAction,
        nargs="+",
        type=str,
        help="Which formats to save. Options include json, markdown, formatted, "
        "and edn. Note that if only formatted is specified, the markdown "
        "directory will be converted to a formatted directory skipping "
        "fetching entirely. Also note that if jet is installed, the edn "
        "output will be pretty printed allowing for cleaner git diffs.")
    args = parser.parse_args()

    patch_pyppeteer()
    if args.directory is None:
        git_path = Path("notes").absolute()
    else:
        git_path = Path(args.directory).absolute()

    if (git_path / ".env").exists():
        logger.info("Loading secrets from {}", git_path / ".env")
        load_dotenv(git_path / ".env", override=True)
    else:
        logger.debug("No secret found at {}", git_path / ".env")
    if "ROAMRESEARCH_USER" not in os.environ or "ROAMRESEARCH_PASSWORD" not in os.environ:
        logger.error(
            "Please define ROAMRESEARCH_USER and ROAMRESEARCH_PASSWORD, "
            "in the .env file of your notes repository, or in environment variables"
        )
        sys.exit(1)
    config = Config(args.database,
                    debug=args.debug,
                    sleep_duration=float(args.sleep_duration),
                    browser_args=args.browser_arg)

    if args.skip_git:
        repo = None
    else:
        repo = git.Repo(git_path)
        assert not repo.bare  # Fail fast if it's not a repo

    if args.formats is None or len(args.formats) == 0:
        args.formats = DEFAULT_FORMATS

    if any(f not in ALL_FORMATS for f in args.formats):
        logger.error("The format values must be one of {}.", ALL_FORMATS)
        sys.exit(1)

    # reset all directories to be modified
    for f in args.formats:
        reset_git_directory(git_path / f)

    # check if we need to fetch a format from roam
    roam_formats = [f for f in args.formats if f in ROAM_FORMATS]
    if len(roam_formats) > 0:
        with tempfile.TemporaryDirectory() as root_zip_path:
            root_zip_path = Path(root_zip_path)
            scrap(root_zip_path, roam_formats, config)
            if config.debug:
                logger.debug("waiting for the download...")
                time.sleep(20)
                return
            # Unzip and save all the downloaded files.
            for f in roam_formats:
                unzip_and_save_archive(f, root_zip_path / f, git_path / f)

    if "formatted" in args.formats:
        formatted = format_markdown(
            read_markdown_directory(git_path / "markdown"))
        save_files("formatted", git_path / "formatted", formatted)

    if repo is not None:
        commit_git_directory(repo)
        if not args.skip_push:
            push_git_repository(repo)