之前一直使用 Typora 来进行笔记和写作,后来接触并爱上了语雀,主要是贴图太方便了。刚开始用语雀的时候还特地看了下,可以导出 Markdown 格式的文章。但后来想批量导出时发现只能选择 PDF 或者语雀特定的格式,感觉不大放心。
于是根据语雀官方 API 写了个脚本,主要功能就是批量导出文章以及相关的图片和附件等资源
相关代码已开源至 https://github.com/M1r0ku/YuqueExport
用法:使用浏览器访问 语雀-Token 页面,新建一个读取权限的密钥,然后将该密钥填入脚本中的token变量,最后执行脚本即可
Tips:现在语雀需要开通超级会员才可以创建 Token,但是之前已创建的 Token,非会员也可以继续使用。
补一张运行截图:
YuqueExport-运行截图
完整代码如下:
import sys
import re
import os
import asyncio
import aiohttp
from urllib import parse
from pyuque.client import Yuque
from huepy import *
from prettytable import PrettyTable
def get_repos(user_id):
repos = {}
for repo in yuque.user_list_repos(user_id)['data']:
repo_id = str(repo['id'])
repo_name = repo['name']
repos[repo_id] = repo_name
return repos
def get_docs(repo_id):
docs = {}
for doc in yuque.repo_list_docs(repo_id)['data']:
doc_id = str(doc['id'])
doc_title = doc['title']
docs[doc_id] = doc_title
return docs
def get_body(repo_id, doc_id):
doc = yuque.doc_get(repo_id, doc_id)
body = doc['data']['body']
body = re.sub("<a name=\"(\w.*)\"></a>", "", body)
body = re.sub(r'\<br \/\>', "\n", body)
body = re.sub(r'\<br \/\>!\[image.png\]', "\n![image.png]", body)
body = re.sub(r'\)\<br \/\>', ")\n", body)
body = re.sub(r'png[#?](.*)+', 'png)', body)
body = re.sub(r'jpeg[#?](.*)+', 'jpeg)', body)
return body
async def download_md(repo_id, repo_name, doc_id, doc_title):
body = get_body(repo_id, doc_id)
repo_dir = os.path.join(base_dir, repo_name)
make_dir(repo_dir)
assets_dir = os.path.join(repo_dir, "assets")
make_dir(assets_dir)
pattern_images = r'(\!\[(.*)\]\((https:\/\/cdn\.nlark\.com\/yuque.*\/(\d+)\/(.*?\.[a-zA-z]+)).*\))'
images = [index for index in re.findall(pattern_images, body)]
if images:
for index, image in enumerate(images):
image_body = image[0]
image_url = image[2]
image_suffix = image_url.split(".")[-1]
local_abs_path = f"{assets_dir}/{doc_title}-{str(index)}.{image_suffix}"
doc_title_temp = doc_title.replace(" ", "%20").replace("(", "%28").replace(")", "%29")
local_md_path = f"}.{image_suffix})"
await download_images(image_url, local_abs_path)
body = body.replace(image_body, local_md_path)
pattern_annexes = r'(\[(.*)\]\((https:\/\/www\.yuque\.com\/attachments\/yuque.*\/(\d+)\/(.*?\.[a-zA-z]+)).*\))'
annexes = [index for index in re.findall(pattern_annexes, body)]
if annexes:
for index, annex in enumerate(annexes):
annex_body = annex[0]
annex_name = annex[1]
annex_url = re.findall(r'\((https:\/\/.*?)\)', annex_body)
annex_url = annex_url[0].replace("/attachments/", "/api/v2/attachments/")
local_abs_path = f"{assets_dir}/{annex_name}"
local_md_path = f"[{annex_name}](assets/{annex_name})"
await download_annex(annex_url, local_abs_path)
body = body.replace(annex_body, local_md_path)
markdown_path = f"{repo_dir}/{doc_title}.md"
with open(markdown_path, "w", encoding="utf-8") as f:
f.write(body)
doc_title_temp = doc_title.replace(" ","%20").replace("(","%28").replace(")","%29")
record_doc_file = os.path.join(base_dir, f"{repo_name}.md")
record_doc_output = f"- [{doc_title}](./{repo_name}/{doc_title_temp}.md) \n"
with open(record_doc_file, "a+") as f:
f.write(record_doc_output)
async def download_images(image, local_name):
print(good(f"Download {local_name} ..."))
async with aiohttp.ClientSession() as session:
async with session.get(image) as resp:
with open(local_name, "wb") as f:
f.write(await resp.content.read())
async def download_annex(annex, local_name):
print(good(f"Download {local_name} ..."))
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"X-Auth-Token": token
}
async with aiohttp.ClientSession() as session:
async with session.get(annex, headers=headers) as resp:
with open(local_name, "wb") as f:
f.write(await resp.content.read())
def make_dir(path):
if not os.path.exists(path):
os.makedirs(path)
print(info(f"Make Dir {path} ..."))
async def main():
user_id = yuque.user.get()['data']['id']
all_repos = get_repos(user_id)
repos_table = PrettyTable(["ID", "Name"])
for repo_id, repo_name in all_repos.items():
repos_table.add_row([repo_id, repo_name])
print(repos_table)
input_ids = input(lcyan("Repo ID (Example: 111,222): "))
temp_ids = [ temp.strip() for temp in input_ids.split(",") ]
for temp_id in temp_ids:
if temp_id not in all_repos:
print(bad(red(f"Repo ID {temp_id} Not Found !")))
sys.exit(0)
for temp_id in temp_ids:
repo = {temp_id: all_repos[temp_id]}
for repo_id, repo_name in repo.items():
all_docs = get_docs(repo_id)
print(cyan(f"\n===== {repo_name}: {len(all_docs)} docs ===== "))
docs_table = PrettyTable(["Doc", "Title"])
for doc_id, doc_title in all_docs.items():
docs_table.add_row([doc_id, doc_title])
print(docs_table)
input_doc_ids = input(lcyan("Doc ID (Example: 111,222 or ALL): "))
temp_doc_ids = [temp.strip() for temp in input_doc_ids.split(",")]
is_all = "all" in [temp.lower() for temp in temp_doc_ids]
if not is_all:
temp_docs = dict()
for temp_doc_id in temp_doc_ids:
try:
temp_docs[temp_doc_id] = all_docs[temp_doc_id]
except KeyError:
print(bad(red(f"Doc ID {temp_doc_id} Not Found !!")))
all_docs = temp_docs
for doc_id, doc_title in all_docs.items():
for char in r'/\<>?:"|*':
doc_title = doc_title.replace(char, parse.quote_plus(char))
print(run(cyan(f"Get Doc {doc_title} ...")))
await download_md(repo_id, repo_name, doc_id, doc_title)
if __name__ == '__main__':
token = "<Your_Yuque_Token>"
yuque = Yuque(token)
base_dir = "./YuqueExport"
asyncio.run(main())