Spaces:
Sleeping
Sleeping
Create ingest/export.py
Browse files- ingest/export.py +42 -13
ingest/export.py
CHANGED
|
@@ -1,16 +1,45 @@
|
|
| 1 |
-
import
|
| 2 |
import zipfile
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import zipfile
|
| 3 |
+
import json
|
| 4 |
+
import tempfile
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
def export_results(results: list) -> str:
|
| 8 |
+
"""
|
| 9 |
+
Always returns a valid ZIP path.
|
| 10 |
+
Never throws.
|
| 11 |
+
"""
|
| 12 |
+
ts = datetime.utcnow().strftime("%Y%m%d_%H%M%S")
|
| 13 |
+
tmpdir = tempfile.mkdtemp()
|
| 14 |
+
zip_path = os.path.join(tmpdir, f"foia_results_{ts}.zip")
|
| 15 |
+
|
| 16 |
+
live_results = [r for r in results if r.get("exportable")]
|
| 17 |
+
|
| 18 |
+
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 19 |
+
if not live_results:
|
| 20 |
+
# Metadata-only export (HF-safe)
|
| 21 |
+
zf.writestr(
|
| 22 |
+
"README.txt",
|
| 23 |
+
"No live, exportable FOIA documents were returned.\n\n"
|
| 24 |
+
"Stub results are informational only and cannot be exported.\n"
|
| 25 |
+
"This behavior is intentional and compliant with agency policies."
|
| 26 |
+
)
|
| 27 |
+
zf.writestr(
|
| 28 |
+
"results.json",
|
| 29 |
+
json.dumps(results, indent=2)
|
| 30 |
)
|
| 31 |
+
return zip_path
|
| 32 |
+
|
| 33 |
+
# Live metadata
|
| 34 |
+
zf.writestr(
|
| 35 |
+
"results.json",
|
| 36 |
+
json.dumps(live_results, indent=2)
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# URLs list (no scraping!)
|
| 40 |
+
zf.writestr(
|
| 41 |
+
"urls.txt",
|
| 42 |
+
"\n".join(r["url"] for r in live_results if r.get("url"))
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return zip_path
|