Skip to content

Commit

Permalink
重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
malinkang committed Jan 5, 2024
1 parent cabaa71 commit 408b998
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 52 deletions.
40 changes: 27 additions & 13 deletions .github/workflows/read_time.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
name: weread sync
name: read time sync

on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *"
- cron: "0 */3 * * *"
jobs:
sync:
name: Sync
runs-on: ubuntu-latest
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
BOOK_DATABASE_ID: ${{ secrets.BOOK_DATABASE_ID }}
AUTHOR_DATABASE_ID: ${{ secrets.AUTHOR_DATABASE_ID }}
CATEGORY_DATABASE_ID: ${{ secrets.CATEGORY_DATABASE_ID }}
BOOKMARK_DATABASE_ID: ${{ secrets.BOOKMARK_DATABASE_ID }}
REVIEW_DATABASE_ID: ${{ secrets.REVIEW_DATABASE_ID }}
CHAPTER_DATABASE_ID: ${{ secrets.CHAPTER_DATABASE_ID }}
YEAR_DATABASE_ID: ${{ secrets.YEAR_DATABASE_ID }}
WEEK_DATABASE_ID: ${{ secrets.WEEK_DATABASE_ID }}
MONTH_DATABASE_ID: ${{ secrets.MONTH_DATABASE_ID }}
DAY_DATABASE_ID: ${{ secrets.DAY_DATABASE_ID }}
NOTION_PAGE: ${{ secrets.NOTION_PAGE }}
WEREAD_COOKIE: ${{ secrets.WEREAD_COOKIE }}
BOOK_DATABASE_NAME: ${{ vars.BOOK_DATABASE_NAME }}
AUTHOR_DATABASE_NAME: ${{ vars.AUTHOR_DATABASE_NAME }}
CATEGORY_DATABASE_NAME: ${{ vars.CATEGORY_DATABASE_NAME }}
BOOKMARK_DATABASE_NAME: ${{ vars.BOOKMARK_DATABASE_NAME }}
REVIEW_DATABASE_NAME: ${{ vars.REVIEW_DATABASE_NAME }}
CHAPTER_DATABASE_NAME: ${{ vars.CHAPTER_DATABASE_NAME }}
YEAR_DATABASE_NAME: ${{ vars.YEAR_DATABASE_NAME }}
WEEK_DATABASE_NAME: ${{ vars.WEEK_DATABASE_NAME }}
MONTH_DATABASE_NAME: ${{ vars.MONTH_DATABASE_NAME }}
DAY_DATABASE_NAME: ${{ vars.DAY_DATABASE_NAME }}
REF: ${{ github.ref }}
REPOSITORY: ${{ github.repository }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -32,6 +35,17 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: weread heatmap
run: |
github_heatmap weread --weread_cookie "${{secrets.WEREAD_COOKIE}}" --year $(date +"%Y") --me "${{secrets.NAME}}" --with-animation --background-color=#ffffff --track-color=#ACE7AE --special-color1=#69C16E --special-color2=#549F57 --dom-color=#EBEDF0 --text-color=#000000
- name: push
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add .
git commit -m 'add new cover' || echo "nothing to commit"
git push || echo "nothing to push"
- name: read time sync
run: |
python -u scripts/read_time.py
python -u scripts/read_time.py
30 changes: 15 additions & 15 deletions .github/workflows/weread.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: weread sync
name: weread note sync

on:
workflow_dispatch:
Expand All @@ -10,17 +10,20 @@ jobs:
runs-on: ubuntu-latest
env:
NOTION_TOKEN: ${{ secrets.NOTION_TOKEN }}
BOOK_DATABASE_ID: ${{ secrets.BOOK_DATABASE_ID }}
AUTHOR_DATABASE_ID: ${{ secrets.AUTHOR_DATABASE_ID }}
CATEGORY_DATABASE_ID: ${{ secrets.CATEGORY_DATABASE_ID }}
BOOKMARK_DATABASE_ID: ${{ secrets.BOOKMARK_DATABASE_ID }}
REVIEW_DATABASE_ID: ${{ secrets.REVIEW_DATABASE_ID }}
CHAPTER_DATABASE_ID: ${{ secrets.CHAPTER_DATABASE_ID }}
YEAR_DATABASE_ID: ${{ secrets.YEAR_DATABASE_ID }}
WEEK_DATABASE_ID: ${{ secrets.WEEK_DATABASE_ID }}
MONTH_DATABASE_ID: ${{ secrets.MONTH_DATABASE_ID }}
DAY_DATABASE_ID: ${{ secrets.DAY_DATABASE_ID }}
NOTION_PAGE: ${{ secrets.NOTION_PAGE }}
WEREAD_COOKIE: ${{ secrets.WEREAD_COOKIE }}
BOOK_DATABASE_NAME: ${{ vars.BOOK_DATABASE_NAME }}
AUTHOR_DATABASE_NAME: ${{ vars.AUTHOR_DATABASE_NAME }}
CATEGORY_DATABASE_NAME: ${{ vars.CATEGORY_DATABASE_NAME }}
BOOKMARK_DATABASE_NAME: ${{ vars.BOOKMARK_DATABASE_NAME }}
REVIEW_DATABASE_NAME: ${{ vars.REVIEW_DATABASE_NAME }}
CHAPTER_DATABASE_NAME: ${{ vars.CHAPTER_DATABASE_NAME }}
YEAR_DATABASE_NAME: ${{ vars.YEAR_DATABASE_NAME }}
WEEK_DATABASE_NAME: ${{ vars.WEEK_DATABASE_NAME }}
MONTH_DATABASE_NAME: ${{ vars.MONTH_DATABASE_NAME }}
DAY_DATABASE_NAME: ${{ vars.DAY_DATABASE_NAME }}
REF: ${{ github.ref }}
REPOSITORY: ${{ github.repository }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -35,10 +38,7 @@ jobs:
pip install -r requirements.txt
- name: weread sync
run: |
python -u scripts/weread.py "${{ github.ref }}" "${{ github.repository }}"
- name: weread heatmap
run: |
github_heatmap weread --weread_cookie "${{secrets.WEREAD_COOKIE}}" --year 2018-2023 --me "${{secrets.NAME}}" --with-animation --background-color=#ffffff --track-color=#ACE7AE --special-color1=#69C16E --special-color2=#549F57 --dom-color=#EBEDF0 --text-color=#000000
python -u scripts/weread.py
- name: push
run: |
git config --local user.email "action@github.com"
Expand Down
Binary file modified scripts/__pycache__/notion_helper.cpython-311.pyc
Binary file not shown.
Binary file modified scripts/__pycache__/weread_api.cpython-311.pyc
Binary file not shown.
78 changes: 66 additions & 12 deletions scripts/notion_helper.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import logging
import os
import re
import time

from notion_client import Client
from retrying import retry
from datetime import timedelta

from utils import (
format_date,
Expand All @@ -26,19 +28,70 @@


class NotionHelper:
database_name_dict = {
"BOOK_DATABASE_NAME":"书架",
"REVIEW_DATABASE_NAME":"笔记",
"BOOKMARK_DATABASE_NAME":"划线",
"DAY_DATABASE_NAME":"日",
"WEEK_DATABASE_NAME":"周",
"MONTH_DATABASE_NAME":"月",
"YEAR_DATABASE_NAME":"年",
"CATEGORY_DATABASE_NAME":"分类",
"AUTHOR_DATABASE_NAME":"作者",
"CHAPTER_DATABASE_NAME":"章节",
}
database_id_dict = {}
image_dict = {}
def __init__(self):
self.client = Client(auth=os.getenv("NOTION_TOKEN"), log_level=logging.ERROR)
self.book_database_id = os.getenv("BOOK_DATABASE_ID")
self.author_database_id = os.getenv("AUTHOR_DATABASE_ID")
self.category_database_id = os.getenv("CATEGORY_DATABASE_ID")
self.bookmark_database_id = os.getenv("BOOKMARK_DATABASE_ID")
self.review_database_id = os.getenv("REVIEW_DATABASE_ID")
self.chapter_database_id = os.getenv("CHAPTER_DATABASE_ID")
self.year_database_id = os.getenv("YEAR_DATABASE_ID")
self.week_database_id = os.getenv("WEEK_DATABASE_ID")
self.month_database_id = os.getenv("MONTH_DATABASE_ID")
self.day_database_id = os.getenv("DAY_DATABASE_ID")
self.__cache = {}
self.__cache={}
self.search_database(self.extract_page_id(os.getenv("NOTION_PAGE")))
for key in self.database_name_dict.keys():
if(os.getenv(key)!=None and os.getenv(key)!=""):
self.database_name_dict[key] = os.getenv(key)
self.book_database_id = self.database_id_dict.get(self.database_name_dict.get("BOOK_DATABASE_NAME"))
self.review_database_id = self.database_id_dict.get(self.database_name_dict.get("REVIEW_DATABASE_NAME"))
self.bookmark_database_id = self.database_id_dict.get(self.database_name_dict.get("BOOKMARK_DATABASE_NAME"))
self.day_database_id = self.database_id_dict.get(self.database_name_dict.get("DAY_DATABASE_NAME"))
self.week_database_id = self.database_id_dict.get(self.database_name_dict.get("WEEK_DATABASE_NAME"))
self.month_database_id = self.database_id_dict.get(self.database_name_dict.get("MONTH_DATABASE_NAME"))
self.year_database_id = self.database_id_dict.get(self.database_name_dict.get("YEAR_DATABASE_NAME"))
self.category_database_id = self.database_id_dict.get(self.database_name_dict.get("CATEGORY_DATABASE_NAME"))
self.author_database_id = self.database_id_dict.get(self.database_name_dict.get("AUTHOR_DATABASE_NAME"))
self.chapter_database_id = self.database_id_dict.get(self.database_name_dict.get("CHAPTER_DATABASE_NAME"))

def extract_page_id(self,notion_url):
# 正则表达式匹配 32 个字符的 Notion page_id
match = re.search(r"([a-f0-9]{32}|[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})", notion_url)
if match:
return match.group(0)
else:
raise Exception(f"获取NotionID失败,请检查输入的Url是否正确")
def search_database(self,block_id):
children = self.client.blocks.children.list(block_id=block_id)["results"]
# 遍历子块
for child in children:
# 检查子块的类型

if child["type"] == "child_database":
self.database_id_dict[child.get('child_database').get('title')] = child.get("id")
elif child["type"] == "image":
self.image_dict["url"] = child.get('image').get('external').get('url')
self.image_dict["id"] = child.get('id')
# 如果子块有子块,递归调用函数
if "has_children" in child and child["has_children"]:
self.search_database(child["id"])

def update_image_block_link(self,block_id, new_image_url):
# 更新 image block 的链接
self.client.blocks.update(
block_id=block_id,
image={
"external": {
"url": new_image_url
}
}
)

def get_week_relation_id(self, date):
year = date.isocalendar().year
Expand Down Expand Up @@ -68,10 +121,11 @@ def get_year_relation_id(self, date):

def get_day_relation_id(self, date):
new_date = date.replace(hour=0, minute=0, second=0, microsecond=0)
timestamp = (new_date-timedelta(hours=8)).timestamp()
day = new_date.strftime("%Y年%m月%d日")
properties = {
"日期": get_date(format_date(date)),
"时间戳": get_number(new_date.timestamp()),
"时间戳": get_number(timestamp),
}
properties["年"] = get_relation(
[
Expand Down
10 changes: 8 additions & 2 deletions scripts/read_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ def insert_to_notion(page_id,timestamp,duration):
weread_cookie = os.getenv("WEREAD_COOKIE")
notion_helper = NotionHelper()
weread_api = WeReadApi()
if(os.path.isfile("./OUT_FOLDER/weread.svg")):
image_url = notion_helper.image_dict.get("url")
block_id = notion_helper.image_dict.get("id")
branch = os.getenv("REF").split("/")[-1]
repository = os.getenv("REPOSITORY")
new_image_url = f"https://raw.githubusercontent.com/{repository}/{branch}/OUT_FOLDER/weread.svg"
if(image_url and block_id):
notion_helper.update_image_block_link(block_id,new_image_url)
api_data = weread_api.get_api_data()
readTimes = dict(sorted(api_data["readTimes"].items()))
print(f"readTimes {len(readTimes)}")
results = notion_helper.query_all(database_id=notion_helper.day_database_id)
for result in results:
timestamp = result.get("properties").get("时间戳").get("number")
duration = result.get("properties").get("时长").get("number")
id = result.get("id")
if(str(timestamp) in readTimes):
value = readTimes.pop(str(timestamp))
print(value)
if(value !=duration):
insert_to_notion(page_id=id,timestamp=timestamp,duration=value)
for key,value in readTimes.items():
Expand Down
11 changes: 4 additions & 7 deletions scripts/weread.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_bookmark_list(page_id, bookId):
return bookmarks


def get_review_list(bookId):
def get_review_list(page_id,bookId):
"""获取笔记"""
filter = {"property": "书籍", "relation": {"contains": page_id}}
results = notion_helper.query_all_by_book(notion_helper.review_database_id, filter)
Expand Down Expand Up @@ -358,13 +358,10 @@ def append_blocks_to_notion(id, blocks, after, contents):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("ref")
parser.add_argument("repository")
options = parser.parse_args()
weread_cookie = os.getenv("WEREAD_COOKIE")
ref = options.ref
branch = ref.split("/")[-1]
repository = options.repository
branch = os.getenv("REF").split("/")[-1]
repository = os.getenv("REPOSITORY")
weread_api = WeReadApi()
notion_helper = NotionHelper()
latest_sort = get_sort()
Expand Down Expand Up @@ -397,7 +394,7 @@ def append_blocks_to_notion(id, blocks, after, contents):
)
chapter = weread_api.get_chapter_info(bookId)
bookmark_list = get_bookmark_list(page_id, bookId)
reviews = get_review_list(bookId)
reviews = get_review_list(page_id,bookId)
bookmark_list.extend(reviews)
content = sort_notes(page_id, chapter, bookmark_list)
append_blocks(page_id, content)
6 changes: 3 additions & 3 deletions scripts/weread_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_notebooklist(self):
books.sort(key=lambda x: x["sort"])
return books
else:
raise Exception(f"Could not get notebook list")
raise Exception(f"Could not get notebook list {r.text}")

@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_bookinfo(self, bookId):
Expand All @@ -61,7 +61,7 @@ def get_bookinfo(self, bookId):
newRating = data["newRating"] / 1000
return (isbn, newRating)
else:
raise Exception("get {bookId} book info failed {r.text}")
return ("", 0)

@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_bookmark_list(self, bookId):
Expand All @@ -84,7 +84,7 @@ def get_read_info(self, bookId):
if r.ok:
return r.json()
else:
raise Exception("get {bookId} read info failed {r.text}")
raise Exception(f"get {bookId} read info failed {r.text}")

@retry(stop_max_attempt_number=3, wait_fixed=5000)
def get_review_list(self, bookId):
Expand Down

0 comments on commit 408b998

Please sign in to comment.