-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotion_sync.py
More file actions
130 lines (114 loc) · 4.27 KB
/
notion_sync.py
File metadata and controls
130 lines (114 loc) · 4.27 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pandas as pd
import requests
from dotenv import load_dotenv
import os
import json
# Load environment variables
dotenv_path = os.path.join(os.getcwd(), '.env')
load_dotenv(dotenv_path)
notion_api_key = os.getenv("NOTION_API_KEY")
database_id=os.getenv("NOTION_DATABASE_ID1")
#set working directory to the location of this file
os.chdir(os.path.dirname(os.path.abspath(__file__)))
headers = {
"Authorization": f"Bearer {notion_api_key}",
"Content-Type": "application/json",
"Notion-Version": "2022-06-28"
}
json_file_path = 'upserted_records.json'
try:
with open(json_file_path, 'r') as file:
upserted_records = json.load(file)
except FileNotFoundError:
upserted_records = {}
def search_notion_page_by_email(email_id):
search_payload = {
"filter": {
"property": "EMAIL_ID",
"text": {
"equals": email_id
}
},
"page_size": 1
}
search_response = requests.post(f'https://api.notion.com/v1/databases/{database_id}/query', json=search_payload, headers=headers)
print(search_response.json())
if search_response.status_code == 200:
results = search_response.json().get("results", [])
if results:
return results[0]["id"] # Return the first match's page ID
return None
def upsert_notion_page(action, summary, due_date, priority, email_id, to, from_, date):
# Check if due_date is NaN or 'NA' before processing
# Before creating a new page, check if it already exists by email_id
print(due_date)
if pd.isna(due_date) or due_date == 'NA':
formatted_due_date = None
else:
# Since due_date is already in 'YYYY-MM-DD' format, no need for further formatting
formatted_due_date = due_date
if priority: # Check if priority is not None or empty
priority = priority.capitalize() # Capitalize the first letter of the priority tag
# Construct the data payload
data = {
"parent": {"database_id": database_id},
"properties": {
"Action": {
"rich_text": [
{"text": {"content": action}}
]
},
"Summary": {
"title": [
{"text": {"content": summary}}
]
},
"Priority": {
"select": {
"name": priority
}
},
"EMAIL_ID": {
"rich_text": [
{"text": {"content": email_id}}
]
},
"To": {
"rich_text": [
{"text": {"content": to}}
]
},
"From": {
"rich_text": [
{"text": {"content": from_}}
]
},
"Date": {
"date": {"start": date}
}
}
}
if formatted_due_date:
data["properties"]["Due_Date"] = {
"date": {"start": formatted_due_date}
}
response = requests.post('https://api.notion.com/v1/pages', json=data, headers=headers)
if response.status_code == 200:
print(f"{action}: Page upserted successfully")
return True
else:
print(f"{action}: Failed to upsert page", response.text)
return False
# Load CSV file and iterate over rows to create/update Notion pages
csv_file_path = 'processed_messages.csv'
df = pd.read_csv(csv_file_path)
for index, row in df.iterrows():
email_id = row['Email_ID']
action = row['Action'] if pd.notna(row['Action']) and row['Action'] != 'NA' else ''
if email_id not in upserted_records: # Proceed if the entry hasn't been upserted yet
success = upsert_notion_page(action, row['Summary'], row['Due_Date'], row['Priority'], row['Email_ID'], row['To'], row['From'], row['Date']) # Your function call here
if success:
upserted_records[email_id] = {'Upserted': 'Yes'}
# Write to the JSON file after each successful upsert
with open(json_file_path, 'w') as file:
json.dump(upserted_records, file, indent=4)