gogs_create.py 1.5 KB

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