Spaces:
Sleeping
Sleeping
Create ingest/export.py
Browse files- ingest/export.py +19 -7
ingest/export.py
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
|
|
| 1 |
import zipfile
|
| 2 |
-
import json
|
| 3 |
-
import tempfile
|
| 4 |
|
| 5 |
-
def
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
import zipfile
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def export_results_zip(results):
|
| 5 |
+
"""
|
| 6 |
+
results: list[dict]
|
| 7 |
+
returns: BytesIO zip
|
| 8 |
+
"""
|
| 9 |
+
buffer = io.BytesIO()
|
| 10 |
+
with zipfile.ZipFile(buffer, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 11 |
+
for i, r in enumerate(results, start=1):
|
| 12 |
+
content = (
|
| 13 |
+
f"Source: {r.get('source')}\n"
|
| 14 |
+
f"Title: {r.get('title')}\n"
|
| 15 |
+
f"URL: {r.get('url')}\n\n"
|
| 16 |
+
f"Snippet:\n{r.get('snippet')}"
|
| 17 |
+
)
|
| 18 |
+
zf.writestr(f"{i:03d}_{r.get('source')}.txt", content)
|
| 19 |
+
|
| 20 |
+
buffer.seek(0)
|
| 21 |
+
return buffer
|