Skip to content

Commit 9a5dfd4

Browse files
author
xiangshan
committed
适配life os 1.1
1 parent 046e4e0 commit 9a5dfd4

File tree

4 files changed

+125
-61
lines changed

4 files changed

+125
-61
lines changed

scripts/book.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def insert_book_to_notion(books, index, bookId):
5151
book["书架分类"] = archive_dict.get(bookId)
5252
if bookId in notion_books:
5353
book.update(notion_books.get(bookId))
54+
5455
bookInfo = weread_api.get_bookinfo(bookId)
5556
if bookInfo != None:
5657
book.update(bookInfo)
@@ -65,7 +66,7 @@ def insert_book_to_notion(books, index, bookId):
6566
markedStatus = book.get("markedStatus")
6667
status = "想读"
6768
if markedStatus == 4:
68-
status = "已读"
69+
status = "阅读完"
6970
elif book.get("readingTime", 0) >= 60:
7071
status = "在读"
7172
book["阅读状态"] = status
@@ -101,23 +102,23 @@ def insert_book_to_notion(books, index, bookId):
101102
book["BookId"] = book.get("bookId")
102103
book["ISBN"] = book.get("isbn")
103104
book["链接"] = utils.get_weread_url(bookId)
104-
book["简介"] = book.get("intro")
105+
#book["简介"] = book.get("intro")
105106
book["作者"] = [
106107
notion_helper.get_relation_id(
107108
x, notion_helper.author_database_id, USER_ICON_URL
108109
)
109110
for x in book.get("author").split(" ")
110111
]
111-
if book.get("categories"):
112-
book["分类"] = [
113-
notion_helper.get_relation_id(
114-
x.get("title"), notion_helper.category_database_id, TAG_ICON_URL
115-
)
116-
for x in book.get("categories")
117-
]
112+
#if book.get("categories"):
113+
# book["分类"] = [
114+
# notion_helper.get_relation_id(
115+
# x.get("title"), notion_helper.category_database_id, TAG_ICON_URL
116+
# )
117+
# for x in book.get("categories")
118+
# ]
118119
properties = utils.get_properties(book, book_properties_type_dict)
119120

120-
print(f"正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。")
121+
#print(f"正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。")
121122
if not book.get("readDetail") or not book.get("readDetail").get("data"):
122123
print(f"《{book.get('title')}》没有阅读记录,跳过")
123124
return
@@ -207,7 +208,11 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
207208
notion_helper = NotionHelper()
208209
notion_books = notion_helper.get_all_book()
209210
bookshelf_books = weread_api.get_bookshelf()
210-
# 获取书架上的图书信息
211+
# 有阅读记录的图书信息
212+
# 样例数据:{'$bookId': {'bookId': '26062915', 'progress': 1, 'chapterUid': 8, 'chapterOffset': 0,
213+
# 'chapterIdx': 8, 'appId': '11413501', 'updateTime': 1691221509, 'readingTime': 875,
214+
# 'syncKey': 720606794}
215+
# }
211216
bookProgress = bookshelf_books.get("bookProgress")
212217
bookProgress = {book.get("bookId"): book for book in bookProgress}
213218
archive_dict = {}
@@ -217,31 +222,58 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
217222
bookIds = archive.get("bookIds")
218223
archive_dict.update({bookId: name for bookId in bookIds})
219224
not_need_sync = []
225+
# Notion里的文献笔记
226+
# 样例数据:{'$bookId': {'pageId': '10c911dc-da56-8106-8b58-ddaacc287c74', 'readingTime': 2325,
227+
# 'category': None, 'Sort': None, 'douban_url': None,
228+
# 'cover': {'type': 'external', 'external': {'url': 'https://bts-image.xyzcdn.net/aHR0cHM6Ly9pbWFnZS54eXpjZG4ubmV0L0ZxUWs2VThtWDU0YnZ3MFBsbm5HemtHMVpEajkuanBn.jpg'}},
229+
# 'myRating': None, 'comment': None, 'status': '阅读完'}
230+
# }
220231
for key, value in notion_books.items():
221232
if (
222233
(
223234
key not in bookProgress
224235
or value.get("readingTime") == bookProgress.get(key).get("readingTime")
225236
)
226237
and (archive_dict.get(key) == value.get("category"))
227-
and (value.get("cover") is not None)
228-
and (
229-
value.get("status") != "已读"
230-
or (value.get("status") == "已读" and value.get("myRating"))
231-
)
238+
#and (value.get("cover") is not None)
239+
#and (
240+
# value.get("status") != "已读"
241+
# or (value.get("status") == "已读" and value.get("myRating"))
242+
#)
232243
):
233244
#这里判定Notion中的书籍和微信中的书籍是否有属性发现变化,没有的话就不需要同步了
234245
not_need_sync.append(key)
235246
#continue
236247
notebooks = weread_api.get_notebooklist()
248+
notebooks_map = {d["bookId"] : d for d in notebooks if "bookId" in d}
237249
notebooks = [d["bookId"] for d in notebooks if "bookId" in d]
250+
238251
books = bookshelf_books.get("books")
252+
bookshelf_map = {d["bookId"] : d for d in books if "bookId" in d}
239253
books = [d["bookId"] for d in books if "bookId" in d]
254+
255+
240256
books = list((set(notebooks) | set(books)) - set(not_need_sync))
257+
len = len(books)
241258
for index, bookId in enumerate(books):
242259
try:
260+
bp = bookProgress[bookId] if bookId in bookProgress else None
261+
bs = bookshelf_map[bookId] if bookId in bookshelf_map else None
262+
nt = notebooks_map[bookId] if bookId in notebooks_map else None
263+
264+
title = bs['title'] if bs and 'title' in bs else ""
265+
print(f"正在插入《{title}》,一共{len}本,当前是第{index + 1}本。")
266+
267+
if bp and bp['readingTime'] < 600:
268+
print(f"《{title}》阅读不超时10分钟,跳过")
269+
continue
270+
271+
if not nt:
272+
print(f"《{title}》没有阅读笔记,跳过")
273+
continue
274+
243275
insert_book_to_notion(books, index, bookId)
244276
except Exception as e:
245277
print("处理book: " + bookId + "出现异常,跳过:")
246278
traceback.print_exc()
247-
continue
279+
continue

scripts/notion_helper.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from notion_client import Client
77
from retrying import retry
8-
from datetime import timedelta
8+
from datetime import timedelta, datetime
99
from dotenv import load_dotenv
1010
from utils import (
1111
format_date,
@@ -37,9 +37,9 @@ class NotionHelper:
3737
"BOOK_DATABASE_NAME": "文献笔记",
3838
"REVIEW_DATABASE_NAME": "笔记",
3939
"BOOKMARK_DATABASE_NAME": "划线",
40-
"DAY_DATABASE_NAME": "每日工作",
41-
"WEEK_DATABASE_NAME": "每周工作",
42-
"MONTH_DATABASE_NAME": "每月工作",
40+
"DAY_DATABASE_NAME": "",
41+
"WEEK_DATABASE_NAME": "",
42+
"MONTH_DATABASE_NAME": "",
4343
"YEAR_DATABASE_NAME": "年",
4444
"CATEGORY_DATABASE_NAME": "分类",
4545
"AUTHOR_DATABASE_NAME": "作者",
@@ -50,7 +50,7 @@ class NotionHelper:
5050
heatmap_block_id = None
5151

5252
def __init__(self):
53-
# os.environ['NOTION_PAGE'] = 'd91e1d17-1a03-4165-af8c-7cf49e185dcd'
53+
print("notion helper开始初始化", datetime.now())
5454
self.client = Client(auth=os.getenv("NOTION_TOKEN"), log_level=logging.ERROR)
5555
self.__cache = {}
5656
self.page_id = self.extract_page_id(os.getenv("NOTION_PAGE"))
@@ -94,6 +94,7 @@ def __init__(self):
9494
self.update_book_database()
9595
if self.read_database_id is None:
9696
self.create_database()
97+
print("notion helper完成初始化", datetime.now())
9798

9899
def extract_page_id(self, notion_url):
99100
# 正则表达式匹配 32 个字符的 Notion page_id
@@ -223,10 +224,9 @@ def get_day_relation_id(self, date):
223224
day = new_date.strftime("%Y-%m-%d")
224225

225226
properties = {}
226-
return self.get_relation_id(
227-
day, self.day_database_id, DATE_EMOJ_ICON, properties
227+
return self.get_reltion_id_by_property(
228+
"【兼容】日期", day, "date", self.day_database_id, DATE_EMOJ_ICON, properties
228229
)
229-
230230
def get_day_relation_id_old(self, date):
231231
new_date = date.replace(hour=0, minute=0, second=0, microsecond=0)
232232
timestamp = (new_date - timedelta(hours=8)).timestamp()
@@ -271,6 +271,25 @@ def get_relation_id(self, name, id, icon, properties={}):
271271
self.__cache[key] = page_id
272272
return page_id
273273

274+
def get_reltion_id_by_property(self, property_name, property_value, property_type,
275+
id, icon, properties = {}):
276+
key = f"{id}{property_name}-{property_value}"
277+
if key in self.__cache:
278+
return self.__cache.get(key)
279+
filter = {"property": property_name, property_type: {"equals": property_value}}
280+
response = self.client.databases.query(database_id=id, filter=filter)
281+
if len(response.get("results")) == 0:
282+
raise Exception("未找到该日期")
283+
#parent = {"database_id": id, "type": "database_id"}
284+
#properties["标题"] = get_title(name)
285+
#page_id = self.client.pages.create(
286+
# parent=parent, properties=properties, icon=get_icon(icon)
287+
#).get("id")
288+
else:
289+
page_id = response.get("results")[0].get("id")
290+
self.__cache[key] = page_id
291+
return page_id
292+
274293
def insert_bookmark(self, id, bookmark):
275294
icon = get_icon(BOOKMARK_ICON_URL)
276295
properties = {
@@ -382,7 +401,9 @@ def delete_block(self, block_id):
382401
@retry(stop_max_attempt_number=3, wait_fixed=5000)
383402
def get_all_book(self):
384403
"""从Notion中获取所有的书籍"""
404+
print("从notion拉取所有书籍开始: ", datetime.now())
385405
results = self.query_all(self.book_database_id)
406+
#filter = {"property": "类型", "relation": {"contains": page_id}}
386407
books_dict = {}
387408
for result in results:
388409
bookId = get_property_value(result.get("properties").get("BookId"))
@@ -398,13 +419,14 @@ def get_all_book(self):
398419
"douban_url": get_property_value(
399420
result.get("properties").get("豆瓣链接")
400421
),
401-
"cover": result.get("cover"),
422+
"cover": result.get("cover"),#Notion里已无这个字段,
402423
"myRating": get_property_value(
403424
result.get("properties").get("我的评分")
404425
),
405426
"comment": get_property_value(result.get("properties").get("豆瓣短评")),
406427
"status": get_property_value(result.get("properties").get("阅读状态")),
407428
}
429+
print("从notion拉取所有书籍结束: ", datetime.now())
408430
return books_dict
409431

410432
@retry(stop_max_attempt_number=3, wait_fixed=5000)
@@ -431,6 +453,7 @@ def query_all(self, database_id):
431453
has_more = True
432454
start_cursor = None
433455
while has_more:
456+
print("从notion拉取数据...", datetime.now())
434457
response = self.client.databases.query(
435458
database_id=database_id,
436459
start_cursor=start_cursor,

scripts/read_time.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,23 @@ def get_file():
8282

8383
HEATMAP_GUIDE = "https://mp.weixin.qq.com/s?__biz=MzI1OTcxOTI4NA==&mid=2247484145&idx=1&sn=81752852420b9153fc292b7873217651&chksm=ea75ebeadd0262fc65df100370d3f983ba2e52e2fcde2deb1ed49343fbb10645a77570656728&token=157143379&lang=zh_CN#rd"
8484
if __name__ == "__main__":
85+
os.environ['NOTION_PAGE'] = 'Life-OS-System-81b25bbd21684a3c943eb03a44b2f900'
86+
os.environ['NOTION_TOKEN'] = 'secret_LAVFOz2S74ztgXTaDPuYKEjGEKPtflnk5IV0DbzW4vd'
87+
os.environ['WEREAD_COOKIE'] = 'ptcz=754d621bb3cc45abb68f07d842270b8db5224d5c18bc38c78d239f19edb36567; pgv_pvid=6811236617; _qimei_uuid42=17c02151133100c79dcf768330e7e363052c0fa717; _qimei_q36=; wr_gid=273651192; wr_theme=white; RK=QHthLXYzU2; pac_uid=0_3fbe47a232d9f; iip=0; suid=ek171315182328217824; wr_vid=68910221; wr_pf=0; wr_rt=web%40xEmeptoh5f57cXUSQSQ_AL; wr_localvid=be8325e0741b7c8dbe86f3b; wr_name=Easonlee; wr_gender=1; wr_avatar=https%3A%2F%2Fwx.qlogo.cn%2Fmmhead%2FcyiaTKm65RvUN6Mnm27TwKefnx3fCSsEClhwOuysFbHw%2F0; qq_domain_video_guid_verify=aef172aa1a598b90; _ga=GA1.2.1031625882.1723940499; _ga_8YVFNWD1KC=GS1.2.1723940499.1.1.1723940531.0.0.0; _clck=3911196076|1|fon|0; _qimei_fingerprint=88a18db09377adee6270beae94358cff; _qimei_h38=b115791b9dcf768330e7e36303000006617c02; wr_fp=25923672; wr_skey=7SD1H2_a'
8588
notion_helper = NotionHelper()
8689
weread_api = WeReadApi()
87-
image_file = get_file()
88-
if image_file:
89-
image_url = f"https://raw.githubusercontent.com/{os.getenv('REPOSITORY')}/{os.getenv('REF').split('/')[-1]}/OUT_FOLDER/{image_file}"
90-
heatmap_url = f"https://heatmap.malinkang.com/?image={image_url}"
91-
if notion_helper.heatmap_block_id:
92-
response = notion_helper.update_heatmap(
93-
block_id=notion_helper.heatmap_block_id, url=heatmap_url
94-
)
95-
else:
96-
print(f"更新热力图失败,没有添加热力图占位。具体参考:{HEATMAP_GUIDE}")
97-
else:
98-
print(f"更新热力图失败,没有生成热力图。具体参考:{HEATMAP_GUIDE}")
90+
#image_file = get_file()
91+
#if image_file:
92+
# image_url = f"https://raw.githubusercontent.com/{os.getenv('REPOSITORY')}/{os.getenv('REF').split('/')[-1]}/OUT_FOLDER/{image_file}"
93+
# heatmap_url = f"https://heatmap.malinkang.com/?image={image_url}"
94+
# if notion_helper.heatmap_block_id:
95+
# response = notion_helper.update_heatmap(
96+
# block_id=notion_helper.heatmap_block_id, url=heatmap_url
97+
# )
98+
# else:
99+
# print(f"更新热力图失败,没有添加热力图占位。具体参考:{HEATMAP_GUIDE}")
100+
#else:
101+
# print(f"更新热力图失败,没有生成热力图。具体参考:{HEATMAP_GUIDE}")
99102
api_data = weread_api.get_api_data()
100103
readTimes = {int(key): value for key, value in api_data.get("readTimes").items()}
101104
now = pendulum.now("Asia/Shanghai").start_of("day")

0 commit comments

Comments
 (0)