From 038b9251d48c47824d57b0715d5830c223b15a11 Mon Sep 17 00:00:00 2001 From: clouder Date: Fri, 12 Nov 2021 18:34:26 +0800 Subject: [PATCH] Fix authorization --- SiyuanYuque/__main__.py | 2 +- SiyuanYuque/config.py | 2 + SiyuanYuque/siyuanHelper.py | 151 ------------------------------------ setup.cfg | 3 +- 4 files changed, 5 insertions(+), 153 deletions(-) delete mode 100644 SiyuanYuque/siyuanHelper.py diff --git a/SiyuanYuque/__main__.py b/SiyuanYuque/__main__.py index f9c63c3..389e77c 100644 --- a/SiyuanYuque/__main__.py +++ b/SiyuanYuque/__main__.py @@ -1,6 +1,6 @@ import asyncio import datetime -from . import siyuanHelper as siyuan +from siyuanhelper import helper as siyuan from yuque_py import Yuque from .config import conf from . import config diff --git a/SiyuanYuque/config.py b/SiyuanYuque/config.py index 164446c..009ab7b 100644 --- a/SiyuanYuque/config.py +++ b/SiyuanYuque/config.py @@ -1,7 +1,9 @@ import toml +from siyuanhelper import helper as siyuan conf = toml.load("sqconfig.toml") +siyuan.set_token(conf["siyuan_token"]) def write_conf(): diff --git a/SiyuanYuque/siyuanHelper.py b/SiyuanYuque/siyuanHelper.py deleted file mode 100644 index 49a7444..0000000 --- a/SiyuanYuque/siyuanHelper.py +++ /dev/null @@ -1,151 +0,0 @@ -import requests -import json -import re -import asyncio -import aiohttp -from .config import conf - - -API_URL = "http://127.0.0.1:6806/api/" -HEADERS = {"Content-Type": "application/json"} -TOKEN = conf["siyuan_token"] - - -async def post(url: str, **params): - async with aiohttp.ClientSession() as session: - async with session.post(url=url, json=params, headers=HEADERS) as resp: - return await resp.json() - - -class ApiException(Exception): - pass - - -class AuthCodeIncorrectException(Exception): - pass - - -def parse_ial(text: str): - ret = {} - subs = re.finditer(r" (.+?)=\"(.+?)\"", text, flags=re.DOTALL) - for sub in subs: - ret[sub.group(1)] = sub.group(2) - return ret - - -class Block: - def __init__(self, - alias, box, content, - created, hash, ial, id, length, - markdown: str, memo, name, - next_id, parent_id, path, - previous_id, root_id, sort): - self.alias = alias - self.box = box - self.content = content - self.created = created - self.hash = hash - self.ial = ial - self.id = id - self.length = length - self.markdown = markdown - self.memo = memo - self.name = name - self.next_id = next_id - self.parent_id = parent_id - self.path = path - self.previous_id = previous_id - self.root_id = root_id - self.sort = sort - self.properties = parse_ial(self.ial) - - def __init__(self, dict): - for x in dict: - setattr(self, x, dict[x]) - self.properties = parse_ial(self.ial) - - -""" -BlockPool = {} -def get_block(dict) -> Block: - ret = BlockPool.get(dict["id"]) - if ret is None: - ret = Block(dict) - BlockPool[dict["id"]] = ret - return ret -""" - - -async def query_sql(SQL: str): - result = await post(API_URL + "query/sql", stmt=SQL) - if result["code"] == 0: - return result["data"] - raise ApiException(result) - - -class NullBlockException(Exception): - pass - - -async def find_by_id(id: str) -> Block: - res = await query_sql(r"SELECT * FROM blocks WHERE id='{}'".format(id)) - if len(res) <= 0: - raise NullBlockException(id) - return Block(res[0]) - - -async def get_col_by_id(id: str, attr_name: str): - # print("get_col_by_id", id, attr_name) - res = await query_sql( - r"SELECT {} FROM blocks WHERE id='{}'".format(attr_name, id)) - if len(res) <= 0: - raise NullBlockException(id) - return res[0][attr_name] - - -async def get_ial_by_id(id: str) -> str: - return (await get_col_by_id(id, "ial")).replace("_esc_newline_", "\n") - - -class PropertyNotFoundException(Exception): - pass - - -async def get_property_by_id(id: str, property_name: str): - ial = await get_ial_by_id(id) - match = re.search(r" {}=\"(.+?)\"".format(property_name), - ial, flags=re.DOTALL) - if match is None: - raise PropertyNotFoundException(id, property_name) - res = match.group(1) - return res - - -async def do_property_exist_by_id(id: str, property_name: str): - # print(id) - ial = await get_ial_by_id(id) - return property_name in ial - - -async def get_sons_by_id(id: str): - return await query_sql(r"SELECT id FROM blocks WHERE parent_id='{}'".format(id)) - - -async def get_parent_by_id(id: str): - # print("get parent by id: {} parent: {}".format( - # id, get_col_by_id(id, "parent_id"))) - return await get_col_by_id(id, "parent_id") - - -async def set_attribute(id: str, attr: str, val: str): - return await post(API_URL + "attr/setBlockAttrs", id=id, attrs={attr: val}) - - -async def set_attributes(id: str, attrs): - return await post(API_URL + "attr/setBlockAttrs", id=id, attrs=attrs) - - -async def export_md_content(id: str) -> str: - result = await post(API_URL + "export/exportMdContent", id=id) - if result["code"] == 0: - return result["data"]["content"] diff --git a/setup.cfg b/setup.cfg index ecd4231..cd6bfd3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = SiyuanYuque -version = 0.0.7 +version = 0.0.8 author = Clouder author_email = clouder0@outlook.com description = Sync SiYuan with Yuque @@ -20,4 +20,5 @@ install_requires = yuque-py toml aiohttp + siyuanhelper python_requires = >=3.6