python脚本ETH获取最新发行版本并将是否更新信息发送到钉钉

发布于:2025-06-29 ⋅ 阅读:(14) ⋅ 点赞:(0)
import requests
import json
import time
import hmac
import hashlib
import base64
import urllib.parse

# === 1. 配置钉钉机器人 ===
webhook = "https://oapi.dingtalk.com/robot/send?access_token=XXX"
secret = "XXX"  # 如果没有加签验证,请设置为 None

# === 2. GitHub Releases API ===
ETH_GITHUB = "https://github.com/ethereum/go-ethereum/releases/"
GITHUB_API = "https://api.github.com/repos/ethereum/go-ethereum/releases"


def get_local_geth_version(rpc_url="http://127.0.0.1:8545"):
    payload = {
        "jsonrpc": "2.0",
        "method": "web3_clientVersion",
        "params": [],
        "id": 1
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(rpc_url, headers=headers, data=json.dumps(payload))
    result = response.json().get("result")
    return result

def get_latest_mandatory_release():
    try:
        response = requests.get(GITHUB_API, timeout=10)
        releases = response.json()
        for release in releases:
            tag = release.get("tag_name", "").lower()
            body = release.get("body", "").lower()
            html_url = ETH_GITHUB + "tag/" + tag
            return tag, body.strip(), html_url
        return None, None, None
    except Exception as e:
        return None, f"????: {e}", None


def gen_sign(secret):
    timestamp = str(round(time.time() * 1000))
    string_to_sign = f'{timestamp}\n{secret}'
    hmac_code = hmac.new(secret.encode(), string_to_sign.encode(), hashlib.sha256).digest()
    sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
    return timestamp, sign

def send_to_dingtalk(title, content, link):
    headers = {"Content-Type": "application/json"}
    message = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": f"{title}\n\n{content}\n\n[查看详情]({link})"
        }
    }

    url = webhook
    if secret:
        timestamp, sign = gen_sign(secret)
        url += f"&timestamp={timestamp}&sign={sign}"

    resp = requests.post(url, headers=headers, data=json.dumps(message))
    if resp.status_code != 200:
        print(f"发送失败: {resp.status_code}, {resp.text}")
    else:
        print("✅ 已发送钉钉通知")

if __name__ == "__main__":
    version, note, url = get_latest_mandatory_release()
    localversion = get_local_geth_version(rpc_url="http://127.0.0.1:8545")
    if version and note:
        if version in localversion:
            send_to_dingtalk(
                title=f"ETH 最新升级版本 {version}",
                content="本地版本和最新强更版本一致,无需迭代",
                link=url
            )
        else:
            send_to_dingtalk(
                title=f"ETH 发布最新升级版本 {version}",
                content=note[:500],  # 钉钉限制最大消息体
                link=url
            )
    else:
        print("未发现最新升级版本或请求失败")

编写定时任务

59 23 * * 1 /usr/bin/python3 /root/geth_update.py

没有最新版本的消息文本
在这里插入图片描述
有最新版本的消息
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到