import requests, json, os, zipfile, io, sys

here = r"E:\ios\tools"
os.makedirs(here, exist_ok=True)
r = requests.get("https://api.github.com/repos/2dust/v2rayN/releases/latest", timeout=30)
rel = r.json()
print("tag:", rel.get("tag_name"))
assets = rel.get("assets", [])
for a in assets:
    print("asset:", a["name"], a["size"] // 1048576, "MB")
# pick windows-64 with core
cand = [a for a in assets if "windows-64" in a["name"].lower() and a["name"].lower().endswith(".zip")
        and "desktop" not in a["name"].lower()]
if not cand:
    cand = [a for a in assets if "windows-64" in a["name"].lower()]
if not cand:
    print("NO ASSET"); sys.exit(1)
a = cand[0]
print("downloading:", a["name"])
z = requests.get(a["browser_download_url"], timeout=300)
zp = os.path.join(here, "v2rayn.zip")
with open(zp, "wb") as f:
    f.write(z.content)
print("saved", zp, len(z.content)//1048576, "MB")
