获取当前块、文档和笔记本信息 - 动作信息 - Quicker (getquicker.net)
import json
import requests
import os
from jsonpath_ng.ext import parse
import sys
class SiYunNoteBook:
def __init__(self, TokenID) -> None:
self.TokenID = TokenID
def Command(self, url: str, data=None, indent=False):
headers = {
"Content-Type": "application/javascript",
"Authorization": f"Token { self.TokenID }"
}
Result = requests.post(f"http://localhost:6806{ url }",
headers=headers, data=json.dumps(data))
if indent:
return (json.dumps(json.loads(Result.text), indent=True, ensure_ascii=False))
else:
return json.loads(Result.text)
def lsNotebooks(self) -> str:
return self.Command("/api/notebook/lsNotebooks")
def openNotebook(self, nbID: str):
return self.Command("/api/notebook/openNotebook", data={"notebook": nbID})
def closeNotebook(self, nbID: str):
return self.Command("/api/notebook/closeNotebook", data={"notebook": nbID})
def renameNotebook(self, nbID: str, newName: str):
return self.Command("/api/notebook/renameNotebook", data={"notebook": nbID, "name": newName})
def createNotebook(self, nb_name: str):
return self.Command("/api/notebook/createNotebook", data={"name": nb_name})
def removeNotebook(self, nbID: str):
return self.Command("/api/notebook/removeNotebook", data={"notebook": nbID})
def getNotebookConf(self, nbID: str, dict: dict):
return self.Command("/api/notebook/getNotebookConf", data={"notebook": nbID, "conf": dict})
def setNotebookConf(self, nbID):
return self.Command("/api/notebook/setNotebookConf", data={"notebook": nbID})
def createDocWithMd(self, nbID: str, path: str, markdown: str):
return self.Command("/api/filetree/createDocWithMd",
data={"notebook": nbID,
"path": path,
"markdown": markdown
})
def appendBlock(self, parentID: str, data: str):
return self.Command("/api/block/appendBlock",
data={"data": data,
"dataType": "markdown",
"parentID": parentID
})
def prependBlock(self, parentID: str, dataType: str, data):
return self.Command("/api/block/prependBlock",
data={"data": data,
"dataType": "markdown",
"parentID": parentID
})
def insertBlock(self, previousID: str, data: str):
return self.Command("/api/block/prependBlock",
data={
"dataType": "markdown",
"data": data,
"parentID": previousID
})
def pushMsg(self, Msg: str):
return self.Command("/api/notification/pushMsg",
data={"msg": Msg, "timeout": 7000}
)
def pushErrMsg(self, Msg: str):
return self.Command("/api/notification/pushErrMsg",
data={"msg": Msg, "timeout": 7000}
)
def version(self):
return self.Command("/api/system/version",
data={}
)
def exportMdContent(self, doc_id: str):
return self.Command("/api/export/exportMdContent",
data={"id": doc_id}
)
def getFileContent(self, file: str):
return self.Command("/api/file/getFile",
data={"path": file}
)
def render(self, doc_id: str, template_path: str):
return self.Command("/api/file/render",
data={"id": doc_id,
"path": template_path
}
)
def SQL_query(self, SQL: str):
return self.Command("/api/query/sql",
data={"stmt": SQL})
def getBlockAttrs(self, block_id: str):
return self.Command("/api/attr/getBlockAttrs",
data={"id": block_id})
def setBlockAttrs(self, block_id: str, dict: dict):
return self.Command("/api/attr/setBlockAttrs",
data={"id": block_id,
"attrs": dict
})
def getBlockKramdown(self, block_id: str):
return self.Command("/api/block/getBlockKramdown",
data={"id": block_id})
def deleteBlock(self, block_id: str):
return self.Command("/api/block/deleteBlock",
data={"id": block_id})
def updateBlock(self, doc_id: str, dataType: str, data):
return self.Command("/api/block/updateBlock",
data={"id": doc_id,
"dataType": dataType,
"data": data
}
)
def getDocumentIDbyBlock(self, block_id):
result = self.SQL_query(
f"SELECT root_id FROM blocks where id= '{ block_id }'")
return (result["data"][0]["root_id"])
def upload(self, imageFile):
headers = {
"Authorization": f"Token { self.TokenID }"
}
values = {"assetsDirPath": "/assets/"}
fields = {
"file[]": ('file.png', open(imageFile, 'rb'), 'jpeg/png')
}
Result = requests.post("http://localhost:6806/api/asset/upload",
headers=headers, files=fields, data=values)
return (json.loads(Result.text))
def getConf_Siyuan(self):
return self.Command("/api/system/getConf", data={})
TokenID: str = "{API_token}"
block_id: str = "{block_id}"
SY = SiYunNoteBook("SiYunNoteBook")
INFO = SY.SQL_query(
f'SELECT box,root_id,hpath, path FROM blocks where id= "{block_id}"')
NB_id: str = INFO["data"][0]["box"]
doc_id: str = INFO["data"][0]["root_id"]
hpath: str = INFO["data"][0]["hpath"]
title: str = os.path.basename(hpath) if hpath else ""
Result = SY.getConf_Siyuan()
dataDir: str = Result["data"]["conf"]["system"]["dataDir"]
filePath: str = f'{dataDir}\\{NB_id}{INFO["data"][0]["path"]}'.replace(
"/", "\\")
INFO: str = {
"当前块ID": block_id,
"当前笔记本ID": NB_id,
"当前文档ID": doc_id,
"当前文档标题": title,
"文档逻辑路径": hpath,
"文档物理路径": filePath
}
print(json.dumps(INFO, indent=2, ensure_ascii=False))
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于