GodsDevProject commited on
Commit
b25aadb
·
verified ·
1 Parent(s): 642305d

Create ingest/export.py

Browse files
Files changed (1) hide show
  1. 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
- 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
 
 
 
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