forked from mayneyao/NotionPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (51 loc) · 1.88 KB
/
app.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
from types import SimpleNamespace
from configparser import ConfigParser
from flask import Flask, escape, request, Response
from flask_cors import CORS
from notion.client import NotionClient
from notion.collection import CollectionQuery
from notion.block import CodeBlock, EmbedOrUploadBlock
from notion.utils import remove_signed_prefix_as_needed
from actions import notion_plus
conf = ConfigParser()
conf.read('config.ini')
token = conf.get('notion', 'token')
timezone = conf.get('notion', 'timezone')
auth_token = conf.get('security', 'auth_token')
client = NotionClient(token_v2=token, timezone=timezone)
app = Flask(__name__)
CORS(app)
def get_notion_action_code(action_name, action_table_url):
actions_cv = client.get_collection_view(action_table_url)
action_block = None
q = CollectionQuery(actions_cv.collection, actions_cv, action_name)
for row in q.execute():
print(row)
action_block = row
action_code = None
for block in action_block.children:
if isinstance(block, CodeBlock):
action_code = block.title
return action_code
@app.route('/', methods=['POST'])
def npp():
auth = request.headers.get('authtoken')
if auth == auth_token:
act = SimpleNamespace(**request.json)
action_name = None
obj = client.get_block(act.blockID)
if act.actionName.startswith('#'):
action_name = act.actionName.split('#')[-1]
action_code = get_notion_action_code(action_name, act.actionTableUrl)
if action_code:
exec(action_code)
else:
return 'Task Code Not Found'
elif act.actionName.startswith('@'):
action_name = act.actionName.split('@')[-1]
func = notion_plus.action_func_map[action_name]
func(obj)
setattr(obj, action_name, False)
return 'Ok'
else:
return 'Bad Token'