Github API call for user accounts

半腔热情 提交于 2020-06-25 17:24:13

问题


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.


回答1:


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))


来源:https://stackoverflow.com/questions/49275824/github-api-call-for-user-accounts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!