博客地址https://jameshoi.github.io/
要跟语雀说再见了,最爱这个能每篇文章显示封面的功能,但是在语雀写文章再转pdf格式实在是太烂了,导出成markdown图片还保留cdn的图片格式,而且还不能批量导出文章。
最后我自己写了个脚本解决这个问题,贴到这里给后人作为参考吧
import requestsimport jsonimport osimport timefrom urllib.parse import urlparse,urlsplit,parse_qs,parse_qslTOKEN = "" #这里填你的tokenNAMESPACE = "jameshoi"SAVE_PATH = "./source/"DOWNLOAD_FILE_PATH = "https://jameshoi.github.io/files/"TIMEOUT = 5def download_file(url,filename):while True:try:response=requests.get(url,timeout=TIMEOUT)breakexcept Exception as e:time.sleep(2)with open(filename,'wb') as f: f.write(response.content)def get_all_md_info(doc_path):url = "https://www.yuque.com/api/v2/repos/jameshoi/{}/docs".format(doc_path)headers = {'Content-Type': 'application/json','X-Auth-Token': TOKEN}response = requests.get(url, headers=headers,timeout=TIMEOUT)return json.loads(response.content)def download_md(doc_path,doc_name):optional = "attachment=true&latexcode=false&anchor=false&linebreak=false"url = "https://www.yuque.com/jameshoi/{}/{}/markdown?{}".format(doc_path,doc_name,optional)download_file(url,SAVE_PATH+doc_name+".md")def save_img(doc_name,url):filename = urlparse(url).path.split("/")[-1]if not os.path.exists(SAVE_PATH+doc_name): os.mkdir(SAVE_PATH+doc_name)download_file(url, SAVE_PATH+doc_name+"/"+filename)return filenamedef replace_img_url(doc_name):with open(SAVE_PATH+doc_name+".md","rb+") as f:content = f.read().decode()start = 0; url_list = []while True:head = content.find("![",start)mid = content.find("(",head)+1end = content.find(")",mid)if head == -1 or mid == 0 or end == -1: breakurl = content[mid:end]; start = endfilename = save_img(doc_name, url)url_list.append([url, doc_name+"/"+filename])for [url,path] in url_list: content = content.replace(url,path)with open(SAVE_PATH+doc_name+".md","wb+") as f: f.write(content.encode())def add_md_info(doc_name,title,date):data = "---\ntitle: {}\ndate: {}\n---\n".format(title,date)with open(SAVE_PATH+doc_name+".md","rb+") as f:old = f.read(); f.seek(0)f.write(data.encode()); f.write(old)def replace_file_url(doc_name):with open(SAVE_PATH+doc_name+".md","rb+") as f:content = f.read().decode()start = 0; url_list = []while True:head = content.find("[",start)mid = content.find("(",head)+1end = content.find(")",mid)if head == -1 or mid == 0 or end == -1: breakurl = content[mid:end]; start = endif content[mid:mid+33] != "https://www.yuque.com/attachments": continuefilename = content[head+1:mid-2]url_list.append([url, DOWNLOAD_FILE_PATH+doc_name+"/"+filename])for [url,path] in url_list: content = content.replace(url,path)with open(SAVE_PATH+doc_name+".md","wb+") as f: f.write(content.encode())def yuque_main():DOC_PATH = ["writeup","default"]for doc_path in DOC_PATH:all_info = get_all_md_info(doc_path)total = len(all_info["data"]); i = 0for info in all_info["data"]:doc_name = info["slug"]; i += 1date = info["created_at"]title = info["title"]print("[{}][{}/{}]正在下载 {}.md".format(doc_path,i,total,doc_name))download_md(doc_path, doc_name)replace_img_url(doc_name)add_md_info(doc_name, title, date)replace_file_url(doc_name)if __name__ == '__main__':yuque_main()
