Skip to content

Commit

Permalink
适配life os 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeos committed Oct 23, 2024
1 parent 046e4e0 commit 73b4390
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 61 deletions.
66 changes: 49 additions & 17 deletions scripts/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def insert_book_to_notion(books, index, bookId):
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)
Expand All @@ -65,7 +66,7 @@ def insert_book_to_notion(books, index, bookId):
markedStatus = book.get("markedStatus")
status = "想读"
if markedStatus == 4:
status = "已读"
status = "阅读完"
elif book.get("readingTime", 0) >= 60:
status = "在读"
book["阅读状态"] = status
Expand Down Expand Up @@ -101,23 +102,23 @@ def insert_book_to_notion(books, index, bookId):
book["BookId"] = book.get("bookId")
book["ISBN"] = book.get("isbn")
book["链接"] = utils.get_weread_url(bookId)
book["简介"] = book.get("intro")
#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")
]
#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)

print(f"正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。")
#print(f"正在插入《{book.get('title')}》,一共{len(books)}本,当前是第{index+1}本。")
if not book.get("readDetail") or not book.get("readDetail").get("data"):
print(f"《{book.get('title')}》没有阅读记录,跳过")
return
Expand Down Expand Up @@ -207,7 +208,11 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
notion_helper = NotionHelper()
notion_books = notion_helper.get_all_book()
bookshelf_books = weread_api.get_bookshelf()
# 获取书架上的图书信息
# 有阅读记录的图书信息
# 样例数据:{'$bookId': {'bookId': '26062915', 'progress': 1, 'chapterUid': 8, 'chapterOffset': 0,
# 'chapterIdx': 8, 'appId': '11413501', 'updateTime': 1691221509, 'readingTime': 875,
# 'syncKey': 720606794}
# }
bookProgress = bookshelf_books.get("bookProgress")
bookProgress = {book.get("bookId"): book for book in bookProgress}
archive_dict = {}
Expand All @@ -217,31 +222,58 @@ def insert_to_notion(page_id, timestamp, duration, book_database_id):
bookIds = archive.get("bookIds")
archive_dict.update({bookId: name for bookId in bookIds})
not_need_sync = []
# Notion里的文献笔记
# 样例数据:{'$bookId': {'pageId': '10c911dc-da56-8106-8b58-ddaacc287c74', 'readingTime': 2325,
# 'category': None, 'Sort': None, 'douban_url': None,
# 'cover': {'type': 'external', 'external': {'url': 'https://bts-image.xyzcdn.net/aHR0cHM6Ly9pbWFnZS54eXpjZG4ubmV0L0ZxUWs2VThtWDU0YnZ3MFBsbm5HemtHMVpEajkuanBn.jpg'}},
# 'myRating': None, 'comment': None, 'status': '阅读完'}
# }
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"))
)
#and (value.get("cover") is not None)
#and (
# value.get("status") != "已读"
# or (value.get("status") == "已读" and value.get("myRating"))
#)
):
#这里判定Notion中的书籍和微信中的书籍是否有属性发现变化,没有的话就不需要同步了
not_need_sync.append(key)
#continue
notebooks = weread_api.get_notebooklist()
notebooks_map = {d["bookId"] : d for d in notebooks if "bookId" in d}
notebooks = [d["bookId"] for d in notebooks if "bookId" in d]

books = bookshelf_books.get("books")
bookshelf_map = {d["bookId"] : d for d in books if "bookId" in d}
books = [d["bookId"] for d in books if "bookId" in d]


books = list((set(notebooks) | set(books)) - set(not_need_sync))
len = len(books)
for index, bookId in enumerate(books):
try:
bp = bookProgress[bookId] if bookId in bookProgress else None
bs = bookshelf_map[bookId] if bookId in bookshelf_map else None
nt = notebooks_map[bookId] if bookId in notebooks_map else None

title = bs['title'] if bs and 'title' in bs else ""
print(f"正在插入《{title}》,一共{len}本,当前是第{index + 1}本。")

if bp and bp['readingTime'] < 600:
print(f"《{title}》阅读不超时10分钟,跳过")
continue

if not nt:
print(f"《{title}》没有阅读笔记,跳过")
continue

insert_book_to_notion(books, index, bookId)
except Exception as e:
print("处理book: " + bookId + "出现异常,跳过:")
traceback.print_exc()
continue
continue
41 changes: 32 additions & 9 deletions scripts/notion_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from notion_client import Client
from retrying import retry
from datetime import timedelta
from datetime import timedelta, datetime
from dotenv import load_dotenv
from utils import (
format_date,
Expand Down Expand Up @@ -37,9 +37,9 @@ class NotionHelper:
"BOOK_DATABASE_NAME": "文献笔记",
"REVIEW_DATABASE_NAME": "笔记",
"BOOKMARK_DATABASE_NAME": "划线",
"DAY_DATABASE_NAME": "每日工作",
"WEEK_DATABASE_NAME": "每周工作",
"MONTH_DATABASE_NAME": "每月工作",
"DAY_DATABASE_NAME": "",
"WEEK_DATABASE_NAME": "",
"MONTH_DATABASE_NAME": "",
"YEAR_DATABASE_NAME": "年",
"CATEGORY_DATABASE_NAME": "分类",
"AUTHOR_DATABASE_NAME": "作者",
Expand All @@ -50,7 +50,7 @@ class NotionHelper:
heatmap_block_id = None

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

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

properties = {}
return self.get_relation_id(
day, self.day_database_id, DATE_EMOJ_ICON, properties
return self.get_reltion_id_by_property(
"【兼容】日期", day, "date", self.day_database_id, DATE_EMOJ_ICON, properties
)

def get_day_relation_id_old(self, date):
new_date = date.replace(hour=0, minute=0, second=0, microsecond=0)
timestamp = (new_date - timedelta(hours=8)).timestamp()
Expand Down Expand Up @@ -271,6 +271,25 @@ def get_relation_id(self, name, id, icon, properties={}):
self.__cache[key] = page_id
return page_id

def get_reltion_id_by_property(self, property_name, property_value, property_type,
id, icon, properties = {}):
key = f"{id}{property_name}-{property_value}"
if key in self.__cache:
return self.__cache.get(key)
filter = {"property": property_name, property_type: {"equals": property_value}}
response = self.client.databases.query(database_id=id, filter=filter)
if len(response.get("results")) == 0:
raise Exception("未找到该日期")
#parent = {"database_id": id, "type": "database_id"}
#properties["标题"] = get_title(name)
#page_id = self.client.pages.create(
# parent=parent, properties=properties, icon=get_icon(icon)
#).get("id")
else:
page_id = response.get("results")[0].get("id")
self.__cache[key] = page_id
return page_id

def insert_bookmark(self, id, bookmark):
icon = get_icon(BOOKMARK_ICON_URL)
properties = {
Expand Down Expand Up @@ -382,7 +401,9 @@ def delete_block(self, block_id):
@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_all_book(self):
"""从Notion中获取所有的书籍"""
print("从notion拉取所有书籍开始: ", datetime.now())
results = self.query_all(self.book_database_id)
#filter = {"property": "类型", "relation": {"contains": page_id}}
books_dict = {}
for result in results:
bookId = get_property_value(result.get("properties").get("BookId"))
Expand All @@ -398,13 +419,14 @@ def get_all_book(self):
"douban_url": get_property_value(
result.get("properties").get("豆瓣链接")
),
"cover": result.get("cover"),
"cover": result.get("cover"),#Notion里已无这个字段,
"myRating": get_property_value(
result.get("properties").get("我的评分")
),
"comment": get_property_value(result.get("properties").get("豆瓣短评")),
"status": get_property_value(result.get("properties").get("阅读状态")),
}
print("从notion拉取所有书籍结束: ", datetime.now())
return books_dict

@retry(stop_max_attempt_number=3, wait_fixed=5000)
Expand All @@ -431,6 +453,7 @@ def query_all(self, database_id):
has_more = True
start_cursor = None
while has_more:
print("从notion拉取数据...", datetime.now())
response = self.client.databases.query(
database_id=database_id,
start_cursor=start_cursor,
Expand Down
27 changes: 15 additions & 12 deletions scripts/read_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,23 @@ def get_file():

HEATMAP_GUIDE = "https://mp.weixin.qq.com/s?__biz=MzI1OTcxOTI4NA==&mid=2247484145&idx=1&sn=81752852420b9153fc292b7873217651&chksm=ea75ebeadd0262fc65df100370d3f983ba2e52e2fcde2deb1ed49343fbb10645a77570656728&token=157143379&lang=zh_CN#rd"
if __name__ == "__main__":
os.environ['NOTION_PAGE'] = 'Life-OS-System-81b25bbd21684a3c943eb03a44b2f900'
os.environ['NOTION_TOKEN'] = 'secret_LAVFOz2S74ztgXTaDPuYKEjGEKPtflnk5IV0DbzW4vd'
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'
notion_helper = NotionHelper()
weread_api = WeReadApi()
image_file = get_file()
if image_file:
image_url = f"https://raw.githubusercontent.com/{os.getenv('REPOSITORY')}/{os.getenv('REF').split('/')[-1]}/OUT_FOLDER/{image_file}"
heatmap_url = f"https://heatmap.malinkang.com/?image={image_url}"
if notion_helper.heatmap_block_id:
response = notion_helper.update_heatmap(
block_id=notion_helper.heatmap_block_id, url=heatmap_url
)
else:
print(f"更新热力图失败,没有添加热力图占位。具体参考:{HEATMAP_GUIDE}")
else:
print(f"更新热力图失败,没有生成热力图。具体参考:{HEATMAP_GUIDE}")
#image_file = get_file()
#if image_file:
# image_url = f"https://raw.githubusercontent.com/{os.getenv('REPOSITORY')}/{os.getenv('REF').split('/')[-1]}/OUT_FOLDER/{image_file}"
# heatmap_url = f"https://heatmap.malinkang.com/?image={image_url}"
# if notion_helper.heatmap_block_id:
# response = notion_helper.update_heatmap(
# block_id=notion_helper.heatmap_block_id, url=heatmap_url
# )
# else:
# print(f"更新热力图失败,没有添加热力图占位。具体参考:{HEATMAP_GUIDE}")
#else:
# print(f"更新热力图失败,没有生成热力图。具体参考:{HEATMAP_GUIDE}")
api_data = weread_api.get_api_data()
readTimes = {int(key): value for key, value in api_data.get("readTimes").items()}
now = pendulum.now("Asia/Shanghai").start_of("day")
Expand Down
Loading

0 comments on commit 73b4390

Please sign in to comment.