Github API call for user accounts

api, github, github-api, python

Solution

You can use GraphQL Api v4 to be able to request the specific information you want from the users. In the following query, you search user with `location:uk` & extract their login, name, their followers, follower count, repositories, repository count, languages etc...

{
  search(query: "location:uk", type: USER, first: 100) {
    userCount
    pageInfo {
      hasNextPage
      endCursor
    }
    nodes {
      ... on User {
        login
        name
        location
        repositories(first: 10) {
          totalCount
          nodes {
            languages(first: 2) {
              nodes {
                name
              }
            }
            name
          }
        }
        followers(first: 10) {
          totalCount
          nodes {
            login
          }
        }
      }
    }
  }
}

Try it in the explorer

For the pagination, use `first: 100` to request the first 100 items & use `after: <cursor_value>` to request the next page, the cursor value is the last cursor of the previous page eg the value of `pageInfo.endCursor` in the previous query.

In Python, this would be :

import json
import requests

access_token = "YOUR_ACCESS_TOKEN"

query = """
{
    search(query: "location:uk", type: USER, first: 100) {
        userCount
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          ... on User {
            login
            name
            location
            repositories(first: 10) {
              totalCount
              nodes {
                languages(first: 2) {
                  nodes {
                    name
                  }
                }
                name
              }
            }
            followers(first: 10) {
              totalCount
              nodes {
                login
              }
            }
          }
        }
    }
}"""

data = {'query': query.replace('\n', ' ')}
headers = {'Authorization': 'token ' + access_token, 'Content-Type': 'application/json'}
r = requests.post('https://api.github.com/graphql', headers=headers, json=data)
print(json.loads(r.text))

Problem

Hi I'm trying to get data from the Github API for users, the languages they programme in, their repos and the followers/follows they are connected with and their number. I've read through the documentation but haven't found anything specific to the query that I need. Currently, I've used this query to call `https://api.github.com/search/users?q=location:uk&sort=stars&order=desc&page=1&per_page=100` However, this returns the account name, url and other things that aren't relevant to what I'm trying to achieve. I'm analysing this data with json and python requests on Jupyter notebook. Can anyone share their input, thanks.

Original source