Skip to content

Commit

Permalink
✨ can update hexo articles
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjlm authored and Zhang Xinjian committed Jun 4, 2022
1 parent b9b509a commit 6b789b1
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ convert markdown directory to notions.

__./notetrail/hexo/hexo.py__ 中填入对应的参数.

如果 md 文件的标头中存在 "notion: false" 的配置, 则将会跳过该文件. 对于不想被收录的, 或者因使用了非notion语法而无法被解析的, 可以使用该方法.

### 样例展示

[博客地址](https://blog.harumonia.moe/)
Expand Down
54 changes: 35 additions & 19 deletions notetrail/hexo/hexo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
import json
import re
from datetime import datetime
from pprint import pprint

import yaml
from loguru import logger

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


Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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)
7 changes: 6 additions & 1 deletion notetrail/my_notion_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_)
14 changes: 14 additions & 0 deletions notetrail/utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
@author: harumonia
@license: © Copyright 2022, Node Supply Chain Manager Corporation Limited.
@contact: [email protected]
@software: Pycharm
@homepage: https://harumonia.moe/
@file: exceptions.py
@time: 2022/6/4 18:07
@desc:
"""


class NotionYamlParserError(Exception):
...
1 change: 0 additions & 1 deletion notetrail/utils/oss_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6b789b1

Please sign in to comment.