-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnotion.py
217 lines (202 loc) · 8.67 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from datetime import datetime
import json
from mail import archive_bill, send_email
from conf import configs
import requests
token = configs['notion']['token']
parent_type = configs['notion']['type']
database_id = configs['notion']['database_id']
debug = configs['debug']
if type(debug) is not bool:
debug = False
body = {
"parent": {
"type": parent_type,
"database_id": database_id,
},
"properties": {
"日期": {
"date": {
"start": "",
}
},
"星期": {
"select": {
"name": "", # 可选:周一、周二...周日
},
},
"账单信息": {
"title": [
{
"type": "text",
"text": {"content": ""},
}
]
},
"金额": {
"number": 0.0,
},
"交易平台": {
"select": {
"name": "", # 可选:支付宝、微信
},
},
"交易类型": {
"select": {
"name": "", # 可选:收入、支出
},
},
"交易方式": {
"rich_text": [
{
"type": "text",
"text": {"content": ""},
}
]
},
"平台交易单号": {
"rich_text": [
{
"type": "text",
"text": {"content": ""},
}
]
}
}
}
# 对比已有数据
# data 得到的数据字段形如:{'object': '', 'id': '', 'created_time': '',
# 'last_edited_time': '',
# 'created_by': {'object': '', 'id': ''},
# 'last_edited_by': {'object': '', 'id': ''}, 'cover': , 'icon': ,
# 'parent': {'type': '', 'database_id': ''}, 'archived': ,
# 'in_trash': , 'properties': {'': {'id': '', 'type': 'select', 'select': {'id': '', 'name': '', 'color': ''}}, },
# 'url': '', 'public_url': }
def check_contrast(platform):
# 第一次获取数据
get_data = requests.request(
'POST',
'https://api.notion.com/v1/databases/' + database_id + '/query',
headers={'Authorization': 'Bearer ' + token, 'Notion-Version': '2021-05-13'},
)
datas = json.loads(get_data.text)
if debug:
print(datas)
temp_list = []
temp_list.extend(datas['results'])
# 如果一页不能加载完所有数据,继续获取,防止添加重复数据(折磨了作者三天才发现
while datas['has_more']:
if debug:
print("数据量过大,正在获取更多数据")
get_data = requests.request(
'POST',
'https://api.notion.com/v1/databases/' + database_id + '/query',
headers={'Authorization': 'Bearer ' + token, 'Notion-Version': '2021-05-13'},
json={'start_cursor': datas['next_cursor']}
)
datas = json.loads(get_data.text)
if debug:
print(datas)
try:
temp_list.extend(datas['results'])
last_day = datetime.strptime(temp_list[-1]['properties']['日期']['date']['start'][:10], '%Y-%m-%d')
now_day = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
if (now_day - last_day).days >= 100:
print("已查询最近100天的数据")
break
except KeyError as e:
print("Notion 数据库连接异常,请检查相关信息或集成")
if debug:
print(e)
return -1 # 终止程序
bill_list = []
for data in temp_list:
# 根据平台筛选数据
if data['properties']['交易平台']['select']['name'] == platform:
try:
bill_list.append(data['properties']['平台交易单号']['rich_text'][0]['text']['content'])
except IndexError:
if debug:
print("该记录未填写订单号 " + str(data))
return bill_list
def sync_bills(server, platform, update_data):
nums = 0
same_nums = 0
bill_list = check_contrast(platform)
if bill_list == -1:
return -1
week_list = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
if platform == '微信':
for bill in update_data:
# 去除奇奇怪怪恼人的空格和占位符
if bill[8].replace('\t', '').replace(' ', '') not in bill_list:
body['properties']['日期']['date']['start'] = bill[0] + '+08:00'
body['properties']['星期']['select']['name'] = week_list[
datetime.strptime(bill[0], "%Y-%m-%d %H:%M:%S").weekday()]
body['properties']['金额']['number'] = float(bill[5].replace('¥', ''))
body['properties']['交易平台']['select']['name'] = platform
body['properties']['交易类型']['select']['name'] = bill[4]
body['properties']['平台交易单号']['rich_text'][0]['text']['content'] = bill[8].replace('\t',
'').replace(' ',
'')
if bill[4] == '支出':
body['properties']['交易方式']['rich_text'][0]['text']['content'] = bill[6]
if bill[3] == '"/"':
body['properties']['账单信息']['title'][0]['text']['content'] = bill[1]
else:
body['properties']['账单信息']['title'][0]['text']['content'] = bill[3]
else:
body['properties']['交易方式']['rich_text'][0]['text']['content'] = bill[7]
body['properties']['账单信息']['title'][0]['text']['content'] = bill[1]
try:
send_data = requests.request(
'POST',
'https://api.notion.com/v1/pages',
json=body,
headers={'Authorization': 'Bearer ' + token, 'Notion-Version': '2021-05-13'},
)
if debug:
print(send_data.text)
nums += 1
except requests.exceptions.ConnectionError:
print("订单号:" + bill[9] + "发送失败")
continue
else:
same_nums += 1
else:
for bill in update_data:
if bill[9].replace('\t', '').replace(' ', '') not in bill_list:
body['properties']['日期']['date']['start'] = bill[0] + '+08:00'
body['properties']['星期']['select']['name'] = week_list[
datetime.strptime(bill[0], "%Y-%m-%d %H:%M:%S").weekday()]
body['properties']['账单信息']['title'][0]['text']['content'] = bill[4]
body['properties']['金额']['number'] = float(bill[6].replace('¥', ''))
body['properties']['交易平台']['select']['name'] = platform
body['properties']['交易类型']['select']['name'] = bill[5]
body['properties']['平台交易单号']['rich_text'][0]['text']['content'] = bill[9].replace('\t',
'').replace(' ',
'')
if bill[5] == '支出':
body['properties']['交易方式']['rich_text'][0]['text']['content'] = bill[7]
else:
body['properties']['交易方式']['rich_text'][0]['text']['content'] = bill[8]
try:
send_data = requests.request(
'POST',
'https://api.notion.com/v1/pages',
json=body,
headers={'Authorization': 'Bearer ' + token, 'Notion-Version': '2021-05-13'},
)
if debug:
print(send_data.text)
nums += 1
except requests.exceptions.ConnectionError:
print("订单号:" + bill[9] + "发送失败")
continue
else:
same_nums += 1
send_email(server=server, subject="同步完成", content=platform + "同步完成\n本次同步了" + str(nums) + "条数据")
print("成功同步" + str(nums) + "条数据")
print("重复数据" + str(same_nums) + "条")
archive_bill(platform)
return 0