diff --git a/README.md b/README.md index 7be9344..e6962bc 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ convert markdown directory to notions. 在 __./notetrail/hexo/hexo.py__ 中填入对应的参数. +如果 md 文件的标头中存在 "notion: false" 的配置, 则将会跳过该文件. 对于不想被收录的, 或者因使用了非notion语法而无法被解析的, 可以使用该方法. + ### 样例展示 [博客地址](https://blog.harumonia.moe/) diff --git a/notetrail/hexo/hexo.py b/notetrail/hexo/hexo.py index 403cc99..6c1580c 100644 --- a/notetrail/hexo/hexo.py +++ b/notetrail/hexo/hexo.py @@ -2,6 +2,7 @@ import json import re from datetime import datetime +from pprint import pprint import yaml from loguru import logger @@ -9,6 +10,7 @@ from notetrail.character_scanner import CharacterScanner from notetrail.my_notion_client import notion_client from notetrail.notion_render import SuffixRender, NotionRender +from notetrail.utils.exceptions import NotionYamlParserError from notetrail.utils.utils import BookInfo, markdown_render, generate_md5_from_text, RuntimeConfig @@ -133,6 +135,10 @@ def main(self, property_dict): return ret +class Collector: + error_arts = [] + + class HexoProcessor: def __init__(self, database_id=None, page_id=None): if (database_id or page_id) is None: @@ -159,31 +165,32 @@ def extract_yaml_content(): property_dict = HexoParser().main(yaml_dict) return property_dict else: - raise Exception('notion: false') + logger.info("schema notion is false, skip this file.") def generate_properties(self, file_path): return self.pre_parse_hexo_file(file_path) def file_processor(self, file_path, page_id): logger.info('----------------> Processing file: {}'.format(file_path)) - try: - properties = self.generate_properties(file_path) - - full_title = HexoParser.get_title(properties) - response = notion_client.search(query=full_title) - for result in response.get('results', []): - if result['properties']['\ufeffName']['title'][0]['plain_text']: - if result['properties']['HashValue']['rich_text'][0]['plain_text'] == HexoParser.get_hash(properties): - logger.info(f'blog {full_title} has been in the notion') - return - else: - logger.warning(f'blog {full_title} need to be update, but this is a todo feature...') - return - except Exception as _e: - if str(_e) == 'notion: false': - return - else: - raise _e + properties = self.generate_properties(file_path) + if properties is None: + return + + full_title = HexoParser.get_title(properties) + response = notion_client.search(query=full_title) + + for result in response.get('results', []): + if result['properties']['\ufeffName']['title'][0]['plain_text'] == full_title: + if result['properties']['HashValue']['rich_text'][0]['plain_text'] == HexoParser.get_hash( + properties): + # 标题作为去重的键 + logger.info(f'blog {full_title} has been in the notion') + return + else: + logger.warning(f'blog {full_title} need to be updated') + notion_client.delete_block(result['id']) + # ! 该段产生的异常不排除是代码出现 BUG, 所以不进行异常捕获, 一旦出现立刻结束程序, 避免浪费资源. + try: children = self.render_file(file_path) response = notion_client.create_page(parent={"database_id": page_id}, properties=properties, @@ -192,6 +199,11 @@ def file_processor(self, file_path, page_id): sf.recursion_insert(response['id']) except Exception as _e: logger.exception(_e) + Collector.error_arts.append({ + "title": full_title, + "exception": str(_e), + "file_path": file_path + }) return def main(self): @@ -225,3 +237,7 @@ def clean_annotation(text_): HexoParser.update_properties(database_id_) p = HexoProcessor(database_id=database_id_) p.main() + + if Collector.error_arts: + logger.error("存在如下的错误:") + logger.error(Collector.error_arts) diff --git a/notetrail/my_notion_client.py b/notetrail/my_notion_client.py index e2df86c..7630efb 100644 --- a/notetrail/my_notion_client.py +++ b/notetrail/my_notion_client.py @@ -48,10 +48,15 @@ def delete_all_children(self, block_id, children): for child in children: self.client.blocks.delete(block_id=child['id']) + @log + def delete_block(self, block_id: str): + return self.client.blocks.delete(block_id) + @log def search(self, query): return self.client.search(query=query) -client_ = httpx.Client(proxies={'http://': 'http://127.0.0.1:7890', 'https://': 'http://127.0.0.1:7890'}, timeout=30) +# client_ = httpx.Client(proxies={'http://': 'http://127.0.0.1:7890', 'https://': 'http://127.0.0.1:7890'}, timeout=30) +client_ = httpx.Client(timeout=30) notion_client = MyNotionClient(client_) diff --git a/notetrail/utils/exceptions.py b/notetrail/utils/exceptions.py new file mode 100644 index 0000000..e657a33 --- /dev/null +++ b/notetrail/utils/exceptions.py @@ -0,0 +1,14 @@ +""" +@author: harumonia +@license: © Copyright 2022, Node Supply Chain Manager Corporation Limited. +@contact: zxjlm233@gmail.com +@software: Pycharm +@homepage: https://harumonia.moe/ +@file: exceptions.py +@time: 2022/6/4 18:07 +@desc: +""" + + +class NotionYamlParserError(Exception): + ... diff --git a/notetrail/utils/oss_handler.py b/notetrail/utils/oss_handler.py index 07272ec..822eaba 100644 --- a/notetrail/utils/oss_handler.py +++ b/notetrail/utils/oss_handler.py @@ -13,7 +13,6 @@ def __init__(self): sk = os.environ.get('ALI_OSS_SK') bucket_url = os.environ.get('ALI_BUCKET') if ak and sk and bucket_url: - 'https://notion-oss-bucket.oss-cn-shanghai.aliyuncs.com/' self.BASIC_URL = bucket_url bucket_name, endpoint = self._split_url() auth = oss2.Auth(ak, sk)