用户帐户的 Github API 调用 [英] Github API call for user accounts

查看:31
本文介绍了用户帐户的 Github API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试从 Github API 获取用户的数据、他们使用的编程语言、他们的存储库以及他们所连接的关注者/关注者及其数量.

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.

目前,我使用这个查询来调用 https://api.github.com/search/users?q=location:uk&sort=stars&order=desc&page=1&per_page=100

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

但是,这会返回帐户名称、网址和其他与我要实现的目标无关的内容.我正在 Jupyter 笔记本上使用 json 和 python 请求分析这些数据.

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.

推荐答案

您可以使用 GraphQL Api v4 能够从用户那里请求您想要的特定信息.在以下查询中,您使用 location:uk & 搜索用户提取他们的登录名、姓名、关注者、关注者数量、存储库、存储库数量、语言等...

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

在资源管理器中尝试

对于分页,使用 first: 100 请求前 100 个项目 &使用after:<cursor_value>请求下一页,光标值是上一页的最后一个光标,例如上次查询中pageInfo.endCursor的值.

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.

在 Python 中,这将是:

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

这篇关于用户帐户的 Github API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆