Spaces:
Sleeping
Sleeping
Update exporters.py
Browse files- exporters.py +16 -19
exporters.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
import os, json
|
| 2 |
-
from typing import Dict
|
| 3 |
from models import NarrativeData, CompanyInfo
|
| 4 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 5 |
|
|
@@ -48,12 +48,9 @@ def export_to_notion(outputs: Dict[str, str]) -> Dict[str, str]:
|
|
| 48 |
return {"status": "skipped", "reason": "NOTION_API_KEY or NOTION_DATABASE_ID not set"}
|
| 49 |
|
| 50 |
notion = Client(auth=api_key)
|
| 51 |
-
# Create one parent page, then child pages for each file
|
| 52 |
parent = notion.pages.create(
|
| 53 |
parent={"database_id": database_id},
|
| 54 |
-
properties={
|
| 55 |
-
"Name": {"title": [{"text": {"content": "PR/IR One-Pass Outputs"}}]}
|
| 56 |
-
}
|
| 57 |
)
|
| 58 |
parent_id = parent.get("id")
|
| 59 |
|
|
@@ -61,13 +58,13 @@ def export_to_notion(outputs: Dict[str, str]) -> Dict[str, str]:
|
|
| 61 |
notion.pages.create(
|
| 62 |
parent={"page_id": parent_id},
|
| 63 |
properties={"Name": {"title": [{"text": {"content": name}}]}},
|
| 64 |
-
children=[
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
-
|
| 69 |
-
result["parent_page_id"] = parent_id
|
| 70 |
-
return result
|
| 71 |
|
| 72 |
# ---------------- Google Docs Export (optional) ----------------
|
| 73 |
def export_to_google_docs(outputs: Dict[str, str]) -> Dict[str, str]:
|
|
@@ -99,18 +96,18 @@ def export_to_google_docs(outputs: Dict[str, str]) -> Dict[str, str]:
|
|
| 99 |
doc = docs_service.documents().create(body={"title": name}).execute()
|
| 100 |
doc_id = doc.get("documentId")
|
| 101 |
|
| 102 |
-
|
| 103 |
-
requests = [
|
| 104 |
-
{"insertText": {"location": {"index": 1}, "text": md}}
|
| 105 |
-
]
|
| 106 |
docs_service.documents().batchUpdate(documentId=doc_id, body={"requests": requests}).execute()
|
| 107 |
|
| 108 |
-
# Move to folder if provided
|
| 109 |
if folder_id:
|
| 110 |
-
# first, get current parents
|
| 111 |
file = drive_service.files().get(fileId=doc_id, fields="parents").execute()
|
| 112 |
prev_parents = ",".join(file.get("parents", []))
|
| 113 |
-
drive_service.files().update(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
result["docs"].append(f"https://docs.google.com/document/d/{doc_id}/edit")
|
| 116 |
return result
|
|
|
|
| 1 |
+
import os, json
|
| 2 |
+
from typing import Dict
|
| 3 |
from models import NarrativeData, CompanyInfo
|
| 4 |
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
| 5 |
|
|
|
|
| 48 |
return {"status": "skipped", "reason": "NOTION_API_KEY or NOTION_DATABASE_ID not set"}
|
| 49 |
|
| 50 |
notion = Client(auth=api_key)
|
|
|
|
| 51 |
parent = notion.pages.create(
|
| 52 |
parent={"database_id": database_id},
|
| 53 |
+
properties={"Name": {"title": [{"text": {"content": "PR/IR One-Pass Outputs"}}]}},
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
parent_id = parent.get("id")
|
| 56 |
|
|
|
|
| 58 |
notion.pages.create(
|
| 59 |
parent={"page_id": parent_id},
|
| 60 |
properties={"Name": {"title": [{"text": {"content": name}}]}},
|
| 61 |
+
children=[{
|
| 62 |
+
"object": "block",
|
| 63 |
+
"type": "paragraph",
|
| 64 |
+
"paragraph": {"rich_text": [{"type": "text", "text": {"content": md}}]}
|
| 65 |
+
}]
|
| 66 |
)
|
| 67 |
+
return {"status": "ok", "parent_page_id": parent_id}
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# ---------------- Google Docs Export (optional) ----------------
|
| 70 |
def export_to_google_docs(outputs: Dict[str, str]) -> Dict[str, str]:
|
|
|
|
| 96 |
doc = docs_service.documents().create(body={"title": name}).execute()
|
| 97 |
doc_id = doc.get("documentId")
|
| 98 |
|
| 99 |
+
requests = [{"insertText": {"location": {"index": 1}, "text": md}}]
|
|
|
|
|
|
|
|
|
|
| 100 |
docs_service.documents().batchUpdate(documentId=doc_id, body={"requests": requests}).execute()
|
| 101 |
|
|
|
|
| 102 |
if folder_id:
|
|
|
|
| 103 |
file = drive_service.files().get(fileId=doc_id, fields="parents").execute()
|
| 104 |
prev_parents = ",".join(file.get("parents", []))
|
| 105 |
+
drive_service.files().update(
|
| 106 |
+
fileId=doc_id,
|
| 107 |
+
addParents=folder_id,
|
| 108 |
+
removeParents=prev_parents,
|
| 109 |
+
fields="id, parents"
|
| 110 |
+
).execute()
|
| 111 |
|
| 112 |
result["docs"].append(f"https://docs.google.com/document/d/{doc_id}/edit")
|
| 113 |
return result
|