Compare fetched data with existing and return a diff report.
(existing: dict[str, dict], fetched: list[dict])
| 241 | items = json.load(f) |
| 242 | return {r["number"]: r for r in items if isinstance(r, dict) and "number" in r} |
| 243 | except Exception as e: |
| 244 | print(f"[warn] could not load existing standards.json: {e}") |
| 245 | return {} |
| 246 | |
| 247 | |
| 248 | def _compute_diff(existing: dict[str, dict], fetched: list[dict]) -> dict: |
| 249 | """Compare fetched data with existing and return a diff report.""" |
| 250 | fetched_map = {r["number"]: r for r in fetched} |
| 251 | existing_nums = set(existing.keys()) |
| 252 | fetched_nums = set(fetched_map.keys()) |
| 253 | |
| 254 | added = sorted(fetched_nums - existing_nums) |
| 255 | removed = sorted(existing_nums - fetched_nums) |
| 256 | |
| 257 | changed: list[dict] = [] |
| 258 | for num in sorted(existing_nums & fetched_nums): |
| 259 | old, new = existing[num], fetched_map[num] |
| 260 | diffs = {} |
| 261 | for field in ("title_zh", "status", "status_zh", "effective_date", "approval_date"): |
| 262 | ov, nv = old.get(field, ""), new.get(field, "") |
| 263 | if ov != nv: |
| 264 | diffs[field] = {"old": ov, "new": nv} |
| 265 | if diffs: |
| 266 | changed.append({"number": num, "fields": diffs}) |
| 267 | |
| 268 | return { |
| 269 | "added_count": len(added), |
| 270 | "removed_count": len(removed), |
| 271 | "changed_count": len(changed), |
| 272 | "added": added[:20], |
| 273 | "removed": removed[:20], |