-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_csv.py
More file actions
91 lines (75 loc) · 2.9 KB
/
Copy pathexport_csv.py
File metadata and controls
91 lines (75 loc) · 2.9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
"""MF-Engine — collect every dataset into data/csv/ as CSV.
The pipeline phases write their native formats (JSON for the seed list and page
inventory, CSV for fund managers); the SEBI scraper writes both. This gathers
everything into one place, in CSV, for spreadsheet/BI consumption:
data/csv/fund_managers.csv Phase 3 — managers per AMC
data/csv/fund_managers_enriched.csv Phase 4 — + LinkedIn / email
data/csv/amc_seed_list.csv Phase 1 — AMFI roster + domains
data/csv/sebi_*.csv SEBI directories (own scraper)
data/csv/wealth_managers.csv all SEBI types, rolled up
Usage:
python src/export_csv.py
"""
import csv
import json
import logging
import shutil
import sys
from pathlib import Path
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger("mf-engine.export")
DATA_DIR = Path("data")
CSV_DIR = DATA_DIR / "csv"
# JSON datasets → CSV, with the column order we want in the export.
JSON_EXPORTS: dict[str, list[str]] = {
"amc_seed_list": [
"amc_id", "firm_name", "legal_name", "clean_name", "base_domain",
"sitemap_url", "sitemap_type", "sitemap_verified",
],
}
# Already-CSV datasets → copied through as-is.
CSV_COPIES = ["fund_managers", "fund_managers_enriched"]
def export_json(stem: str, columns: list[str]) -> int:
src = DATA_DIR / f"{stem}.json"
if not src.exists():
log.warning("%s missing — skipped", src)
return 0
rows = json.loads(src.read_text(encoding="utf-8"))
if not isinstance(rows, list) or not rows:
log.warning("%s empty — skipped", src)
return 0
out = CSV_DIR / f"{stem}.csv"
with out.open("w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=columns, extrasaction="ignore")
writer.writeheader()
writer.writerows(rows)
log.info("Wrote %d rows to %s", len(rows), out)
return len(rows)
def copy_csv(stem: str) -> int:
src = DATA_DIR / f"{stem}.csv"
if not src.exists():
log.warning("%s missing — skipped", src)
return 0
out = CSV_DIR / f"{stem}.csv"
shutil.copyfile(src, out)
with out.open(encoding="utf-8") as f:
n = max(sum(1 for _ in f) - 1, 0)
log.info("Wrote %d rows to %s", n, out)
return n
def main() -> int:
CSV_DIR.mkdir(parents=True, exist_ok=True)
total = 0
for stem, columns in JSON_EXPORTS.items():
total += export_json(stem, columns)
for stem in CSV_COPIES:
total += copy_csv(stem)
existing = sorted(p.name for p in CSV_DIR.glob("*.csv"))
log.info("data/csv/ now holds %d file(s): %s", len(existing), ", ".join(existing))
log.info("Note: sebi_*.csv and wealth_managers.csv come from sebi_intermediaries.py")
return 0 if total else 1
if __name__ == "__main__":
sys.exit(main())