|
10 | 10 | For each source the app calls (data/remote/ExternalLookup.kt), this fetches a |
11 | 11 | sample number and checks the HTTP status and the marker field the Kotlin parser |
12 | 12 | keys on. Exit status is non-zero when any source stops answering in that shape. |
| 13 | +
|
| 14 | +--sample measures what SkipCalls adds: it asks about numbers drawn from the |
| 15 | +published database, from the unpublished community pending pool and from |
| 16 | +well-known business lines, and counts how often SkipCalls names a spam |
| 17 | +category the app acts on and how often that number isn't already in the |
| 18 | +local database. |
13 | 19 | """ |
14 | 20 |
|
15 | 21 | from __future__ import annotations |
16 | 22 |
|
| 23 | +import argparse |
| 24 | +import json |
| 25 | +import random |
17 | 26 | import re |
18 | 27 | import sys |
| 28 | +import time |
19 | 29 | import urllib.error |
20 | 30 | import urllib.request |
21 | | -from collections.abc import Callable |
| 31 | +from collections.abc import Callable, Iterable |
22 | 32 | from dataclasses import dataclass |
23 | 33 | from pathlib import Path |
24 | 34 |
|
25 | 35 | ROOT = Path(__file__).resolve().parent.parent |
26 | 36 | EXTERNAL_LOOKUP = ROOT / "app" / "src" / "main" / "java" / "com" / "sysadmindoc" / "callshield" / "data" / "remote" / "ExternalLookup.kt" |
27 | 37 | FETCH_TIMEOUT_SECONDS = 20 |
| 38 | +DATA = ROOT / "data" |
28 | 39 |
|
29 | 40 |
|
30 | 41 | @dataclass(frozen=True) |
@@ -94,7 +105,169 @@ def check(source: Source, fetcher: Fetch) -> tuple[bool, str]: |
94 | 105 | return True, "answers in the shape the app parses, and still flags a known spam number" |
95 | 106 |
|
96 | 107 |
|
| 108 | +# Well-known US customer-service lines. SkipCalls naming one of these as spam |
| 109 | +# would be a false alarm the app shows to the user. |
| 110 | +BUSINESS_LINES = ( |
| 111 | + "+18002752273", # Apple Support |
| 112 | + "+18008291040", # IRS |
| 113 | + "+18007721213", # Social Security |
| 114 | + "+18006334227", # Medicare |
| 115 | + "+18002758777", # USPS |
| 116 | + "+18007425877", # UPS |
| 117 | + "+18004633339", # FedEx |
| 118 | + "+18002255345", # DHL Express |
| 119 | + "+18882804331", # Amazon |
| 120 | + "+18004321000", # Bank of America |
| 121 | + "+18009359935", # Chase |
| 122 | + "+18008693557", # Wells Fargo |
| 123 | + "+18009505114", # Citi |
| 124 | + "+18002274825", # Capital One |
| 125 | + "+18003472683", # Discover |
| 126 | + "+18005284800", # American Express |
| 127 | + "+18882211161", # PayPal |
| 128 | + "+18009346489", # Xfinity |
| 129 | + "+18009220204", # Verizon |
| 130 | + "+18003310500", # AT&T |
| 131 | + "+18009378997", # T-Mobile |
| 132 | + "+18002211212", # Delta |
| 133 | + "+18008648331", # United |
| 134 | + "+18004337300", # American Airlines |
| 135 | + "+18004359792", # Southwest |
| 136 | + "+18006427676", # Microsoft |
| 137 | + "+18006249897", # Dell |
| 138 | + "+18004746836", # HP |
| 139 | + "+18882378289", # Best Buy |
| 140 | + "+18009256278", # Walmart |
| 141 | + "+18005913869", # Target |
| 142 | + "+18004663337", # Home Depot |
| 143 | + "+18004456937", # Lowe's |
| 144 | + "+18007742678", # Costco |
| 145 | + "+18007467287", # CVS |
| 146 | + "+18009254733", # Walgreens |
| 147 | + "+18002077847", # GEICO |
| 148 | + "+18007828332", # State Farm |
| 149 | + "+18007764737", # Progressive |
| 150 | + "+18002557828", # Allstate |
| 151 | + "+18005315000", # DirecTV |
| 152 | + "+18003333474", # DISH |
| 153 | + "+18883973742", # Experian |
| 154 | + "+18009168800", # TransUnion |
| 155 | + "+18773824357", # FTC |
| 156 | + "+18008722657", # U.S. Bank |
| 157 | + "+18887622265", # PNC |
| 158 | + "+18887519000", # TD Bank |
| 159 | + "+18008472911", # Visa |
| 160 | + "+18006278372", # Mastercard |
| 161 | +) |
| 162 | + |
| 163 | +SKIPCALLS_FLAGGED_CATEGORIES = frozenset({"scam", "robocall", "telemarketer", "fraud"}) |
| 164 | + |
| 165 | + |
| 166 | +@dataclass(frozen=True) |
| 167 | +class Answer: |
| 168 | + number: str |
| 169 | + # "flagged" (a category the app acts on), "uncategorized" (is_spam with any |
| 170 | + # other category, which the app shows as neutral), "clean" or "error". |
| 171 | + verdict: str |
| 172 | + |
| 173 | + |
| 174 | +def classify(status: int, body: str) -> str: |
| 175 | + if status != 200: |
| 176 | + return "error" |
| 177 | + try: |
| 178 | + parsed = json.loads(body) |
| 179 | + except ValueError: |
| 180 | + return "error" |
| 181 | + if not isinstance(parsed, dict) or not isinstance(parsed.get("is_spam"), bool): |
| 182 | + return "error" |
| 183 | + if not parsed["is_spam"]: |
| 184 | + return "clean" |
| 185 | + category = str(parsed.get("status_description", "")).strip().lower() |
| 186 | + return "flagged" if category in SKIPCALLS_FLAGGED_CATEGORIES else "uncategorized" |
| 187 | + |
| 188 | + |
| 189 | +def summarize(name: str, answers: list[Answer], local_numbers: set[str]) -> dict: |
| 190 | + """One group's counts. `adds` is a flag for a number the local database doesn't hold.""" |
| 191 | + flagged = [a for a in answers if a.verdict == "flagged"] |
| 192 | + adds = [a for a in flagged if a.number not in local_numbers] |
| 193 | + answered = [a for a in answers if a.verdict != "error"] |
| 194 | + return { |
| 195 | + "group": name, |
| 196 | + "asked": len(answers), |
| 197 | + "answered": len(answered), |
| 198 | + "flagged": len(flagged), |
| 199 | + "adds": len(adds), |
| 200 | + "uncategorized": sum(1 for a in answers if a.verdict == "uncategorized"), |
| 201 | + "errors": len(answers) - len(answered), |
| 202 | + "add_examples": [a.number for a in adds[:5]], |
| 203 | + } |
| 204 | + |
| 205 | + |
| 206 | +def database_numbers() -> set[str]: |
| 207 | + database = json.loads((DATA / "spam_numbers.json").read_text(encoding="utf-8")) |
| 208 | + return {row["number"] for row in database["numbers"]} |
| 209 | + |
| 210 | + |
| 211 | +def pending_numbers(published: set[str]) -> list[str]: |
| 212 | + pending = json.loads((DATA / "community_pending.json").read_text(encoding="utf-8")) |
| 213 | + return sorted( |
| 214 | + number for number, record in pending["numbers"].items() if not record.get("published") and number not in published |
| 215 | + ) |
| 216 | + |
| 217 | + |
| 218 | +def ask_all(numbers: Iterable[str], fetcher: Fetch, delay_seconds: float) -> list[Answer]: |
| 219 | + answers = [] |
| 220 | + for index, number in enumerate(numbers): |
| 221 | + if index and delay_seconds: |
| 222 | + time.sleep(delay_seconds) |
| 223 | + digits = re.sub(r"\D", "", number) |
| 224 | + try: |
| 225 | + status, body = fetcher(SOURCES[0].url_prefix + digits) |
| 226 | + except OSError: |
| 227 | + status, body = 0, "" |
| 228 | + answers.append(Answer(number, classify(status, body))) |
| 229 | + return answers |
| 230 | + |
| 231 | + |
| 232 | +def sample(fetcher: Fetch, seed: int, per_group: int, delay_seconds: float) -> list[dict]: |
| 233 | + published = database_numbers() |
| 234 | + pending = pending_numbers(published) |
| 235 | + rng = random.Random(seed) |
| 236 | + groups = { |
| 237 | + "database": rng.sample(sorted(published), min(per_group, len(published))), |
| 238 | + "pending": rng.sample(pending, min(per_group, len(pending))), |
| 239 | + "business": list(BUSINESS_LINES), |
| 240 | + } |
| 241 | + return [summarize(name, ask_all(numbers, fetcher, delay_seconds), published) for name, numbers in groups.items()] |
| 242 | + |
| 243 | + |
| 244 | +def percent(count: int, total: int) -> str: |
| 245 | + return f"{100 * count / total:.1f}%" if total else "n/a" |
| 246 | + |
| 247 | + |
| 248 | +def print_summaries(summaries: list[dict]) -> None: |
| 249 | + for s in summaries: |
| 250 | + print( |
| 251 | + f"{s['group']:<9} asked {s['asked']:>3}, answered {s['answered']:>3}: " |
| 252 | + f"flagged {s['flagged']:>3} ({percent(s['flagged'], s['answered'])}), " |
| 253 | + f"adds to the local database {s['adds']:>3} ({percent(s['adds'], s['answered'])}), " |
| 254 | + f"is_spam without a category the app acts on {s['uncategorized']:>3}, errors {s['errors']}" |
| 255 | + ) |
| 256 | + if s["add_examples"]: |
| 257 | + print(f" adds, first few: {', '.join(s['add_examples'])}") |
| 258 | + |
| 259 | + |
97 | 260 | def main() -> int: |
| 261 | + parser = argparse.ArgumentParser(description="Check or measure the live caller-lookup sources.") |
| 262 | + parser.add_argument("--sample", action="store_true", help="measure what SkipCalls adds instead of checking its shape") |
| 263 | + parser.add_argument("--per-group", type=int, default=200, help="numbers drawn from the database and the pending pool") |
| 264 | + parser.add_argument("--seed", type=int, default=20260930) |
| 265 | + parser.add_argument("--delay", type=float, default=0.5, help="seconds between requests") |
| 266 | + args = parser.parse_args() |
| 267 | + if args.sample: |
| 268 | + print_summaries(sample(fetch, args.seed, args.per_group, args.delay)) |
| 269 | + return 0 |
| 270 | + |
98 | 271 | configured = app_source_urls(EXTERNAL_LOOKUP.read_text(encoding="utf-8")) |
99 | 272 | probed = {source.url_prefix for source in SOURCES} |
100 | 273 | failures = 0 |
|
0 commit comments