Skip to content

Commit 97bda4d

Browse files
committed
fix heatmap
1 parent 709f1e7 commit 97bda4d

File tree

5 files changed

+127
-79
lines changed

5 files changed

+127
-79
lines changed

.github/workflows/read_time.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
CC_URL: ${{ secrets.CC_URL }}
1919
CC_ID: ${{ secrets.CC_ID }}
2020
CC_PASSWORD: ${{ secrets.CC_PASSWORD }}
21+
HEATMAP_BLOCK_ID: ${{ secrets.HEATMAP_BLOCK_ID }}
2122
BOOK_DATABASE_NAME: ${{ vars.BOOK_DATABASE_NAME }}
2223
AUTHOR_DATABASE_NAME: ${{ vars.AUTHOR_DATABASE_NAME }}
2324
CATEGORY_DATABASE_NAME: ${{ vars.CATEGORY_DATABASE_NAME }}
@@ -49,12 +50,23 @@ jobs:
4950
if: env.YEAR == ''
5051
- name: weread heatmap
5152
run: |
52-
github_heatmap weread --year $YEAR --me "${{secrets.NAME}}" --with-animation --background-color=${{ vars.background_color||'#FFFFFF'}} --track-color=${{ vars.track_color||'#ACE7AE'}} --special-color1=${{ vars.special_color||'#69C16E'}} --special-color2=${{ vars.special_color2||'#549F57'}} --dom-color=${{ vars.dom_color||'#EBEDF0'}} --text-color=${{ vars.text_color||'#000000'}}
53+
github_heatmap weread --year $YEAR --me "${{secrets.NAME}}" --without-type-name --background-color=${{ vars.background_color||'#FFFFFF'}} --track-color=${{ vars.track_color||'#ACE7AE'}} --special-color1=${{ vars.special_color||'#69C16E'}} --special-color2=${{ vars.special_color2||'#549F57'}} --dom-color=${{ vars.dom_color||'#EBEDF0'}} --text-color=${{ vars.text_color||'#000000'}}
5354
- name: Rename weread.svg to a random name
5455
run: |
56+
cd OUT_FOLDER
57+
find . -type f ! -name "weread.svg" -exec rm -f {} +
58+
cd ..
5559
RANDOM_FILENAME=$(uuidgen).svg
5660
mv ./OUT_FOLDER/weread.svg ./OUT_FOLDER/$RANDOM_FILENAME
5761
echo "Renamed file to $RANDOM_FILENAME"
62+
- name: push
63+
run: |
64+
git config --local user.email "[email protected]"
65+
git config --local user.name "GitHub Action"
66+
git add .
67+
git commit -m 'add new heatmap' || echo "nothing to commit"
68+
git push || echo "nothing to push"
5869
- name: read time sync
5970
run: |
6071
python -u scripts/read_time.py
72+

.github/workflows/weread.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,5 @@ jobs:
4646
python -u scripts/book.py
4747
- name: weread sync
4848
run: |
49-
python -u scripts/weread.py
49+
python -u scripts/weread.py
50+

scripts/notion_helper.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class NotionHelper:
4444
"READ_DATABASE_NAME": "阅读记录",
4545
}
4646
database_id_dict = {}
47-
image_dict = {}
47+
heatmap_block_id = None
4848

4949
def __init__(self):
5050
self.client = Client(auth=os.getenv("NOTION_TOKEN"), log_level=logging.ERROR)
@@ -91,7 +91,6 @@ def __init__(self):
9191
if self.read_database_id is None:
9292
self.create_database()
9393

94-
9594
def extract_page_id(self, notion_url):
9695
# 正则表达式匹配 32 个字符的 Notion page_id
9796
match = re.search(
@@ -108,14 +107,13 @@ def search_database(self, block_id):
108107
# 遍历子块
109108
for child in children:
110109
# 检查子块的类型
111-
112110
if child["type"] == "child_database":
113-
self.database_id_dict[
114-
child.get("child_database").get("title")
115-
] = child.get("id")
116-
elif child["type"] == "image" and child.get("image").get("external"):
117-
self.image_dict["url"] = child.get("image").get("external").get("url")
118-
self.image_dict["id"] = child.get("id")
111+
self.database_id_dict[child.get("child_database").get("title")] = (
112+
child.get("id")
113+
)
114+
elif child["type"] == "embed" and child.get("embed").get("url"):
115+
if child.get("embed").get("url").startswith("https://heatmap.malinkang.com/"):
116+
self.heatmap_block_id = child.get("id")
119117
# 如果子块有子块,递归调用函数
120118
if "has_children" in child and child["has_children"]:
121119
self.search_database(child["id"])
@@ -171,24 +169,24 @@ def create_database(self):
171169
"时长": {"number": {}},
172170
"时间戳": {"number": {}},
173171
"日期": {"date": {}},
174-
"书架": {"relation": {
175-
"database_id":self.book_database_id,
176-
"single_property": {},
177-
}},
172+
"书架": {
173+
"relation": {
174+
"database_id": self.book_database_id,
175+
"single_property": {},
176+
}
177+
},
178178
}
179179
parent = parent = {"page_id": self.page_id, "type": "page_id"}
180-
self.read_database_id=self.client.databases.create(
180+
self.read_database_id = self.client.databases.create(
181181
parent=parent,
182182
title=title,
183183
icon=get_icon("https://www.notion.so/icons/target_gray.svg"),
184184
properties=properties,
185185
).get("id")
186186

187-
def update_image_block_link(self, block_id, new_image_url):
187+
def update_heatmap(self, block_id, url):
188188
# 更新 image block 的链接
189-
self.client.blocks.update(
190-
block_id=block_id, image={"external": {"url": new_image_url}}
191-
)
189+
return self.client.blocks.update(block_id=block_id, embed={"url": url})
192190

193191
def get_week_relation_id(self, date):
194192
year = date.isocalendar().year
@@ -329,20 +327,20 @@ def update_book_page(self, page_id, properties):
329327
return self.client.pages.update(page_id=page_id, properties=properties)
330328

331329
@retry(stop_max_attempt_number=3, wait_fixed=5000)
332-
def update_page(self, page_id, properties,cover):
330+
def update_page(self, page_id, properties, cover):
333331
return self.client.pages.update(
334-
page_id=page_id, properties=properties,cover=cover
332+
page_id=page_id, properties=properties, cover=cover
335333
)
336334

337335
@retry(stop_max_attempt_number=3, wait_fixed=5000)
338336
def create_page(self, parent, properties, icon):
339337
return self.client.pages.create(parent=parent, properties=properties, icon=icon)
340-
341-
342338

343339
@retry(stop_max_attempt_number=3, wait_fixed=5000)
344340
def create_book_page(self, parent, properties, icon):
345-
return self.client.pages.create(parent=parent, properties=properties, icon=icon,cover=icon)
341+
return self.client.pages.create(
342+
parent=parent, properties=properties, icon=icon, cover=icon
343+
)
346344

347345
@retry(stop_max_attempt_number=3, wait_fixed=5000)
348346
def query(self, **kwargs):

scripts/read_time.py

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,78 +9,108 @@
99

1010
from notion_helper import NotionHelper
1111
from weread_api import WeReadApi
12-
from utils import format_date, get_date, get_icon, get_number, get_relation, get_title,upload_image
12+
from utils import (
13+
format_date,
14+
get_date,
15+
get_icon,
16+
get_number,
17+
get_relation,
18+
get_title,
19+
get_embed,
20+
)
1321

14-
def insert_to_notion(page_id,timestamp,duration):
22+
23+
def insert_to_notion(page_id, timestamp, duration):
1524
parent = {"database_id": notion_helper.day_database_id, "type": "database_id"}
1625
properties = {
17-
"标题": get_title(format_date(datetime.utcfromtimestamp(timestamp)+timedelta(hours=8),"%Y年%m月%d日")),
18-
"日期": get_date(start = format_date(datetime.utcfromtimestamp(timestamp)+timedelta(hours=8))),
26+
"标题": get_title(
27+
format_date(
28+
datetime.utcfromtimestamp(timestamp) + timedelta(hours=8),
29+
"%Y年%m月%d日",
30+
)
31+
),
32+
"日期": get_date(
33+
start=format_date(datetime.utcfromtimestamp(timestamp) + timedelta(hours=8))
34+
),
1935
"时长": get_number(duration),
2036
"时间戳": get_number(timestamp),
21-
"年":get_relation(
37+
"年": get_relation(
2238
[
23-
notion_helper.get_year_relation_id(datetime.utcfromtimestamp(timestamp)+timedelta(hours=8)),
39+
notion_helper.get_year_relation_id(
40+
datetime.utcfromtimestamp(timestamp) + timedelta(hours=8)
41+
),
2442
]
2543
),
26-
"月":get_relation(
44+
"月": get_relation(
2745
[
28-
notion_helper.get_month_relation_id(datetime.utcfromtimestamp(timestamp)+timedelta(hours=8)),
46+
notion_helper.get_month_relation_id(
47+
datetime.utcfromtimestamp(timestamp) + timedelta(hours=8)
48+
),
2949
]
3050
),
31-
"周":get_relation(
51+
"周": get_relation(
3252
[
33-
notion_helper.get_week_relation_id(datetime.utcfromtimestamp(timestamp)+timedelta(hours=8)),
53+
notion_helper.get_week_relation_id(
54+
datetime.utcfromtimestamp(timestamp) + timedelta(hours=8)
55+
),
3456
]
35-
)
36-
57+
),
3758
}
38-
if page_id!=None:
59+
if page_id != None:
3960
notion_helper.client.pages.update(page_id=page_id, properties=properties)
4061
else:
41-
notion_helper.client.pages.create(parent=parent,icon = get_icon("https://www.notion.so/icons/target_red.svg"),properties=properties)
62+
notion_helper.client.pages.create(
63+
parent=parent,
64+
icon=get_icon("https://www.notion.so/icons/target_red.svg"),
65+
properties=properties,
66+
)
4267

4368

4469
def get_file():
4570
# 设置文件夹路径
46-
folder_path = './OUT_FOLDER'
71+
folder_path = "./OUT_FOLDER"
4772

4873
# 检查文件夹是否存在
4974
if os.path.exists(folder_path) and os.path.isdir(folder_path):
5075
entries = os.listdir(folder_path)
51-
76+
5277
file_name = entries[0] if entries else None
5378
return file_name
5479
else:
5580
print("OUT_FOLDER does not exist.")
5681
return None
5782

83+
5884
if __name__ == "__main__":
5985
notion_helper = NotionHelper()
6086
weread_api = WeReadApi()
6187
image_file = get_file()
6288
if image_file:
63-
image_url = upload_image(f"heatmap/{os.getenv('REPOSITORY').split('/')[0]}",image_file,f"./OUT_FOLDER/{image_file}")
64-
block_id = os.getenv("HEATMAP_BLOCK_ID")
65-
if block_id == None or block_id.strip() == "":
66-
block_id = notion_helper.image_dict.get("id")
67-
if(image_url and block_id):
68-
notion_helper.update_image_block_link(block_id,image_url)
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+
response = notion_helper.append_blocks(
97+
block_id=notion_helper.page_id, children=[get_embed(heatmap_url)]
98+
)
6999
api_data = weread_api.get_api_data()
70-
readTimes = {int(key):value for key,value in api_data.get("readTimes").items()}
71-
now = pendulum.now('Asia/Shanghai').start_of('day')
100+
readTimes = {int(key): value for key, value in api_data.get("readTimes").items()}
101+
now = pendulum.now("Asia/Shanghai").start_of("day")
72102
today_timestamp = now.int_timestamp
73-
if(today_timestamp not in readTimes):
103+
if today_timestamp not in readTimes:
74104
readTimes[today_timestamp] = 0
75105
readTimes = dict(sorted(readTimes.items()))
76-
results = notion_helper.query_all(database_id=notion_helper.day_database_id)
106+
results = notion_helper.query_all(database_id=notion_helper.day_database_id)
77107
for result in results:
78108
timestamp = result.get("properties").get("时间戳").get("number")
79109
duration = result.get("properties").get("时长").get("number")
80110
id = result.get("id")
81-
if(timestamp in readTimes):
111+
if timestamp in readTimes:
82112
value = readTimes.pop(timestamp)
83-
if(value !=duration):
84-
insert_to_notion(page_id=id,timestamp=timestamp,duration=value)
85-
for key,value in readTimes.items():
86-
insert_to_notion(None,int(key),value)
113+
if value != duration:
114+
insert_to_notion(page_id=id, timestamp=timestamp, duration=value)
115+
for key, value in readTimes.items():
116+
insert_to_notion(None, int(key), value)

0 commit comments

Comments
 (0)