55
66from notion_client import Client
77from retrying import retry
8- from datetime import timedelta
8+ from datetime import timedelta , datetime
99from dotenv import load_dotenv
1010from 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 ,
0 commit comments