Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-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 | |
| 4 | Android only lets an app become the default handler for web links whose hosts are |
| 5 | declared in the manifest. Merchant sites can't host our assetlinks.json, so the |
| 6 | filter is unverified and users approve it once in "Open by default". |
| 7 | |
| 8 | Rerun before each release: python3 scripts/shop/generate_merchant_links.py |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import pathlib |
| 13 | import re |
| 14 | import sys |
| 15 | import urllib.request |
| 16 | |
| 17 | DIRECTORY_URL = "https://api.maproj.org/shop/v1/merchants" |
| 18 | MANIFEST = pathlib.Path(__file__).resolve().parents[2] / "shop/src/main/AndroidManifest.xml" |
| 19 | BEGIN = "<!-- BEGIN MERCHANT LINKS (generated by scripts/shop/generate_merchant_links.py) -->" |
| 20 | END = "<!-- END MERCHANT LINKS -->" |
| 21 | HOST = re.compile(r"^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$") |
| 22 | INDENT = " " * 12 |
| 23 | |
| 24 | |
| 25 | def 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 | |
| 45 | def 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 | |
| 61 | def 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 | |
| 76 | if __name__ == "__main__": |
| 77 | sys.exit(main()) |