# -*- coding: utf-8 -*- r"""改 Gitee 仓库的简介 / 主页,并核对发行版。 用法(令牌只走命令行,不落盘): python _build/gitee_meta.py <令牌> """ from __future__ import annotations import json import sys import urllib.error import urllib.request OWNER, REPO = "xmz7891", "ykxmz" API = f"https://gitee.com/api/v5/repos/{OWNER}/{REPO}" DESCRIPTION = ( "教室电脑手表遥控 · 老师用手表管教室电脑:放课件、投图、" "屏蔽游戏网站、下课关机。支持局域网直连与跨网络中继" ) HOMEPAGE = "https://relay.xxmxxs.top" def call(url, payload=None, method="GET", timeout=60): data = None headers = {"User-Agent": "ykxmz-meta"} if payload is not None: data = json.dumps(payload).encode("utf-8") headers["Content-Type"] = "application/json;charset=UTF-8" req = urllib.request.Request(url, data=data, headers=headers, method=method) with urllib.request.urlopen(req, timeout=timeout) as resp: body = resp.read().decode("utf-8", "replace") return resp.status, (json.loads(body) if body.strip() else {}) def main() -> int: if len(sys.argv) < 2: print(" 用法: python _build/gitee_meta.py <令牌>") return 1 token = sys.argv[1] print("===== 1. 改仓库简介 =====") try: status, reply = call( API, {"access_token": token, "name": REPO, "description": DESCRIPTION, "homepage": HOMEPAGE}, method="PATCH") if status in (200, 201): print(f" ✓ 简介已更新") print(f" {reply.get('description', '')}") print(f" 主页: {reply.get('homepage', '')}") else: print(f" ✗ HTTP {status}: {reply}") except urllib.error.HTTPError as exc: detail = exc.read().decode("utf-8", "replace")[:300] print(f" ✗ HTTP {exc.code}: {detail}") return 1 print() print("===== 2. 核对发行版 =====") try: _s, releases = call(f"{API}/releases?access_token={token}&per_page=5") if not isinstance(releases, list): print(f" 读不到发行版列表: {releases}") for rel in releases[:3]: print(f" {rel.get('tag_name')} {rel.get('name')}") for asset in rel.get("assets", []): size = asset.get("size", 0) / 1024 / 1024 print(f" {asset.get('browser_download_url', '')}" .replace("https://gitee.com", "")) print(f" 大小 {size:.2f} MB " f"下载次数 {asset.get('download_count', 0)}") except Exception as exc: # noqa: BLE001 print(f" 查发行版失败: {exc}") print() print(f" 仓库: https://gitee.com/{OWNER}/{REPO}") print(f" 发行版: https://gitee.com/{OWNER}/{REPO}/releases") return 0 if __name__ == "__main__": sys.exit(main())