-
Notifications
You must be signed in to change notification settings - Fork 1
/
notion-agent.py
executable file
·67 lines (57 loc) · 2.11 KB
/
notion-agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from dotenv import load_dotenv
import requests
from notion_client import Client
import re
# Load environment variables
load_dotenv()
# Set up Notion client
notion = Client(auth=os.getenv("NOTION_API_KEY"))
def read_file(file_path):
with open(file_path, 'r') as file:
return file.read()
def update_notion_page(page_id, content):
# Split content into paragraphs
paragraphs = content.split('\n\n')
for paragraph in paragraphs:
# If a single paragraph is longer than 2000 characters, split it
if len(paragraph) > 2000:
chunks = [paragraph[i:i+2000] for i in range(0, len(paragraph), 2000)]
for chunk in chunks:
notion.blocks.children.append(
block_id=page_id,
children=[
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": chunk.strip()}}]
}
}
]
)
else:
notion.blocks.children.append(
block_id=page_id,
children=[
{
"object": "block",
"type": "paragraph",
"paragraph": {
"rich_text": [{"type": "text", "text": {"content": paragraph.strip()}}]
}
}
]
)
# Main execution
file_path = "transcript.txt"
page_id = os.getenv("NOTION_PAGE_ID")
# Read the content from the file
file_content = read_file(file_path)
# Clear existing content (optional)
notion.blocks.children.list(block_id=page_id)
for block in notion.blocks.children.list(block_id=page_id)["results"]:
notion.blocks.delete(block_id=block["id"])
# Update the Notion page with new content
update_notion_page(page_id, file_content)
print("Content updated successfully!")