From 0e1c6c1c1aa59106f3ceff50b070ce2810b3e652 Mon Sep 17 00:00:00 2001 From: n3040 Date: Sat, 5 Mar 2022 23:16:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=8F=90=E9=86=92=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++ api.py | 3 ++ auth.py | 3 ++ main.py | 128 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 19 +++++++ run.sh | 0 6 files changed, 157 insertions(+) create mode 100644 Makefile create mode 100644 api.py create mode 100644 auth.py create mode 100644 main.py create mode 100644 requirements.txt create mode 100644 run.sh diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b371bd0 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +publish: + #scp -v -P 22 main.py cubie@192.168.1.130:/home/cubie/down/document/python/task_automation + scp -v -P 24123 -i C:\\Users\\3040/.ssh/163 main.py cubie@14.18.54.194:/home/cubie/down/document/python/task_automation + #scp -v -P 24123 -i C:\\Users\\3040/.ssh/163 requirements.txt cubie@14.18.54.194:/home/cubie/down/document/python/task_automation diff --git a/api.py b/api.py new file mode 100644 index 0000000..9bb71ec --- /dev/null +++ b/api.py @@ -0,0 +1,3 @@ +from fastapi import FastAPI + +app = FastAPI() diff --git a/auth.py b/auth.py new file mode 100644 index 0000000..787118b --- /dev/null +++ b/auth.py @@ -0,0 +1,3 @@ +from notion.client import NotionClient +client = NotionClient(token_v2=r"1bc7fdf20c9b3059eeb3897fd904b02e23c17682fb30103fe886211528551adbd5ae35e3892bf8fdc8528ec9318f9c89d7cc3f8eaabb43f37187580911e29f17b3c902f4c9b813ae4552c9fe1775") +print('client init') \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..02f5646 --- /dev/null +++ b/main.py @@ -0,0 +1,128 @@ +# from notion.block import CalloutBlock, BulletedListBlock, TextBlock +# from auth import client +from notion_client import AsyncClient +from pprint import pprint +import asyncio +import platform +from dateutil.parser import parse +from dateutil.tz import gettz +import datetime +import requests +import json +from loguru import logger + +# def update_callout(): +# page_callout_extract = client.get_block( +# "https://www.notion.so/facat/76583e3369764976868d11bc761abc93" +# ) +# page_callout_update = client.get_block( +# "https://www.notion.so/facat/8f4f0ed22f9f4afb9b8faee15b33d8ed" +# ) +# page_callout_extract.locked = True +# page_callout_update.locked = True +# print("The old title is:", page_callout_extract.title) +# callout_list = [] +# for child in page_callout_extract.children: +# if type(child) == CalloutBlock: +# callout_list.append(child.title) +# print(f"找到Callout{child.title}") +# # 清除重点的内容 +# for child in page_callout_update.children: +# child.remove() +# # 更新重点的内容 +# for callout_list in callout_list: +# page_callout_update.children.add_new(BulletedListBlock, title=callout_list) +# page_callout_update.children.add_new(TextBlock, title="End!") +# page_callout_extract.locked = False +# +# +# async def sdk_callout(): +# async with AsyncClient( +# auth="secret_WoGb0MDPDdwiEMQr8ZU6i50u7fT6WD6RzXkHu6RAF4R" +# ) as notion: +# page = await notion.pages.retrieve("8f4f0ed22f9f4afb9b8faee15b33d8ed") +# children_info = await notion.blocks.children.list(page["id"]) +# children = children_info["results"] +# for child in children: +# # text=await notion.blocks.children.list(child['id']) +# pprint(child) +# +# +# async def extract_callout(): +# async with AsyncClient( +# auth="secret_WoGb0MDPDdwiEMQr8ZU6i50u7fT6WD6RzXkHu6RAF4R" +# ) as notion: +# extract_page_block_info = await notion.blocks.children.list('76583e3369764976868d11bc761abc93') +# extract_blocks = extract_page_block_info["results"] +# for blocks in extract_blocks: +# if blocks['type']=='synced_block': +# callouts=await notion.blocks.children.list(blocks['id']) +# pprint(callouts) + + +def test_if_mention(block): + if "paragraph" in block: + paragraph = block["paragraph"] + if "text" in paragraph: + texts = paragraph["text"] + for text in texts: + if "mention" in text: + return text["mention"] + return None + + +def send_msg(text): + r = requests.post( + "https://mmssgg.happymap.xyz/sendmessage", + headers={"Content-Type": "application/json"}, + data=json.dumps({"message": text}), + ) + + +async def check_un_do(): + notion = AsyncClient(auth="secret_WoGb0MDPDdwiEMQr8ZU6i50u7fT6WD6RzXkHu6RAF4R") + todo_database_id = "31ba234684e54e57a10e7e155fe7767c" + uncompleted_todos_query = await notion.databases.query( + **{ + "database_id": todo_database_id, + "filter": {"property": "状态", "select": {"equals": "未完成"}}, + } + ) + uncompleted_todos = uncompleted_todos_query["results"] + for uncompleted_todo in uncompleted_todos: + # pprint(uncompleted_todo) + task_name = uncompleted_todo["properties"]["Name"]["title"][0]["plain_text"] + task_url = uncompleted_todo["url"] + page_id = uncompleted_todo["id"] + page_blocks_query = await notion.blocks.children.list(page_id) + page_blocks = page_blocks_query["results"] + for block in page_blocks: + mention = test_if_mention(block) + # pprint(block) + if mention: + mention_date = mention["date"]["start"] + time_zone = gettz("Asia/Chongqing") + mention_datetime = parse(mention_date) + now = datetime.datetime.now(time_zone) + duration = now - mention_datetime + duration_seconds = duration.total_seconds() + if -5 * 60 < duration_seconds < 0: # 提前5分钟通知 + send_msg(f"任务 {task_name} 要开始了。地址:{task_url}") + logger.info(f"提示了任务 {task_name} ") + pass + # pprint(mention) + + +async def loop(): + while True: + logger.info("检查任务。") + await check_un_do() + await asyncio.sleep(3 * 60) # 3分钟检查一次 + + +if __name__ == "__main__": + # update_callout() + if platform.system() == "Windows": + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + asyncio.run(loop()) + print("PyCharm") diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..d84ed55 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +# Automatically generated by https://github.com/damnever/pigar. + +# D:\code\notion\api.py: 1 +fastapi == 0.70.1 + +# D:\code\notion\main.py: 12 +loguru == 0.5.3 + +# D:\code\notion\auth.py: 1 +notion == 0.0.28 + +# D:\code\notion\main.py: 3 +notion_client == 0.9.0 + +# D:\code\notion\main.py: 7,8 +python_dateutil == 2.8.2 + +# D:\code\notion\main.py: 10 +requests == 2.26.0 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..e69de29