ES/restAPI.py

121 lines
4.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import requests
import base64
def get_repo_info(username, repo_name, access_token):
headers = {"Authorization": f"token {access_token}"}
base_url = f"https://api.github.com/repos/{username}/{repo_name}"
# 获取仓库基本信息
repo_response = requests.get(base_url, headers=headers) #requests.get函数发送GET请求headers参数将个人访问令牌作为身份验证提供给API。
repo_response.raise_for_status() #raise_for_status()检查请求是否成功,如果请求失败,将引起异常。
repo_info = repo_response.json()#repo_response.json()将HTTP响应转换为JSON格式并将仓库信息存储在repo_info变量中。
# 获取仓库的 commits 列表
commits_url = f"{base_url}/stats/commit_activity"
commits_response = requests.get(commits_url, headers=headers)
commits_response.raise_for_status()
commits = commits_response.json()
# 获取仓库的拉取请求Pull Requests列表
pulls_url = f"{base_url}/pulls"
pulls_response = requests.get(pulls_url, headers=headers)
pulls_response.raise_for_status()
pull_requests = pulls_response.json()
# 获取仓库的问题Issues列表
issues_url = f"{base_url}/issues"
issues_response = requests.get(issues_url, headers=headers)
issues_response.raise_for_status()
issues = issues_response.json()
# 获取仓库的文档star列表
doc_url = f"{base_url}/community/profile"
doc_response = requests.get(doc_url, headers=headers)
doc_response.raise_for_status()
doc = doc_response.json()
#获取contents内容
content_url = f"{base_url}/contents/"
content_response = requests.get(content_url,headers=headers)
content_response.raise_for_status()
content = content_response.json()
return {
"repo_info": repo_info,
"commits": commits,
"pull_requests": pull_requests,
"issues": issues,
"doc": doc,
"content": content
}
target_projects = [
{"username": "dmlc", "repo_name": "xgboost"},
{"username": "microsoft", "repo_name": "LightGBM"},
{"username": "liudragonfly", "repo_name": "GBDT"},
]
access_token = "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"
for project in target_projects:
target_username = project["username"]
target_repo_name = project["repo_name"]
repo_data = get_repo_info(target_username, target_repo_name, access_token)
# 打印仓库基本信息
print("Repository Info:")
print(f"Name: {repo_data['repo_info']['name']}")
print(f"Description: {repo_data['repo_info']['description']}")
print(f"URL: {repo_data['repo_info']['html_url']}")
print("---")
# 打印 Commits 列表
print("Commits:")
for commit in repo_data['commits']:
print(f"day: {commit['days']}")
print(f"total: {commit['total']}")
print(f"week: {commit['week']}") # week的值代表的是时间戳
print("---")
# 打印拉取请求Pull Requests列表
print("Pull Requests:")
for pr in repo_data['pull_requests']:
print(f"Title: {pr['title']}")
print(f"Author: {pr['user']['login']}")
print(f"URL: {pr['html_url']}")
print("---")
# 打印问题Issues列表
print("Issues:")
for issue in repo_data['issues']:
print(f"Title: {issue['title']}")
print(f"Author: {issue['user']['login']}")
print(f"URL: {issue['html_url']}")
print("---")
#打印标星(star)列表
print("doc:")
print(f"documentation:{not bool(repo_data['doc']['documentation'])}")
print(f"code_of_conduct: {repo_data['doc']['files']['code_of_conduct'] is not None}")
#打印.gitignore文件信息
gitignore_exists = any(file_info['name'] == '.gitignore' for file_info in repo_data['content'])
if gitignore_exists:
git_url = f"https://api.github.com/repos/{target_username}/{target_repo_name}/contents/.gitignore"
headers = {"Authorization": f"token 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"}
git_response = requests.get(git_url,headers=headers)
git = git_response.json()
print(f"项目{target_repo_name}的.gitignore文件信息")
encoded_data = git['content']
encoded_data = encoded_data.replace("\n", "").replace(" ", "")
decoded_data = base64.b64decode(encoded_data).decode("utf-8")
print(decoded_data)
else:
print(".gitignore file does not exist in the repository.")
# 打印项目之间的分隔线
print("===================")