#!/usr/bin/env python3
"""Download the language-switcher flag icons into images/flags/svg/."""
import urllib.request
from pathlib import Path

LANGS = ["ar", "zh-TW", "en", "fr", "de", "hi", "it", "ja", "no", "ru", "es"]
SRC = "https://sarunibasecamp.com/wp-content/plugins/gtranslate/flags/svg/{}.svg"
UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
      "(KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")

here = Path(__file__).resolve().parent
root = here.parent if here.name == "_source" else here
out = root / "images" / "flags" / "svg"
out.mkdir(parents=True, exist_ok=True)
for lang in LANGS:
    url = SRC.format(lang)
    try:
        req = urllib.request.Request(url, headers={"User-Agent": UA})
        with urllib.request.urlopen(req, timeout=30) as r:
            (out / f"{lang}.svg").write_bytes(r.read())
        print("ok  ", lang)
    except Exception as e:
        print("FAIL", lang, e)
print("Saved to", out)
