Skip to content

Instantly share code, notes, and snippets.

@14790897
Created January 5, 2025 07:15
Show Gist options
  • Save 14790897/a008379c7dc9ada3e31cd5dc50749c91 to your computer and use it in GitHub Desktop.
Save 14790897/a008379c7dc9ada3e31cd5dc50749c91 to your computer and use it in GitHub Desktop.
判断是否给某个仓库做过贡献(适用于贡献人数较多的仓库)
import requests
# 仓库的所有者和名称
repo_owner = "freqtrade"
repo_name = "freqtrade"
# 你的 GitHub 用户名
your_username = "14790897"
# GitHub API 的 URL
base_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/contributors"
# 分页获取所有贡献者
page = 1
found = False # 标志是否找到你的用户名
while True:
# 为每一页添加参数
url = f"{base_url}?page={page}&per_page=30"
response = requests.get(url)
# 检查请求是否成功
if response.status_code != 200:
print(f"无法获取贡献者列表,错误代码:{response.status_code}")
break
# 解析当前页的贡献者
contributors = response.json()
# 如果当前页为空,说明没有更多页
if not contributors:
break
# 遍历当前页的贡献者
for contributor in contributors:
print(f"Username: {contributor['login']}, Contributions: {contributor['contributions']}")
if contributor["login"] == your_username:
found = True
print(f"你是该仓库的贡献者!贡献次数:{contributor['contributions']}")
break
if found:
break
# 查看下一页
page += 1
# 如果遍历所有页仍未找到
if not found:
print("你不是该仓库的贡献者。")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment