|
1 | 1 | import argparse |
2 | 2 | import json |
| 3 | +import os |
3 | 4 | import re |
4 | 5 | import sys |
5 | 6 | import time |
@@ -384,25 +385,28 @@ def main(): |
384 | 385 | else formatter.into_json(results) |
385 | 386 | ) |
386 | 387 |
|
| 388 | + if args.output: |
387 | 389 | if args.format == "json": |
| 390 | + # Get the new data as a LIST of DICTS, not a string |
| 391 | + new_items = formatter.get_json_data(results) |
388 | 392 | data = [] |
389 | | - try: |
390 | | - with open(args.output, "r", encoding="utf-8") as f: |
391 | | - old = json.load(f) |
392 | | - if isinstance(old, list) and all(isinstance(x, dict) for x in old): |
393 | | - data = old |
394 | | - except Exception: |
395 | | - pass |
396 | | - |
397 | | - new_items = json.loads(content) |
398 | | - if isinstance(new_items, list) and all( |
399 | | - isinstance(x, dict) for x in new_items |
400 | | - ): |
401 | | - data.extend(new_items) |
402 | 393 |
|
| 394 | + # Try to load existing data |
| 395 | + if os.path.exists(args.output): |
| 396 | + try: |
| 397 | + with open(args.output, "r", encoding="utf-8") as f: |
| 398 | + old = json.load(f) |
| 399 | + if isinstance(old, list): |
| 400 | + data = old |
| 401 | + except (json.JSONDecodeError, Exception): |
| 402 | + pass |
| 403 | + |
| 404 | + # Merge and save |
| 405 | + data.extend(new_items) |
403 | 406 | with open(args.output, "w", encoding="utf-8") as f: |
404 | 407 | json.dump(data, f, indent=2, ensure_ascii=False) |
405 | 408 |
|
| 409 | + |
406 | 410 | if args.format == "csv": |
407 | 411 | try: |
408 | 412 | with open(args.output, "r", encoding="utf-8") as init_file: |
|
0 commit comments