GodsDevProject commited on
Commit
78539be
·
verified ·
1 Parent(s): 141afab

Create ingest/export.py

Browse files
Files changed (1) hide show
  1. ingest/export.py +42 -13
ingest/export.py CHANGED
@@ -1,16 +1,45 @@
1
- import io
2
  import zipfile
 
 
 
3
 
4
- def export_results_zip(results):
5
- buffer = io.BytesIO()
6
- with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
7
- for i, r in enumerate(results, start=1):
8
- content = (
9
- f"Source: {r['source']}\n"
10
- f"Title: {r['title']}\n"
11
- f"URL: {r['url']}\n\n"
12
- f"Snippet:\n{r['snippet']}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  )
14
- zf.writestr(f"{i:03d}_{r['source']}.txt", content)
15
- buffer.seek(0)
16
- return buffer
 
 
 
 
 
 
 
 
 
 
 
 
 
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