Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github API call for user accounts

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.

like image 419
snow_fall Avatar asked Oct 17 '22 21:10

snow_fall


1 Answers

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))
like image 159
Bertrand Martel Avatar answered Oct 19 '22 23:10

Bertrand Martel