How to find the last page of github api using urllib

Viewed 37

I am using a script for getting the metrics of github repositories using github API.

The line which sends the request is like this:

import urllib.request

request_url = urllib.request.urlopen('https://api.github.com/repos/freeCodeCamp/freeCodeCamp/commits?page=1&per_page=100')

I want to find the last page that contains information, so only get the data of that page (for example, page=20). How should I do it?

1 Answers

I would suggest to use GitHub's GraphQL API. This will return what you want. Try it out in the explorer here

{
  repository(owner: "freeCodeCamp", name: "freeCodeCamp") {
    defaultBranchRef {
      target {
        ... on Commit {
          history {
            totalCount
            edges {
              node {
                oid
                pushedDate
              }
            }
            pageInfo {
              hasNextPage
            }
          }
        }
      }
    }
  }
}
Related