-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_website_technology_csv.py
More file actions
78 lines (65 loc) · 2.51 KB
/
Copy pathexport_website_technology_csv.py
File metadata and controls
78 lines (65 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Flatten a Website Technology Lookup Dataset JSON export into CSV."""
from __future__ import annotations
import argparse
import csv
import json
from pathlib import Path
from typing import Any
FIELDS = [
"domain",
"status",
"title",
"category",
"technology",
"version",
"detectionStatus",
"isCurrentlyDetected",
"httpStatusCode",
"attempts",
"error",
]
def export_csv(input_path: Path, output_path: Path) -> None:
results = json.loads(input_path.read_text(encoding="utf-8"))
if not isinstance(results, list):
raise ValueError("The input file must contain a JSON array of Dataset items.")
rows: list[dict[str, Any]] = []
for result in results:
technologies = result.get("technologies") or []
if not technologies:
rows.append({
"domain": result.get("domain"),
"status": result.get("status"),
"title": result.get("title"),
"httpStatusCode": result.get("httpStatusCode"),
"attempts": result.get("attempts"),
"error": result.get("error"),
})
continue
for technology in technologies:
rows.append({
"domain": result.get("domain"),
"status": result.get("status"),
"title": result.get("title"),
"category": technology.get("category"),
"technology": technology.get("name"),
"version": technology.get("version"),
"detectionStatus": technology.get("detectionStatus"),
"isCurrentlyDetected": technology.get("isCurrentlyDetected"),
"httpStatusCode": result.get("httpStatusCode"),
"attempts": result.get("attempts"),
"error": result.get("error"),
})
output_path.parent.mkdir(parents=True, exist_ok=True)
with output_path.open("w", encoding="utf-8", newline="") as handle:
writer = csv.DictWriter(handle, fieldnames=FIELDS, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("input", type=Path, nargs="?", default=Path("data/sample-output.json"))
parser.add_argument("output", type=Path, nargs="?", default=Path("data/exported-website-technology.csv"))
args = parser.parse_args()
export_csv(args.input, args.output)
print(f"Wrote {args.output}")
if __name__ == "__main__":
main()