Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / scripts / shop / generate_merchant_links.py

generate_merchant_links.py77 lines · 2.9 KiBRaw
1#!/usr/bin/env python3
2"""Regenerate the Shop app's merchant link intent-filter from the live directory.
3
4Android only lets an app become the default handler for web links whose hosts are
5declared in the manifest. Merchant sites can't host our assetlinks.json, so the
6filter is unverified and users approve it once in "Open by default".
7
8Rerun before each release: python3 scripts/shop/generate_merchant_links.py
9"""
10
11import json
12import pathlib
13import re
14import sys
15import urllib.request
16
17DIRECTORY_URL = "https://api.maproj.org/shop/v1/merchants"
18MANIFEST = pathlib.Path(__file__).resolve().parents[2] / "shop/src/main/AndroidManifest.xml"
19BEGIN = "<!-- BEGIN MERCHANT LINKS (generated by scripts/shop/generate_merchant_links.py) -->"
20END = "<!-- END MERCHANT LINKS -->"
21HOST = re.compile(r"^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$")
22INDENT = " " * 12
23
24
25def fetch_domains() -> list[str]:
26 request = urllib.request.Request(
27 DIRECTORY_URL, headers={"Accept": "application/json", "User-Agent": "ModernApps-build/1.0"}
28 )
29 with urllib.request.urlopen(request, timeout=60) as response:
30 merchants = json.load(response)["merchants"]
31 hosts = set()
32 for merchant in merchants:
33 # Only the curated UCPList entries ("live"/"announced"); the ~10k directly verified
34 # seed stores ("verified") would make Android's open-by-default list unusable.
35 if merchant.get("directoryStatus") == "verified":
36 continue
37 domain = str(merchant.get("domain", "")).strip().lower()
38 if not HOST.match(domain):
39 continue
40 bare = domain.removeprefix("www.")
41 hosts.update({bare, f"www.{bare}"})
42 return sorted(hosts)
43
44
45def render(hosts: list[str]) -> str:
46 lines = [
47 BEGIN,
48 '<intent-filter android:autoVerify="false" tools:ignore="AppLinkUrlError,AppLinkWarning">',
49 ' <action android:name="android.intent.action.VIEW" />',
50 ' <category android:name="android.intent.category.DEFAULT" />',
51 ' <category android:name="android.intent.category.BROWSABLE" />',
52 ' <data android:scheme="https" />',
53 ' <data android:scheme="http" />',
54 *[f' <data android:host="{host}" />' for host in hosts],
55 "</intent-filter>",
56 END,
57 ]
58 return "\n".join(INDENT + line for line in lines)
59
60
61def main() -> int:
62 hosts = fetch_domains()
63 if not hosts:
64 print("Directory returned no usable domains; manifest left unchanged.", file=sys.stderr)
65 return 1
66 text = MANIFEST.read_text()
67 start, end = text.find(INDENT + BEGIN), text.find(END)
68 if start < 0 or end < 0:
69 print(f"Markers not found in {MANIFEST}", file=sys.stderr)
70 return 1
71 MANIFEST.write_text(text[:start] + render(hosts) + text[end + len(END):])
72 print(f"Wrote {len(hosts)} hosts to {MANIFEST}")
73 return 0
74
75
76if __name__ == "__main__":
77 sys.exit(main())