|
9 | 9 |
|
10 | 10 | from notion_helper import NotionHelper |
11 | 11 | 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 | +) |
13 | 21 |
|
14 | | -def insert_to_notion(page_id,timestamp,duration): |
| 22 | + |
| 23 | +def insert_to_notion(page_id, timestamp, duration): |
15 | 24 | parent = {"database_id": notion_helper.day_database_id, "type": "database_id"} |
16 | 25 | 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 | + ), |
19 | 35 | "时长": get_number(duration), |
20 | 36 | "时间戳": get_number(timestamp), |
21 | | - "年":get_relation( |
| 37 | + "年": get_relation( |
22 | 38 | [ |
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 | + ), |
24 | 42 | ] |
25 | 43 | ), |
26 | | - "月":get_relation( |
| 44 | + "月": get_relation( |
27 | 45 | [ |
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 | + ), |
29 | 49 | ] |
30 | 50 | ), |
31 | | - "周":get_relation( |
| 51 | + "周": get_relation( |
32 | 52 | [ |
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 | + ), |
34 | 56 | ] |
35 | | - ) |
36 | | - |
| 57 | + ), |
37 | 58 | } |
38 | | - if page_id!=None: |
| 59 | + if page_id != None: |
39 | 60 | notion_helper.client.pages.update(page_id=page_id, properties=properties) |
40 | 61 | 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 | + ) |
42 | 67 |
|
43 | 68 |
|
44 | 69 | def get_file(): |
45 | 70 | # 设置文件夹路径 |
46 | | - folder_path = './OUT_FOLDER' |
| 71 | + folder_path = "./OUT_FOLDER" |
47 | 72 |
|
48 | 73 | # 检查文件夹是否存在 |
49 | 74 | if os.path.exists(folder_path) and os.path.isdir(folder_path): |
50 | 75 | entries = os.listdir(folder_path) |
51 | | - |
| 76 | + |
52 | 77 | file_name = entries[0] if entries else None |
53 | 78 | return file_name |
54 | 79 | else: |
55 | 80 | print("OUT_FOLDER does not exist.") |
56 | 81 | return None |
57 | 82 |
|
| 83 | + |
58 | 84 | if __name__ == "__main__": |
59 | 85 | notion_helper = NotionHelper() |
60 | 86 | weread_api = WeReadApi() |
61 | 87 | image_file = get_file() |
62 | 88 | 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 | + ) |
69 | 99 | 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") |
72 | 102 | today_timestamp = now.int_timestamp |
73 | | - if(today_timestamp not in readTimes): |
| 103 | + if today_timestamp not in readTimes: |
74 | 104 | readTimes[today_timestamp] = 0 |
75 | 105 | 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) |
77 | 107 | for result in results: |
78 | 108 | timestamp = result.get("properties").get("时间戳").get("number") |
79 | 109 | duration = result.get("properties").get("时长").get("number") |
80 | 110 | id = result.get("id") |
81 | | - if(timestamp in readTimes): |
| 111 | + if timestamp in readTimes: |
82 | 112 | 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