iuna

iuna

iuna - experimental mainnet-candidate protocol
git clone https://getiuna.org/git/iuna.git
Log | Files | Refs | README | LICENSE

inject_cloudflare_analytics.py (1654B)


      1 #!/usr/bin/env python3
      2 import re
      3 import sys
      4 from pathlib import Path
      5 
      6 
      7 ANALYTICS = """<!-- Cloudflare Web Analytics --><script type='module' src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "a0d6dbbe9b7846f59f24fd2052322cda"}'></script><!-- End Cloudflare Web Analytics -->"""
      8 BODY_CLOSE_RE = re.compile(r"</body\s*>", re.IGNORECASE)
      9 
     10 
     11 def inject_analytics(html: str) -> str:
     12     if ANALYTICS in html:
     13         return html
     14 
     15     matches = list(BODY_CLOSE_RE.finditer(html))
     16     if not matches:
     17         raise ValueError("HTML document has no closing body tag")
     18 
     19     closing_body = matches[-1]
     20     return html[: closing_body.start()] + ANALYTICS + "\n" + html[closing_body.start() :]
     21 
     22 
     23 def main() -> int:
     24     if len(sys.argv) != 2:
     25         print(f"Usage: {sys.argv[0]} <site-root>", file=sys.stderr)
     26         return 2
     27 
     28     site_root = Path(sys.argv[1])
     29     if not site_root.is_dir():
     30         print(f"error: site root does not exist: {site_root}", file=sys.stderr)
     31         return 1
     32 
     33     html_paths = sorted(site_root.rglob("*.html"))
     34     if not html_paths:
     35         print(f"error: no HTML pages found below {site_root}", file=sys.stderr)
     36         return 1
     37 
     38     injected = 0
     39     for path in html_paths:
     40         html = path.read_text(encoding="utf-8")
     41         updated_html = inject_analytics(html)
     42         if updated_html == html:
     43             continue
     44         path.write_text(updated_html, encoding="utf-8")
     45         injected += 1
     46 
     47     print(
     48         f"Cloudflare Web Analytics present on {len(html_paths)} HTML pages "
     49         f"({injected} injected)"
     50     )
     51     return 0
     52 
     53 
     54 if __name__ == "__main__":
     55     raise SystemExit(main())