你知唔知我系靓仔

监控github仓库更新

需要监控一些运行库的关键更新,重复造了个轮子,偷懒使用qmsg推送

import time
import requests
from github import Github

def send_qmsg(token, group_key, message):
    api_url = f"https://qmsg.zendee.cn/group/{group_key}"
    params = {
        "msg": message
    }

    response = requests.get(api_url, params=params)
    if response.status_code == 200:
        print("Message sent successfully!")
    else:
        print(f"Failed to send message. Status code: {response.status_code}")

def get_latest_commit_sha(repo):
    # 获取最新的 commit sha
    return repo.get_commits()[0].sha

def monitor_repositories(token, repo_urls, qmsg_token, qmsg_group_key):
    # 使用 GitHub Token 进行身份验证
    g = Github(token)

    # 初始化每个仓库的最新 commit sha
    latest_commit_shas = {}

    for repo_url in repo_urls:
        try:
            # 获取仓库信息
            repo = g.get_repo(repo_url)

            # 记录当前时刻的最新 commit sha
            latest_commit_shas[repo_url] = get_latest_commit_sha(repo)
        except Exception as e:
            print(f"Error: {e}")

    while True:
        for repo_url in repo_urls:
            try:
                # 获取仓库信息
                repo = g.get_repo(repo_url)

                # 获取最新的 commit
                latest_commit = repo.get_commits()[0]

                # 检查是否有新的 commit
                if latest_commit.sha != latest_commit_shas[repo_url]:
                    message = f"订阅的仓库 {repo_url} 有修改: {latest_commit.commit.message}"
                    print(message)

                    # 发送消息到 qmsg
                    send_qmsg(qmsg_token, qmsg_group_key, message)

                    latest_commit_shas[repo_url] = latest_commit.sha
            except Exception as e:
                print(f"Error: {e}")

        # 等待5秒再次检查
        time.sleep(5)

if __name__ == "__main__":
    github_token = "ghp_U*pm4"
    repository_urls = ["username/main1", "username/main2", "username/main3"]  # 添加更多仓库地址
    qmsg_token = "qq"  # 或者使用 "msg",根据你的需求选择
    qmsg_group_key = "c2575a7*c89d6f"

    monitor_repositories(github_token, repository_urls, qmsg_token, qmsg_group_key)

#github #python #check