GithubHelp home page GithubHelp logo

paginate-json's Introduction

paginate-json

PyPI CircleCI License

CLI tool for retrieving JSON from paginated APIs.

Currently works against APIs that use the HTTP Link header for pagination. The GitHub API is the most obvious example.

Usage: paginate-json [OPTIONS] URL

  Fetch paginated JSON from a URL

Options:
  --version                Show the version and exit.
  --nl                     Output newline-delimited JSON
  --jq TEXT                jq transformation to run on each page
  --accept TEXT            Accept header to send
  --sleep INTEGER          Seconds to delay between requests
  --silent                 Don't show progress on stderr
  --show-headers           Dump response headers out to stderr
  --header <TEXT TEXT>...  Send custom request headers
  --help                   Show this message and exit.

The --jq option only works if you install the optional pyjq dependency.

Works well in conjunction with sqlite-utils. For example, here's how to load all of the GitHub issues for a project into a local SQLite database.

paginate-json \
    "https://api.github.com/repos/simonw/datasette/issues?state=all&filter=all" \
    --nl | \
    sqlite-utils upsert /tmp/issues.db issues - --nl --pk=id

You can then use other features of sqlite-utils to enhance the resulting database. For example, to enable full-text search on the issue title and body columns:

sqlite-utils enable-fts /tmp/issues.db issues title body

You can use the --header option to send additional request headers. For example, if you have a GitHub OAuth token you can pass it like this:

paginate-json https://api.github.com/users/simonw/events \
  --header Authorization "bearer e94d9e404d86..."

paginate-json's People

Contributors

simonw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

paginate-json's Issues

Handle relative URLs in Link header

It would be super-handy if this tool were able to handle relative URLs in the link header; something like:

Link: </widgets?page=2>;rel="next"

which would be joined with the original URL. This would be relatively straightforward to implement using requests.compat.urljoin:

diff --git a/paginate_json/cli.py b/paginate_json/cli.py
index 4be0fb4..555577d 100644
--- a/paginate_json/cli.py
+++ b/paginate_json/cli.py
@@ -63,7 +63,11 @@ def paginate(
                 json.dumps(dict(response.headers), indent=4, default=repr), err=True
             )
         try:
-            url = response.links.get("next").get("url")
+            # Get the 'next' link
+            next = response.links.get("next").get("url")
+
+            # Resolve the potentially-relative URL to be an absolute URL
+            url = requests.compat.urljoin(url, next)
         except AttributeError:
             url = None
         if jq:

Thoughts? Would you be open to a PR that implements this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.