forked from malinkang/weread2notion-pro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.py
194 lines (179 loc) · 7.38 KB
/
book.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
import pendulum
from weread2notionpro.notion_helper import NotionHelper
from weread2notionpro.weread_api import WeReadApi
from weread2notionpro import utils
from weread2notionpro.config import book_properties_type_dict, tz
TAG_ICON_URL = "https://www.notion.so/icons/tag_gray.svg"
USER_ICON_URL = "https://www.notion.so/icons/user-circle-filled_gray.svg"
BOOK_ICON_URL = "https://www.notion.so/icons/book_gray.svg"
rating = {"poor": "⭐️", "fair": "⭐️⭐️⭐️", "good": "⭐️⭐️⭐️⭐️⭐️"}
def insert_book_to_notion(books, index, bookId):
"""插入Book到Notion"""
book = {}
if bookId in archive_dict:
book["书架分类"] = archive_dict.get(bookId)
if bookId in notion_books:
book.update(notion_books.get(bookId))
bookInfo = weread_api.get_bookinfo(bookId)
if bookInfo != None:
book.update(bookInfo)
readInfo = weread_api.get_read_info(bookId)
# 研究了下这个状态不知道什么情况有的虽然读了状态还是1 markedStatus = 1 想读 4 读完 其他为在读
readInfo.update(readInfo.get("readDetail", {}))
readInfo.update(readInfo.get("bookInfo", {}))
book.update(readInfo)
book["阅读进度"] = (
100 if (book.get("markedStatus") == 4) else book.get("readingProgress", 0)
) / 100
markedStatus = book.get("markedStatus")
status = "想读"
if markedStatus == 4:
status = "已读"
elif book.get("readingTime", 0) >= 60:
status = "在读"
book["阅读状态"] = status
book["阅读时长"] = book.get("readingTime")
book["阅读天数"] = book.get("totalReadDay")
book["评分"] = book.get("newRating")
if book.get("newRatingDetail") and book.get("newRatingDetail").get("myRating"):
book["我的评分"] = rating.get(book.get("newRatingDetail").get("myRating"))
elif status == "已读":
book["我的评分"] = "未评分"
book["时间"] = (
book.get("finishedDate")
or book.get("lastReadingDate")
or book.get("readingBookDate")
)
book["开始阅读时间"] = book.get("beginReadingDate")
book["最后阅读时间"] = book.get("lastReadingDate")
cover = book.get("cover").replace("/s_", "/t7_")
if not cover or not cover.strip() or not cover.startswith("http"):
cover = BOOK_ICON_URL
if bookId not in notion_books:
book["书名"] = book.get("title")
book["BookId"] = book.get("bookId")
book["ISBN"] = book.get("isbn")
book["链接"] = weread_api.get_url(bookId)
book["简介"] = book.get("intro")
book["作者"] = [
notion_helper.get_relation_id(
x, notion_helper.author_database_id, USER_ICON_URL
)
for x in book.get("author").split(" ")
]
if book.get("categories"):
book["分类"] = [
notion_helper.get_relation_id(
x.get("title"), notion_helper.category_database_id, TAG_ICON_URL
)
for x in book.get("categories")
]
properties = utils.get_properties(book, book_properties_type_dict)
if book.get("时间"):
notion_helper.get_date_relation(
properties,
pendulum.from_timestamp(book.get("时间"), tz="Asia/Shanghai"),
)
print(
f"正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。"
)
parent = {"database_id": notion_helper.book_database_id, "type": "database_id"}
result = None
if bookId in notion_books:
result = notion_helper.update_page(
page_id=notion_books.get(bookId).get("pageId"),
properties=properties,
cover=utils.get_icon(cover),
)
else:
result = notion_helper.create_book_page(
parent=parent,
properties=properties,
icon=utils.get_icon(cover),
)
page_id = result.get("id")
if book.get("readDetail") and book.get("readDetail").get("data"):
data = book.get("readDetail").get("data")
data = {item.get("readDate"): item.get("readTime") for item in data}
insert_read_data(page_id, data)
def insert_read_data(page_id, readTimes):
readTimes = dict(sorted(readTimes.items()))
filter = {"property": "书架", "relation": {"contains": page_id}}
results = notion_helper.query_all_by_book(notion_helper.read_database_id, filter)
for result in results:
timestamp = result.get("properties").get("时间戳").get("number")
duration = result.get("properties").get("时长").get("number")
id = result.get("id")
if timestamp in readTimes:
value = readTimes.pop(timestamp)
if value != duration:
insert_to_notion(
page_id=id,
timestamp=timestamp,
duration=value,
book_database_id=page_id,
)
for key, value in readTimes.items():
insert_to_notion(None, int(key), value, page_id)
def insert_to_notion(page_id, timestamp, duration, book_database_id):
parent = {"database_id": notion_helper.read_database_id, "type": "database_id"}
properties = {
"标题": utils.get_title(
pendulum.from_timestamp(timestamp, tz=tz).to_date_string()
),
"日期": utils.get_date(
start=pendulum.from_timestamp(timestamp, tz=tz).format(
"YYYY-MM-DD HH:mm:ss"
)
),
"时长": utils.get_number(duration),
"时间戳": utils.get_number(timestamp),
"书架": utils.get_relation([book_database_id]),
}
if page_id != None:
notion_helper.client.pages.update(page_id=page_id, properties=properties)
else:
notion_helper.client.pages.create(
parent=parent,
icon=utils.get_icon("https://www.notion.so/icons/target_red.svg"),
properties=properties,
)
weread_api = WeReadApi()
notion_helper = NotionHelper()
archive_dict = {}
notion_books = {}
def main():
global notion_books
global archive_dict
bookshelf_books = weread_api.get_bookshelf()
notion_books = notion_helper.get_all_book()
bookProgress = bookshelf_books.get("bookProgress")
bookProgress = {book.get("bookId"): book for book in bookProgress}
for archive in bookshelf_books.get("archive"):
name = archive.get("name")
bookIds = archive.get("bookIds")
archive_dict.update({bookId: name for bookId in bookIds})
not_need_sync = []
for key, value in notion_books.items():
if (
(
key not in bookProgress
or value.get("readingTime") == bookProgress.get(key).get("readingTime")
)
and (archive_dict.get(key) == value.get("category"))
and (value.get("cover") is not None)
and (
value.get("status") != "已读"
or (value.get("status") == "已读" and value.get("myRating"))
)
):
not_need_sync.append(key)
notebooks = weread_api.get_notebooklist()
notebooks = [d["bookId"] for d in notebooks if "bookId" in d]
books = bookshelf_books.get("books")
books = [d["bookId"] for d in books if "bookId" in d]
books = list((set(notebooks) | set(books)) - set(not_need_sync))
for index, bookId in enumerate(books):
insert_book_to_notion(books, index, bookId)
if __name__ == "__main__":
main()