使用关键字从组织获取所有回购

Get all repos from an org with a keyword

我正在尝试找出 Github API to get all repos from a particular organization that has one search term as a criteria. I can't seem to find the right combination. The Github API docs don't show how to combine your search. I have 2 problems so far. One, is I can't get a full list to show. Two, I don't know how to search within an org for all repos with a specific keyword. Like if I wanted to search in the org Azure 并找到所有在回购名称或描述中包含“认知”一词的回购(默认搜索回购名称和描述,因此无需指定那些).我该怎么做?

这是我目前拥有的:

import requests   

org = 'Azure'
response = requests.get(f'https://api.github.com/orgs/{org}/repos')
print(response.json())

但是当我 运行 那个时,它只有 returns 大约 30 个 repos。我猜它不会全部显示它们(因为有数千个)但是如果我添加一个搜索条件,“认知”这个词,这应该会减少很多回购的数量,所以我到目前为止的代码可能只是缺少搜索条件关键字。但这只是猜测。

这是因为 Github 默认情况下限制 30 个项目。如果您想要更多,请考虑分页。

这里有更多内容。 Pagination.

有关搜索查询,请查看 Constructing a search query

还有Searching on GitHub.

看起来组合不同的搜索限定符可以达到目的。此代码在 Github 上搜索组织,因此我只返回在回购名称或描述中包含“认知”一词的回购结果。

下面是工作代码,您只需将 {org}org 替换为您选择的 Github 组织。该代码获取 JSON 结果的 URL(也称为 html_url),然后打印 URL。取消注释 json.dumps() 函数以获得完整响应。
注意:cognitive in:name,description 中的 space 是可以的。

代码:

import json
import requests

response = requests.get(f"https://api.github.com/search/repositories?q=cognitive in:name,description+org:{org}&per_page=100")
#print(json.dumps(response.json(), indent=2))
    
urls = []
for repo in response.json()['items']:
    urls.append(repo['html_url'])
    
print(len(urls), 'repos in the', org, 'organization.')
for url in urls:
    print(url)

Github API 的页面帮助将其组合在一起:

这显示了存储库搜索的基础知识:
https://developer.github.com/v3/search/#search-repositories

显示您要使用的限定词:
https://help.github.com/en/github/searching-for-information-on-github/searching-for-repositories

正如 Rishav 提到的,分页部分 per_page=100 returns 您需要的正确结果数:
https://developer.github.com/v3/#pagination

获取信息的所有可能端点 API:
https://api.github.com/