以下代码实现了读取某一目录下(包含子目录)所有md格式文件,批量导入到show doc的项目中。
注意"api_key"和“api_token”需要替换成你自己的项目,可以在show doc设置中的开放API部分查看。
import requests
import os
path = "Your markdown directory"
filetype = ".md"
def get_file_path_by_name(file_dir):
fp = []
fn = []
for root, dirs, files in os.walk(file_dir): # 获取所有文件
for file in files: # 遍历所有文件名
if os.path.splitext(file)[1] == filetype:
fp.append(os.path.join(root, file)) # 拼接处绝对路径并放入列表
fn.append(file)
return fp, fn
allfile, fileName = get_file_path_by_name(path)
for i in range(len(allfile)):
contents = ""
with open(allfile[i], 'r', encoding="UTF-8", errors="ignore") as f:
contents += f.read()
url = "https://www.showdoc.cc/server/api/item/updateByApi"
data = {"api_key": "Your api_key",
"api_token": "Your api_token", "page_title": fileName[i], "page_content": contents}
res = requests.post(url=url, data=data)
print(res.text)