Ubuntu Python与GitHub API 交互,获取仓库更新信息

1. 获取 GitHub 个人访问令牌

  1. 登录 GitHub ,首先使用帐户登录 GitHub

  2. 在 GitHub 页面右上角点击头像,然后选择 “Settings”
    Ubuntu Python与GitHub API 交互,获取仓库更新信息_第1张图片

  3. 在左侧菜单栏滚动到最下方,找到并点击 “Developer settings”
    Ubuntu Python与GitHub API 交互,获取仓库更新信息_第2张图片

  4. 在 “Developer settings” 页面中,点击 “Personal access tokens”,然后选择 “Tokens (classic)”
    Ubuntu Python与GitHub API 交互,获取仓库更新信息_第3张图片

  5. 点击 “Generate new token” 按钮,显示生成令牌的表单,按照提示设置:

  • Note : 给令牌一个描述性的名称,比如 “Sentinel Access”
  • Expiration:选择令牌的过期时间(例如 30 天、60 天、或 No expiration,没有到期时间)
  • Scopes: 选择令牌的权限范围。
    需要读取仓库信息,勾选 repo 下的 repo:statusrepo_deploymentpublic_reporepo:invite
    需要对仓库内容进行写操作(例如推送代码),还需选择 repo:write
  1. 置完毕后,点击页面底部的 “Generate token” 按钮,GitHub 将生成一个新的个人访问令牌
  2. 写入 ~/.bashrc 文件
export GITHUB_KEY=ghp_xxxx

查看

$ echo $GITHUB_KEY
ghp_xxxx

2. Python与GitHub API 交互

代码:

import requests
import os


class GitHubClient:
    def __init__(self, token):
        self.token = token

    def fetch_updates(self, subscriptions):
        headers = {
            'Authorization': f'token {self.token}'
        }
        updates = {}
        for repo in subscriptions:
            response = requests.get(f'https://api.github.com/repos/{repo}/events', headers=headers)
            if response.status_code == 200:
                updates[repo] = response.json()
        return updates


if __name__ == "__main__":
    github_client = GitHubClient(os.getenv("GITHUB_KEY"))
    updates = github_client.fetch_updates(["huggingface/transformers", "tensorflow/tensorflow","apache/spark"])
    #print(updates)
    # print("**"*400)
    report = ""
    for repo, events in updates.items():

        print(events)
        report += f"Repository: {repo}\n"
        for event in events:
            report += f"- {event['type']} id : {event['id']} at {event['created_at']}\n"
    print(report)

报错:

    raise ValueError(f"Unable to determine SOCKS version from {proxy_url}")
ValueError: Unable to determine SOCKS version from socks://127.0.0.1:1080/

修改后:

import os
import httpx


# 设置代理服务器
proxy_url = 'socks5://127.0.0.1:1080'

# 创建一个 HTTPX 客户端并配置代理
client = httpx.Client(proxies={
    "http://": proxy_url,
    "https://": proxy_url,
})

class GitHubClient:
    def __init__(self, token):
        self.token = token

    def fetch_updates(self, subscriptions):
        headers = {
            'Authorization': f'token {self.token}'
        }
        updates = {}
        for repo in subscriptions:
            response = client.get(f'https://api.github.com/repos/{repo}/events', headers=headers)
            if response.status_code == 200:
                updates[repo] = response.json()
        return updates


if __name__ == "__main__":

    github_client = GitHubClient(os.getenv("GITHUB_KEY"))
    updates = github_client.fetch_updates(["huggingface/transformers", "tensorflow/tensorflow"])

    report = ""
    for repo, events in updates.items():
        report += f"Repository: {repo}\n"
        for event in events:
            report += f"- {event['type']} id : {event['id']} at {event['created_at']}\n"
    print(report)

运行结果:

Repository: huggingface/transformers
- IssueCommentEvent id : 41653179474 at 2024-09-05T11:25:30Z
- WatchEvent id : 41653123066 at 2024-09-05T11:23:39Z
- IssueCommentEvent id : 41653105714 at 2024-09-05T11:23:05Z
- PullRequestReviewCommentEvent id : 41653061082 at 2024-09-05T11:21:35Z
- PullRequestReviewEvent id : 41653060997 at 2024-09-05T11:21:36Z
- WatchEvent id : 41652733799 at 2024-09-05T11:11:12Z
- IssueCommentEvent id : 41652632676 at 2024-09-05T11:07:58Z
- PullRequestReviewCommentEvent id : 41652553166 at 2024-09-05T11:05:23Z
- PullRequestReviewEvent id : 41652553115 at 2024-09-05T11:05:24Z
- PullRequestReviewCommentEvent id : 41652525289 at 2024-09-05T11:04:31Z
- PullRequestReviewEvent id : 41652525183 at 2024-09-05T11:04:32Z
- PullRequestReviewEvent id : 41652515178 at 2024-09-05T11:04:13Z
- PullRequestReviewCommentEvent id : 41652515280 at 2024-09-05T11:04:12Z
- PullRequestReviewCommentEvent id : 41652494849 at 2024-09-05T11:03:32Z
- WatchEvent id : 41652495060 at 2024-09-05T11:03:34Z
- PullRequestReviewEvent id : 41652494752 at 2024-09-05T11:03:33Z
- WatchEvent id : 41652344813 at 2024-09-05T10:58:54Z
- IssueCommentEvent id : 41652202609 at 2024-09-05T10:54:31Z
- PullRequestReviewCommentEvent id : 41652140247 at 2024-09-05T10:47:41Z
- PullRequestReviewCommentEvent id : 41652140269 at 2024-09-05T10:50:50Z
- PullRequestReviewCommentEvent id : 41652140261 at 2024-09-05T10:49:47Z
- PullRequestReviewEvent id : 41652140207 at 2024-09-05T10:52:37Z
- PullRequestReviewEvent id : 41652140156 at 2024-09-05T10:52:37Z
- IssuesEvent id : 41651848008 at 2024-09-05T10:43:52Z
- PushEvent id : 41651737460 at 2024-09-05T10:40:46Z
- PushEvent id : 41651577778 at 2024-09-05T10:36:07Z
- IssueCommentEvent id : 41651380814 at 2024-09-05T10:29:58Z
- PullRequestReviewCommentEvent id : 41651114817 at 2024-09-05T10:22:07Z
- PullRequestReviewEvent id : 41651114714 at 2024-09-05T10:22:08Z
- PushEvent id : 41651089649 at 2024-09-05T10:21:22Z
Repository: tensorflow/tensorflow
- ForkEvent id : 41653000234 at 2024-09-05T11:19:37Z
- IssueCommentEvent id : 41652864055 at 2024-09-05T11:15:19Z
- IssueCommentEvent id : 41652805357 at 2024-09-05T11:13:29Z
- PushEvent id : 41652771430 at 2024-09-05T11:12:25Z
- IssueCommentEvent id : 41652723831 at 2024-09-05T11:10:53Z
- WatchEvent id : 41651729210 at 2024-09-05T10:40:32Z
- IssuesEvent id : 41651693889 at 2024-09-05T10:39:36Z
- CommitCommentEvent id : 41651661031 at 2024-09-05T10:38:38Z
- CommitCommentEvent id : 41651626667 at 2024-09-05T10:37:33Z
- CommitCommentEvent id : 41651144722 at 2024-09-05T10:23:02Z
- CommitCommentEvent id : 41651113295 at 2024-09-05T10:22:05Z
- CommitCommentEvent id : 41651098529 at 2024-09-05T10:21:38Z
- CommitCommentEvent id : 41651062753 at 2024-09-05T10:20:30Z
- PullRequestEvent id : 41650975772 at 2024-09-05T10:17:51Z
- CreateEvent id : 41650974318 at 2024-09-05T10:17:48Z
- WatchEvent id : 41650740807 at 2024-09-05T10:10:44Z
- PushEvent id : 41650462691 at 2024-09-05T10:02:36Z
- PushEvent id : 41650330117 at 2024-09-05T09:58:53Z
- IssueCommentEvent id : 41649922726 at 2024-09-05T09:47:09Z
- WatchEvent id : 41649641425 at 2024-09-05T09:39:04Z
- PullRequestEvent id : 41649496774 at 2024-09-05T09:34:54Z
- DeleteEvent id : 41649496291 at 2024-09-05T09:34:53Z
- PushEvent id : 41649495821 at 2024-09-05T09:34:52Z
- PushEvent id : 41649495399 at 2024-09-05T09:34:52Z
- PushEvent id : 41649313881 at 2024-09-05T09:29:47Z
- PushEvent id : 41649201712 at 2024-09-05T09:26:39Z
- PushEvent id : 41648991757 at 2024-09-05T09:20:44Z
- PushEvent id : 41648777010 at 2024-09-05T09:14:50Z
- IssueCommentEvent id : 41648540862 at 2024-09-05T09:08:13Z
- PushEvent id : 41648203089 at 2024-09-05T08:58:48Z

GitHub API 返回的数据,格式为 JSON,假设有两个事件,输出类似于以下内容:

{
    "huggingface/transformers": [
        {
            "id": "event_id_1",
            "type": "PushEvent",
            "created_at": "2023-09-01T12:34:56Z",
            "actor": {
                "id": 1234567,
                "login": "user123",
                "url": "https://api.github.com/users/user123"
            },
            "repo": {
                "id": 6543210,
                "name": "6master6/GitHubSentinel",
                "url": "https://api.github.com/repos/6master6/GitHubSentinel"
            },
            "payload": {
                // ...其他相关信息
            }
        },
        {
            "id": "event_id_2",
            "type": "IssuesEvent",
            "created_at": "2023-08-31T10:11:22Z",
            "actor": {
                "id": 2345678,
                "login": "user456",
                "url": "https://api.github.com/users/user456"
            },
            "repo": {
                "id": 6543210,
                "name": "6master6/GitHubSentinel",
                "url": "https://api.github.com/repos/6master6/GitHubSentinel"
            },
            "payload": {
                // ...其他相关信息
            }
        }
    ]
}

你可能感兴趣的:(ubuntu,ubuntu,python,github)