From e44ebee3aea72a509e9f9de10d912aac08e0b44a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 16 Sep 2020 20:38:28 -0700 Subject: [PATCH] Simplified get() implementation, refs #50 --- github_to_sqlite/cli.py | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/github_to_sqlite/cli.py b/github_to_sqlite/cli.py index a335f2c..0194eda 100644 --- a/github_to_sqlite/cli.py +++ b/github_to_sqlite/cli.py @@ -465,28 +465,27 @@ def emojis(db_path, auth, fetch): def get(url, auth, paginate, nl): "Save repos owened by the specified (or authenticated) username or organization" token = load_token(auth) - if paginate or nl: - first = True - while url: - response = utils.get(url, token) - items = response.json() - if first and not nl: - click.echo("[") - for item in items: - if not first and not nl: - click.echo(",") - first = False - if not nl: - to_dump = json.dumps(item, indent=4) - click.echo(textwrap.indent(to_dump, " "), nl=False) - else: - click.echo(json.dumps(item)) - url = response.links.get("next", {}).get("url") - if not nl: - click.echo("\n]") - else: + first = True + while url: response = utils.get(url, token) - click.echo(json.dumps(response.json(), indent=4)) + items = response.json() + if first and not nl: + click.echo("[") + for item in items: + if not first and not nl: + click.echo(",") + first = False + if not nl: + to_dump = json.dumps(item, indent=4) + click.echo(textwrap.indent(to_dump, " "), nl=False) + else: + click.echo(json.dumps(item)) + if paginate: + url = response.links.get("next", {}).get("url") + else: + url = None + if not nl: + click.echo("\n]") def load_token(auth):