This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotion.py
129 lines (111 loc) · 4.84 KB
/
notion.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
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
import requests
import time
import os
from datetime import datetime, timezone, timedelta
from configure import NOTION_TOKEN
from channel import CHANNELS
from slack import post_message
from cal import calender
# from cal.create_event import create_event
def get_channel(positions):
"""Return Slack channel based on position"""
# print(CHANNELS["channels"][0])
channel = []
for position in positions:
if position in CHANNELS["channels"][0]:
return CHANNELS["channels"][0][position]
elif position in CHANNELS["channels"][1]:
channel.append(CHANNELS["channels"][1][position])
if positions == []:
return ",".join(CHANNELS["channels"][2])
return channel
def read_database(database_id):
"""Read events from Notion database and post reminders to Slack"""
HEADERS = {
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-02-22",
}
response = requests.post(
f"https://api.notion.com/v1/databases/{database_id}/query",
headers=HEADERS,
)
datas = response.json()["results"]
today = datetime.now(timezone(timedelta(hours=9))).strftime("%Y-%m-%d")
tomorrow = (datetime.now(timezone(timedelta(hours=9))) + timedelta(1)).strftime("%Y-%m-%d")
week = (datetime.now(timezone(timedelta(hours=9))) + timedelta(7)).strftime("%Y-%m-%d")
print(response)
for data in datas:
try:
title = data["properties"]["Name"]["title"][0]["plain_text"]
file_path = title + ".ics"
start_date = data["properties"]["Date"]["date"]["start"]
end_date = data["properties"]["Date"]["date"].get("end")
ThreadUrl = data["properties"]["슬렉 쓰레드 링크"]["url"]
url = data["url"]
positions = [p["name"] for p in data["properties"]["Event Type"]["multi_select"]]
channel = get_channel(positions)
except Exception as e:
continue
if title != "" and start_date != "":
try:
check = start_date[:10]
# 시간 계산
if (
(start_date == tomorrow or check == tomorrow)
or (start_date == today or check == today)
or (start_date == week or check == week)
):
if end_date == None:
end_date = start_date
end = ""
# end_date ="빈값"
calender(title, start_date, end_date, url)
if start_date == start_date[:10]: # 시작 날짜 = 년/월/일
if end_date:
end = " ~ " + end_date
else:
start_date = start_date[:16]
start_date = datetime.strptime(start_date, "%Y-%m-%dT%H:%M")
start_date = start_date.strftime("%Y-%m-%d %I:%M %p")
end_date = end_date[:16]
end_date = datetime.strptime(end_date, "%Y-%m-%dT%H:%M")
end_date = end_date.strftime("%Y-%m-%d %I:%M %p")
end = " ~ " + str(end_date)
if start_date == tomorrow or check == tomorrow:
isToday = "[내일]"
elif start_date == today or check == today:
isToday = "[오늘]"
elif start_date == week or check == week:
isToday = "[다음 주]"
positions = ", ".join(positions)
if type(channel) == list:
for cn in channel:
post_message(
cn,
isToday,
title,
start_date,
end,
positions,
url,
file_path,
ThreadUrl,
)
else:
post_message(
channel,
isToday,
title,
start_date,
end,
positions,
url,
file_path,
ThreadUrl,
)
os.remove(file_path)
# post_message("#bot-lab",isToday,title,start_date,end_date,position)
check = ""
except TypeError as e:
print(e)
pass