需求
导出语雀的全部文件,并导出为markdown格式。
技术
python,本人是学的是java并不会python,所以不会介绍太多的技术
流程
1. 安装python环境,参考其他文章
https://www.bilibili.com/video/BV1qW4y1a7fU?p=4&vd_source=a683c51673975db4bf588857848354aa
2. 刚安装的python缺少两个包,导入这两个包*
打开cmd,运行如下命令,安装requests包原理相同
C:\Users\wcb>pip install psutil -i https://pypi.tuna.tsinghua.edu.cn/simple
3. 创建所需文件
登录网页版语雀,获取语雀token链接 👉https://www.yuque.com/settings/tokens/new
{
"TOKEN": "语雀token", # 用户token
"USER_AGENT": "Rion", # 就是一个用户名称,可以随意写,但必须要有
"BASE_URL": "https://customspace.yuque.com/api/v2", # 语雀基础api
"DATA_PATH": "yuque" # 数据储存的文件夹名称(默认当前文件夹下创建)
}
4.创建实现文件(test.py)
还记得第二布吗,刚安装的环境运行下面的代码缺包
"""
这是一个导出语雀库中所有文档的脚本, 并将文件转为md格式
"""
import json
import sys
import os
import re
import requests
import psutil
from datetime import datetime
if getattr(sys, 'frozen', False):
APPLICATION_PATH = os.path.dirname(sys.executable)
else:
APPLICATION_PATH = os.path.dirname('.')
jsonConfig = json.load(open(os.path.join(APPLICATION_PATH, "config.json"), encoding='utf-8'))
class ExportYueQueDoc:
def __init__(self):
try:
if getattr(sys, 'frozen', False):
APPLICATION_PATH = os.path.dirname(sys.executable)
else:
APPLICATION_PATH = os.path.dirname('.')
self.jsonConfig = json.load(open(os.path.join(APPLICATION_PATH, "config.json"), encoding='utf-8'))
self.base_url = self.jsonConfig['BASE_URL']
self.token = self.jsonConfig['TOKEN']
self.headers = {
"User-Agent": self.jsonConfig['USER_AGENT'],
"X-Auth-Token": self.jsonConfig['TOKEN']
}
self.data_path = self.jsonConfig['DATA_PATH']
except:
raise ValueError("config.json 有误")
def get_user_info(self):
"""获取用户信息"""
res_obj = requests.get(url=self.base_url + '/user', headers=self.headers)
if res_obj.status_code != 200:
raise ValueError("Token 信息错误")
user_json = res_obj.json()
self.login_id = user_json['data']['login']
self.uid = user_json['data']['id']
self.username = user_json['data']['name']
print("=========== 用户信息初始化成功 ==========")
def get_repos_data(self):
"""获取知识库"""
repos_json = requests.get(self.base_url + '/users/' + self.login_id + '/repos', headers=self.headers).json()
repos_list = []
for item in repos_json['data']:
rid = item['id'] # 知识库id
name = item['name'] # 知识库名称
repos_list.append({"rid": rid, "repos_name": name})
return repos_list
def get_article_data(self, repos_list):
"""获取文章数据"""
article_list = []
for repos in repos_list:
article_datas = requests.get(self.base_url + '/repos/' + str(repos['rid']) + '/docs',
headers=self.headers).json()
for item in article_datas['data']:
bid = repos['rid']
title = item['title'] # 文章标题
desc = item['description']
slug = item['slug']
article_list.append(
{"bid": bid, "title": title, "desc": desc, "slug": slug, "repos_name": repos["repos_name"]})
for item in article_list:
per_article_data = requests.get(self.base_url + '/repos/' + str(item['bid']) + '/docs/' + item['slug'],
headers=self.headers).json()
posts_text = re.sub(r'\\n', "\n", per_article_data['data']['body'])
result = re.sub(r'<a name="(.*)"></a>', "", posts_text)
# all_datas.append({"title": item['title'], "content": result})
yield result, item["repos_name"], item['title']
def save_article(self, result, repos_name, title):
"""写入文章"""
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
dir_path = f"{self.data_path}/{repos_name}"
filepath = dir_path + f"/{title}.md"
dir_ret = os.path.exists(dir_path)
if not dir_ret:
os.makedirs(dir_path)
exists_ret = os.path.exists(filepath)
if exists_ret:
os.remove(filepath)
try:
with open(filepath, 'a', encoding="utf-8") as fp:
fp.writelines(result)
print(f"[{current_time}] {title} 写入完成")
except Exception as e:
print(f"[{current_time}] {title} 写入失败")
def main(self):
self.get_user_info()
repos_list = self.get_repos_data()
gen_obj = self.get_article_data(repos_list)
for item in gen_obj:
self.save_article(item[0], item[1], item[2])
if __name__ == "__main__":
yq = ExportYueQueDoc()
yq.main()
5.运行实现文件
这里就不用ide了,毕竟没学过的人也不必特意安装个pycharm
PS E:\Desktop\pythonProject> python .\test.py
导出指定知识库所有内容
稍微修改一下代码