Skip to content

Commit 3bdd58e

Browse files
authored
Merge pull request #317 from kaifcodec/fix/output-bug
refactor: use json module for result serialization in into_json
2 parents df932ab + 7e6391d commit 3bdd58e

3 files changed

Lines changed: 31 additions & 20 deletions

File tree

user_scanner/__main__.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import argparse
22
import json
3+
import os
34
import re
45
import sys
56
import time
@@ -384,25 +385,28 @@ def main():
384385
else formatter.into_json(results)
385386
)
386387

388+
if args.output:
387389
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)
388392
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)
402393

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)
403406
with open(args.output, "w", encoding="utf-8") as f:
404407
json.dump(data, f, indent=2, ensure_ascii=False)
405408

409+
406410
if args.format == "csv":
407411
try:
408412
with open(args.output, "r", encoding="utf-8") as init_file:

user_scanner/core/formatter.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from user_scanner.core.result import Result
23
from typing import List
34

@@ -14,14 +15,11 @@ def indentate(msg: str, indent: int):
1415

1516

1617
def into_json(results: List[Result]) -> str:
17-
res = "[\n"
18+
return json.dumps([r.to_dict() for r in results], indent=4)
1819

19-
for i, result in enumerate(results):
20-
is_last = i == len(results) - 1
21-
end = "" if is_last else ","
22-
res += indentate(result.to_json().replace("\t", INDENT), 1) + end + "\n"
23-
24-
return res + "]"
20+
def get_json_data(results: List[Result]) -> list:
21+
"""Returns a list of dictionaries ready for JSON serialization."""
22+
return [r.to_dict() for r in results]
2523

2624

2725
def into_csv(results: List[Result]) -> str:

user_scanner/core/result.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ def as_dict(self) -> dict:
141141
"is_email": self.is_email,
142142
}
143143

144+
def to_dict(self) -> dict:
145+
"""Prepares the clean dictionary for JSON/Exporting."""
146+
data = self.as_dict()
147+
148+
if self.is_email:
149+
data["email"] = data.pop("username")
150+
data.pop("is_email", None)
151+
return data
152+
144153
def debug(self) -> str:
145154
return DEBUG_MSG.format(**self.as_dict())
146155

0 commit comments

Comments
 (0)