#!/usr/bin/env python3 """Regenerate the Shop app's merchant link intent-filter from the upstream 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". Source: the server's own upstream merchant directory (UCPList) — the same feed the server merges for search-time merchant matching. It returns a bare JSON array of listing objects; the merchant host is derived from each listing's `ucpEndpoint` (falling back to `url`), and `ucpStatus` names its standing. Rerun before each release: python3 scripts/shop/generate_merchant_links.py """ import json import pathlib import re import sys import urllib.parse import urllib.request DIRECTORY_URL = "https://ucplist.ai/api/listings?category=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: listings = json.load(response) if isinstance(listings, dict): listings = listings.get("merchants", []) hosts = set() for merchant in listings: if not isinstance(merchant, dict): continue # 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("ucpStatus") == "verified": continue endpoint = str(merchant.get("ucpEndpoint") or merchant.get("url") or "") domain = urllib.parse.urlparse(endpoint).hostname or "" domain = 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())