gpt4 book ai didi

github - 如何获得所有超过 20 颗星的公共(public) GitHub 存储库的列表?

转载 作者:行者123 更新时间:2023-12-01 03:08:29 25 4
gpt4 key购买 nike

我想获得一个包含超过一定星数(比如 15 或 20)的所有公共(public) GitHub 存储库的列表。我可以使用 GitHub GraphQL API 来获取超过 15 颗星的存储库列表:

query {
search(query: "is:public stars:>15", type: REPOSITORY, first:10) {
repositoryCount
edges {
node {
... on Repository {
nameWithOwner
stargazers {
totalCount
}
}
}
}
}
}

结果如下所示:
{
"data": {
"search": {
"repositoryCount": 704279,
"edges": [
{ "node": { "nameWithOwner": "freeCodeCamp/freeCodeCamp", "stargazers": { "totalCount": 308427 } } },
{ "node": { "nameWithOwner": "996icu/996.ICU", "stargazers": { "totalCount": 249062 } } },
{ "node": { "nameWithOwner": "vuejs/vue", "stargazers": { "totalCount": 156364 } } },
{ "node": { "nameWithOwner": "facebook/react", "stargazers": { "totalCount": 143121 } } },
{ "node": { "nameWithOwner": "tensorflow/tensorflow", "stargazers": { "totalCount": 140562 } } },
{ "node": { "nameWithOwner": "twbs/bootstrap", "stargazers": { "totalCount": 138369 } } },
{ "node": { "nameWithOwner": "EbookFoundation/free-programming-books", "stargazers": { "totalCount": 136421 } } },
{ "node": { "nameWithOwner": "sindresorhus/awesome", "stargazers": { "totalCount": 125160 } } },
{ "node": { "nameWithOwner": "getify/You-Dont-Know-JS", "stargazers": { "totalCount": 115851 } } },
{ "node": { "nameWithOwner": "ohmyzsh/ohmyzsh", "stargazers": { "totalCount": 102749 } } }
]
}
}
}

有 704,279 个存储库,但我最多可以请求 100 个存储库/查询,并使用游标逐步查看结果。因此,如果有足够的时间,这似乎会奏效。但不幸的是,GitHub GraphQL API limits you to the first 1,000 results任何查询,所以这不会做。

我可以使用星号范围( stars:1000..1500 )运行多个查询,但是一旦你以更少的星力获得 repo (有超过 1,000 个带有 exactly 123 stars 的 repo ),这就会崩溃。

我可以通过更多方式分解查询(例如,按创建 repo 的日期),但这开始变得疯狂。有没有更简单的方法来获取 15 星或更多星的公共(public) GitHub 存储库的完整列表?

最佳答案

按创建日期和星级范围进行拆分(问题中提到的“疯狂”解决方案)在实践中效果很好。

您可以使用这样的 GraphQL 查询来获取在给定日期范围内创建的具有 15-20 星的 repos 计数:

query {
search(query: "is:public stars:15..20 created:2016-01-01..2016-01-09", type: REPOSITORY, first: 1) {
repositoryCount
}
}

回复:
{ "data": { "search": { "repositoryCount": 534 } } }

对于给定的星级范围(例如 15–20),您从一个较长的日期范围(例如 2007–2020)开始并获取结果计数。如果超过 1,000,则将日期范围一分为二,并分别获取结果计数。继续递归拆分,直到每个星级范围/日期间隔低于 1,000 个结果。

这是执行此操作的代码:

def split_interval(a, b):
d = int((b - a) / 2)
return [(a, a + d), (a + d + 1, b)]

def split_by_days(stars, day_start, day_end):
start_fmt = day_start.strftime('%Y-%m-%d')
end_fmt = day_end.strftime('%Y-%m-%d')
q = f'stars:{stars} created:{start_fmt}..{end_fmt}')
c = get_count(q)
if c <= 1000:
query_repos(q, out_file)
else:
days = (day_end - day_start).days
if days == 0:
raise ValueError(f'Can\'t split any more: {stars} / {day_start} .. {day_end}')
for a, b in split_interval(0, days):
dt_a = day_start + timedelta(days=a)
dt_b = day_start + timedelta(days=b)
split_by_days(stars, dt_a, dt_b)

ranges = [
(15, 20), (21, 25), (26, 30),
# ...
(1001, 1500), (1501, 5000), (5001, 1_000_000)
]
for a, b in ranges:
stars = f'{a}..{b}'
split_by_days(stars, datetime(2007, 1, 1), datetime(2020, 2, 2))

最好从低星级范围向上爬,因为在爬取过程中,repos 更有可能获得星级而不是失去它们。

对我来说,这最终需要 1,102 次不同的搜索。这是 CSV file (~50MB) 使用这种方法收集了所有在 2020 年 2 月 3 日获得超过 15 颗星的存储库。见 this blog postaccompanying source code更多细节。

关于github - 如何获得所有超过 20 颗星的公共(public) GitHub 存储库的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60022429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com