#!/usr/bin/env python3 """Regenerate the Shop app's merchant link intent-filter from the live directory. Android only lets an app become the default handler for web links whose hosts are declared in the manifest. Merchant sites can't host our assetlinks.json, so the filter is unverified and users approve it once in "Open by default". Rerun before each release: python3 scripts/shop/generate_merchant_links.py """ import json import pathlib import re import sys import urllib.request DIRECTORY_URL = "https://api.maproj.org/shop/v1/merchants" MANIFEST = pathlib.Path(__file__).resolve().parents[2] / "shop/src/main/AndroidManifest.xml" BEGIN = "" END = "" HOST = re.compile(r"^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$") INDENT = " " * 12 def fetch_domains() -> list[str]: request = urllib.request.Request( DIRECTORY_URL, headers={"Accept": "application/json", "User-Agent": "ModernApps-build/1.0"} ) with urllib.request.urlopen(request, timeout=60) as response: merchants = json.load(response)["merchants"] hosts = set() for merchant in merchants: # Only the curated UCPList entries ("live"/"announced"); the ~10k directly verified # seed stores ("verified") would make Android's open-by-default list unusable. if merchant.get("directoryStatus") == "verified": continue domain = str(merchant.get("domain", "")).strip().lower() if not HOST.match(domain): continue bare = domain.removeprefix("www.") hosts.update({bare, f"www.{bare}"}) return sorted(hosts) def render(hosts: list[str]) -> str: lines = [ BEGIN, '', ' ', ' ', ' ', ' ', ' ', *[f' ' for host in hosts], "", END, ] return "\n".join(INDENT + line for line in lines) def main() -> int: hosts = fetch_domains() if not hosts: print("Directory returned no usable domains; manifest left unchanged.", file=sys.stderr) return 1 text = MANIFEST.read_text() start, end = text.find(INDENT + BEGIN), text.find(END) if start < 0 or end < 0: print(f"Markers not found in {MANIFEST}", file=sys.stderr) return 1 MANIFEST.write_text(text[:start] + render(hosts) + text[end + len(END):]) print(f"Wrote {len(hosts)} hosts to {MANIFEST}") return 0 if __name__ == "__main__": sys.exit(main())