Spaces:
Sleeping
Sleeping
Create ingest/export.py
Browse files- ingest/export.py +7 -40
ingest/export.py
CHANGED
|
@@ -1,45 +1,12 @@
|
|
| 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 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 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(
|
| 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
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import zipfile
|
| 3 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def export_results(results):
|
| 6 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
| 7 |
+
with zipfile.ZipFile(tmp.name, "w", zipfile.ZIP_DEFLATED) as z:
|
| 8 |
+
z.writestr(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"results.json",
|
| 10 |
+
json.dumps(results, indent=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
+
return tmp.name
|
|
|