#!/usr/bin/env python3 """通过 Gogs web 表单创建仓库(API 500 的兜底路径)""" import requests, re, sys BASE = "https://git.fmode.cn" USER, PWD = "xadmin", "Xadmin@2026" REPO = sys.argv[1] if len(sys.argv) > 1 else "hardware-lab" DESC = sys.argv[2] if len(sys.argv) > 2 else "Pocket Hardware Lab - 8 AI hardware products" s = requests.Session() s.headers.update({"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/120.0 Safari/537.36"}) # 1. 登录 r = s.get(f"{BASE}/user/login", timeout=30) csrf = re.search(r'name="_csrf" value="([^"]+)"', r.text).group(1) r = s.post(f"{BASE}/user/login", data={"_csrf": csrf, "user_name": USER, "password": PWD}, allow_redirects=False, timeout=30) print(f"login: {r.status_code} -> {r.headers.get('Location','')}") # 2. 取建仓页 CSRF r = s.get(f"{BASE}/repo/create", timeout=30) m = re.search(r'name="_csrf" value="([^"]+)"', r.text) csrf = m.group(1) # 3. 提交 data = { "_csrf": csrf, "user_id": "1", "repo_name": REPO, "description": DESC, "private": "0", "auto_init": "on", "gitignores": "", "license": "", "readme": "Default", } s.headers["Referer"] = f"{BASE}/repo/create" s.headers["Origin"] = BASE r = s.post(f"{BASE}/repo/create", data=data, allow_redirects=False, timeout=30) print(f"create: {r.status_code} -> {r.headers.get('Location','')}") if r.status_code == 200: err = re.findall(r'class="ui (?:red|error) message"[^>]*>(.*?)', r.text, re.S) print("ERRORS:", [re.sub(r'<[^>]+>', '', e).strip() for e in err][:3])