本帖最后由 360NENZ 于 2024-7-22 22:32 编辑
- 添加了检查TOKEN是否存在
- 添加了检查用户是否存在
- 可以查看指定用户的Star
代码如下
[Python] 纯文本查看 复制代码
import requests
import os
# 检查并获取Github访问令牌
GITHUB_TOKEN_CHECK = os.getenv('GITHUB_TOKEN')
if GITHUB_TOKEN_CHECK is None:
print("GitHub Token不存在!请在下面输入")
GITHUB_TOKEN = input("输入你的GitHub Token:")
else:
print(f"GitHub Token存在!为:\n{GITHUB_TOKEN_CHECK}")
GITHUB_TOKEN = GITHUB_TOKEN_CHECK
headers = {'Authorization': f'token {GITHUB_TOKEN}'}
def get_starred_repos(username):
url = f'https://api.github.com/users/{username}/starred'
response = requests.get(url, headers=headers)
# 判断回复状态
if response.status_code == 200:
return response.json()
elif response.status_code == 404:
return 404
elif response.status_code == 403:
return 403
else:
return []
def main():
username = input("输入GiHub用户名:")
starred_repos = get_starred_repos(username)
if get_starred_repos(username) == 404:
print("用户不存在!")
elif get_starred_repos(username) == 403:
print("访问被拒绝!可能是超过限制\n官方文档:https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting")
else:
print("用户" + username + " Star过的仓库如下:")
for repo in starred_repos:
print(f"项目名称: {repo['name']}")
print(f"项目描述: {repo['description']}")
print(f"项目地址: {repo['html_url']}")
print(f"Star数量: {repo['stargazers_count']}")
print('---')
if __name__ == '__main__':
main()
|