forked from malinkang/weread2notion-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweread.py
265 lines (242 loc) · 8.83 KB
/
weread.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from weread2notionpro.notion_helper import NotionHelper
from weread2notionpro.weread_api import WeReadApi
from weread2notionpro.utils import (
get_block,
get_heading,
get_number,
get_number_from_result,
get_quote,
get_rich_text_from_result,
get_table_of_contents,
)
def get_bookmark_list(page_id, bookId):
"""获取我的划线"""
filter = {
"and": [
{"property": "书籍", "relation": {"contains": page_id}},
{"property": "blockId", "rich_text": {"is_not_empty": True}},
]
}
results = notion_helper.query_all_by_book(
notion_helper.bookmark_database_id, filter
)
dict1 = {
get_rich_text_from_result(x, "bookmarkId"): get_rich_text_from_result(
x, "blockId"
)
for x in results
}
dict2 = {get_rich_text_from_result(x, "blockId"): x.get("id") for x in results}
bookmarks = weread_api.get_bookmark_list(bookId)
for i in bookmarks:
if i.get("bookmarkId") in dict1:
i["blockId"] = dict1.pop(i.get("bookmarkId"))
for blockId in dict1.values():
notion_helper.delete_block(blockId)
notion_helper.delete_block(dict2.get(blockId))
return bookmarks
def get_review_list(page_id,bookId):
"""获取笔记"""
filter = {
"and": [
{"property": "书籍", "relation": {"contains": page_id}},
{"property": "blockId", "rich_text": {"is_not_empty": True}},
]
}
results = notion_helper.query_all_by_book(notion_helper.review_database_id, filter)
dict1 = {
get_rich_text_from_result(x, "reviewId"): get_rich_text_from_result(
x, "blockId"
)
for x in results
}
dict2 = {get_rich_text_from_result(x, "blockId"): x.get("id") for x in results}
reviews = weread_api.get_review_list(bookId)
for i in reviews:
if i.get("reviewId") in dict1:
i["blockId"] = dict1.pop(i.get("reviewId"))
for blockId in dict1.values():
notion_helper.delete_block(blockId)
notion_helper.delete_block(dict2.get(blockId))
return reviews
def check(bookId):
"""检查是否已经插入过"""
filter = {"property": "BookId", "rich_text": {"equals": bookId}}
response = notion_helper.query(
database_id=notion_helper.book_database_id, filter=filter
)
if len(response["results"]) > 0:
return response["results"][0]["id"]
return None
def get_sort():
"""获取database中的最新时间"""
filter = {"property": "Sort", "number": {"is_not_empty": True}}
sorts = [
{
"property": "Sort",
"direction": "descending",
}
]
response = notion_helper.query(
database_id=notion_helper.book_database_id,
filter=filter,
sorts=sorts,
page_size=1,
)
if len(response.get("results")) == 1:
return response.get("results")[0].get("properties").get("Sort").get("number")
return 0
def sort_notes(page_id, chapter, bookmark_list):
"""对笔记进行排序"""
bookmark_list = sorted(
bookmark_list,
key=lambda x: (
x.get("chapterUid", 1),
0
if (x.get("range", "") == "" or x.get("range").split("-")[0] == "")
else int(x.get("range").split("-")[0]),
),
)
notes = []
if chapter != None:
filter = {"property": "书籍", "relation": {"contains": page_id}}
results = notion_helper.query_all_by_book(
notion_helper.chapter_database_id, filter
)
dict1 = {
get_number_from_result(x, "chapterUid"): get_rich_text_from_result(
x, "blockId"
)
for x in results
}
dict2 = {get_rich_text_from_result(x, "blockId"): x.get("id") for x in results}
d = {}
for data in bookmark_list:
chapterUid = data.get("chapterUid", 1)
if chapterUid not in d:
d[chapterUid] = []
d[chapterUid].append(data)
for key, value in d.items():
if key in chapter:
if key in dict1:
chapter.get(key)["blockId"] = dict1.pop(key)
notes.append(chapter.get(key))
notes.extend(value)
for blockId in dict1.values():
notion_helper.delete_block(blockId)
notion_helper.delete_block(dict2.get(blockId))
else:
notes.extend(bookmark_list)
return notes
def append_blocks(id, contents):
print(f"笔记数{len(contents)}")
before_block_id = ""
block_children = notion_helper.get_block_children(id)
if len(block_children) > 0 and block_children[0].get("type") == "table_of_contents":
before_block_id = block_children[0].get("id")
else:
response = notion_helper.append_blocks(
block_id=id, children=[get_table_of_contents()]
)
before_block_id = response.get("results")[0].get("id")
blocks = []
sub_contents = []
l = []
for content in contents:
if len(blocks) == 100:
results = append_blocks_to_notion(id, blocks, before_block_id, sub_contents)
before_block_id = results[-1].get("blockId")
l.extend(results)
blocks.clear()
sub_contents.clear()
if not notion_helper.sync_bookmark and content.get("type")==0:
continue
blocks.append(content_to_block(content))
sub_contents.append(content)
elif "blockId" in content:
if len(blocks) > 0:
l.extend(
append_blocks_to_notion(id, blocks, before_block_id, sub_contents)
)
blocks.clear()
sub_contents.clear()
before_block_id = content["blockId"]
else:
if not notion_helper.sync_bookmark and content.get("type")==0:
continue
blocks.append(content_to_block(content))
sub_contents.append(content)
if len(blocks) > 0:
l.extend(append_blocks_to_notion(id, blocks, before_block_id, sub_contents))
for index, value in enumerate(l):
print(f"正在插入第{index+1}条笔记,共{len(l)}条")
if "bookmarkId" in value:
notion_helper.insert_bookmark(id, value)
elif "reviewId" in value:
notion_helper.insert_review(id, value)
else:
notion_helper.insert_chapter(id, value)
def content_to_block(content):
if "bookmarkId" in content:
return get_block(
content.get("markText",""),
notion_helper.block_type,
notion_helper.show_color,
content.get("style"),
content.get("colorStyle"),
content.get("reviewId"),
)
elif "reviewId" in content:
return get_block(
content.get("content",""),
notion_helper.block_type,
notion_helper.show_color,
content.get("style"),
content.get("colorStyle"),
content.get("reviewId"),
)
else:
return get_heading(content.get("level"), content.get("title"))
def append_blocks_to_notion(id, blocks, after, contents):
response = notion_helper.append_blocks_after(
block_id=id, children=blocks, after=after
)
results = response.get("results")
l = []
for index, content in enumerate(contents):
result = results[index]
if content.get("abstract") != None and content.get("abstract") != "":
notion_helper.append_blocks(
block_id=result.get("id"), children=[get_quote(content.get("abstract"))]
)
content["blockId"] = result.get("id")
l.append(content)
return l
weread_api = WeReadApi()
notion_helper = NotionHelper()
def main():
notion_books = notion_helper.get_all_book()
books = weread_api.get_notebooklist()
if books != None:
for index, book in enumerate(books):
bookId = book.get("bookId")
title = book.get("book").get("title")
sort = book.get("sort")
if bookId not in notion_books:
continue
if sort == notion_books.get(bookId).get("Sort"):
continue
pageId = notion_books.get(bookId).get("pageId")
print(f"正在同步《{title}》,一共{len(books)}本,当前是第{index+1}本。")
chapter = weread_api.get_chapter_info(bookId)
bookmark_list = get_bookmark_list(pageId, bookId)
reviews = get_review_list(pageId,bookId)
bookmark_list.extend(reviews)
content = sort_notes(pageId, chapter, bookmark_list)
append_blocks(pageId, content)
properties = {
"Sort":get_number(sort)
}
notion_helper.update_book_page(page_id=pageId,properties=properties)
if __name__ == "__main__":
main()