Skip to content

Commit

Permalink
✨ uploaded check
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjlm committed Apr 10, 2022
1 parent c5004f9 commit b9b509a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 6 deletions.
2 changes: 1 addition & 1 deletion notetrail/character_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import glob
import os

from utils.utils import Bcolors, BookInfo
from notetrail.utils.utils import Bcolors, BookInfo


class CharacterScanner:
Expand Down
19 changes: 19 additions & 0 deletions notetrail/hexo/hexo.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def title_parser(value: str):
}
}

@staticmethod
def get_title(properties: dict):
return properties['title']['title'][0]['text']['content']

@staticmethod
def hash_parser(value: str):
return {
Expand All @@ -109,6 +113,10 @@ def hash_parser(value: str):
}
}

@staticmethod
def get_hash(properties: dict):
return properties['HashValue']['rich_text'][0]['text']['content']

def serialize_raw_property(self, property_dict):
...

Expand Down Expand Up @@ -160,6 +168,17 @@ 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
Expand Down
6 changes: 6 additions & 0 deletions notetrail/my_notion_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from loguru import logger
from notion_client import Client

from notetrail.utils.decorators import log


class MyNotionClient:
def __init__(self, client):
Expand Down Expand Up @@ -46,6 +48,10 @@ def delete_all_children(self, block_id, children):
for child in children:
self.client.blocks.delete(block_id=child['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)
notion_client = MyNotionClient(client_)
8 changes: 4 additions & 4 deletions notetrail/notion_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
from mistletoe.span_token import HTMLSpan
from mistletoe.base_renderer import BaseRenderer

from my_notion_client import notion_client
from utils.oss_handler import oss_handler
from utils.utils import markdown_render, erase_prefix_string, BookInfo, long_content_split_patch, validate_language, \
normal_language_map
from notetrail.my_notion_client import notion_client
from notetrail.utils.oss_handler import oss_handler
from notetrail.utils.utils import markdown_render, erase_prefix_string, BookInfo, long_content_split_patch, \
validate_language, normal_language_map

if sys.version_info < (3, 4):
from mistletoe import _html as html
Expand Down
94 changes: 94 additions & 0 deletions notetrail/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""
@author: harumonia
@license: © Copyright 2022, Node Supply Chain Manager Corporation Limited.
@contact: [email protected]
@software: Pycharm
@homepage: https://harumonia.moe/
@file: decorators.py
@time: 2022/4/10 21:54
@desc:
"""
import inspect
from datetime import datetime
from functools import wraps
from typing import Callable

from loguru import logger

LOGGING_FN = logger


def log(
func: Callable = None,
log_time: bool = True,
log_args: bool = True,
log_error: bool = True,
log_file: str = None,
logging_fn: Callable = None,
) -> Callable:
"""
Tracks function time taken, arguments and errors
Arguments:
func: function to decorate
log_time: whether to log time taken or not, default=True
log_args: whether to log arguments or not, default=True
log_error: whether to log error or not, default=True
log_file: filepath where to write log, default=None
logging_fn: log function (e.g. print, logger.info, rich console.print)
Usage:
```python
from deczoo import log
@log
def add(a, b): return a+b
_ = add(1, 2)
# add args=(a=1, b=2) time=0:00:00.000111
```
"""

if logging_fn is None:
logging_fn = LOGGING_FN

@wraps(func)
def wrapper(*args, **kwargs):

tic = datetime.now()

if log_args:

func_args = inspect.signature(func).bind(*args, **kwargs).arguments
func_args_str = ", ".join(f"{k}={v}" for k, v in func_args.items())

optional_strings = [f"args=({func_args_str})"]

else:
optional_strings = []

try:
res = func(*args, **kwargs)
toc = datetime.now()
optional_strings += [
f"time={toc - tic}" if log_time else None,
]

return res

except Exception as e:

toc = datetime.now()
optional_strings += [
f"time={toc - tic}" if log_time else None,
"Failed" + (f" with error: {e}" if log_error else ""),
]
raise e

finally:
log_string = (
f"{func.__name__} {' '.join([s for s in optional_strings if s])}"
)
logging_fn.info(log_string)

if log_file is not None:
with open(log_file, "a") as f:
f.write(f"{tic} {log_string}\n")

return wrapper
2 changes: 1 addition & 1 deletion notetrail/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


class RuntimeConfig:
database_id = 'd0e931a36b43405996d118cf71957f6d' # 数据库id
database_id = '5dfca79037874290b22dec916bdc9f07' # 数据库id


class BookInfo:
Expand Down

0 comments on commit b9b509a

Please sign in to comment.