forked from malinkang/weread2notion-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
195 lines (147 loc) · 4.92 KB
/
utils.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
import calendar
from datetime import datetime
from datetime import timedelta
MAX_LENGTH = 1024 # NOTION 2000个字符限制https://developers.notion.com/reference/request-limits
def get_heading(level, content):
if level == 1:
heading = "heading_1"
elif level == 2:
heading = "heading_2"
else:
heading = "heading_3"
return {
"type": heading,
heading: {
"rich_text": [
{
"type": "text",
"text": {
"content": content[:MAX_LENGTH],
},
}
],
"color": "default",
"is_toggleable": False,
},
}
def get_table_of_contents():
"""获取目录"""
return {"type": "table_of_contents", "table_of_contents": {"color": "default"}}
def get_title(content):
return {"title": [{"type": "text", "text": {"content": content[:MAX_LENGTH]}}]}
def get_rich_text(content):
return {"rich_text": [{"type": "text", "text": {"content": content[:MAX_LENGTH]}}]}
def get_url(url):
return {"url": url}
def get_file(url):
return {"files": [{"type": "external", "name": "Cover", "external": {"url": url}}]}
def get_multi_select(names):
return {"multi_select": [{"name": name} for name in names]}
def get_relation(ids):
return {"relation": [{"id": id} for id in ids]}
def get_date(start,end=None):
return {
"date": {
"start": start,
"end":end,
"time_zone": "Asia/Shanghai",
}
}
def get_icon(url):
return {"type": "external", "external": {"url": url}}
def get_select(name):
return {"select": {"name": name}}
def get_number(number):
return {"number": number}
def get_quote(content):
return {
"type": "quote",
"quote": {
"rich_text": [
{
"type": "text",
"text": {"content": content[:MAX_LENGTH]},
}
],
"color": "default",
},
}
def get_callout(content, style, colorStyle, reviewId):
# 根据不同的划线样式设置不同的emoji 直线type=0 背景颜色是1 波浪线是2
emoji = "〰️"
if style == 0:
emoji = "💡"
elif style == 1:
emoji = "⭐"
# 如果reviewId不是空说明是笔记
if reviewId != None:
emoji = "✍️"
color = "default"
# 根据划线颜色设置文字的颜色
if colorStyle == 1:
color = "red"
elif colorStyle == 2:
color = "purple"
elif colorStyle == 3:
color = "blue"
elif colorStyle == 4:
color = "green"
elif colorStyle == 5:
color = "yellow"
return {
"type": "callout",
"callout": {
"rich_text": [
{
"type": "text",
"text": {
"content": content[:MAX_LENGTH],
},
}
],
"icon": {"emoji": emoji},
"color": color,
},
}
def get_rich_text_from_result(result, name):
return result.get("properties").get(name).get("rich_text")[0].get("plain_text")
def get_number_from_result(result, name):
return result.get("properties").get(name).get("number")
def format_time(time):
"""将秒格式化为 xx时xx分格式"""
result = ""
hour = time // 3600
if hour > 0:
result += f"{hour}时"
minutes = time % 3600 // 60
if minutes > 0:
result += f"{minutes}分"
return result
def format_date(date,format="%Y-%m-%d %H:%M:%S"):
return date.strftime(format)
def timestamp_to_date(timestamp):
"""时间戳转化为date"""
return datetime.utcfromtimestamp(timestamp) + timedelta(hours=8)
def get_first_and_last_day_of_month(date):
# 获取给定日期所在月的第一天
first_day = date.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
# 获取给定日期所在月的最后一天
_, last_day_of_month = calendar.monthrange(date.year, date.month)
last_day = date.replace(
day=last_day_of_month, hour=0, minute=0, second=0, microsecond=0
)
return first_day, last_day
def get_first_and_last_day_of_year(date):
# 获取给定日期所在年的第一天
first_day = date.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
# 获取给定日期所在年的最后一天
last_day = date.replace(month=12, day=31, hour=0, minute=0, second=0, microsecond=0)
return first_day, last_day
def get_first_and_last_day_of_week(date):
# 获取给定日期所在周的第一天(星期一)
first_day_of_week = (date - timedelta(days=date.weekday())).replace(
hour=0, minute=0, second=0, microsecond=0
)
# 获取给定日期所在周的最后一天(星期日)
last_day_of_week = first_day_of_week + timedelta(days=6)
return first_day_of_week, last_day_of_week