Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,6 @@ Every `Result` object returned by the engine supports the following methods:

To format a **List** of results, use the `formatter`:
- `into_json(results_list)`
- `into_csv(results_list)`
- `into_csv(results_list, include_header=True)` — pass `include_header=False` when appending to a CSV that already has a header, to avoid writing it twice.

---
20 changes: 19 additions & 1 deletion tests/test_formatter.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from user_scanner.core.formatter import into_csv, into_json
from user_scanner.core.formatter import CSV_HEADER, into_csv, into_json
from user_scanner.core.result import Result


def test_get_result_output_formats():
res = Result.available(username="alice", site_name="ExampleSite", category="Cat")
res2 = Result.available(username="bob", site_name="ExampleSite", category="Cat")

out_console = res.get_console_output()
assert "Not Found" in out_console
Expand All @@ -14,6 +15,23 @@ def test_get_result_output_formats():
assert '"username": "alice"' in out_json
assert '"site_name": "ExampleSite"' in out_json

# Test CSV output with header included(Default behavior)
out_csv = into_csv([res])
assert "alice" in out_csv
assert "ExampleSite" in out_csv

# Test CSV output with header excluded
out_csv_no_header = into_csv([res], include_header=False)
assert "alice" in out_csv_no_header
assert "ExampleSite" in out_csv_no_header
assert out_csv_no_header.count(CSV_HEADER) == 0

# Test CSV output with multiple results and header included
first = into_csv([res], include_header=True)
second = into_csv([res2], include_header=False)

full = first + "\n" + second

assert full.count(CSV_HEADER) == 1
assert "alice" in full
assert "bob" in full
3 changes: 2 additions & 1 deletion user_scanner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,14 @@ def main():
print(G + f"\n[+] JSON Results saved to {t_output}" + Style.RESET_ALL)

elif args.format == "csv":
content_csv = formatter.into_csv(t_results)
try:
with open(t_output, "r", encoding="utf-8") as init_file:
has_content = init_file.read().strip() != ""
except Exception:
has_content = False

content_csv = formatter.into_csv(t_results, include_header=not has_content)

with open(t_output, "a", encoding="utf-8") as f:
if has_content:
f.write("\n")
Expand Down
4 changes: 2 additions & 2 deletions user_scanner/core/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def get_json_data(results: List[Result]) -> list:
return [r.to_dict() for r in results]


def into_csv(results: List[Result]) -> str:
return CSV_HEADER + "\n" + "\n".join(result.to_csv() for result in results)
def into_csv(results: List[Result], include_header: bool=True) -> str:
return CSV_HEADER + "\n" + "\n".join(result.to_csv() for result in results) if include_header else "\n".join(result.to_csv() for result in results)


def into_pdf(
Expand Down
Loading