1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| ''' 飞书文档转成博客文档 ''' import streamlit as st import os from datetime import datetime import re
class Feishu2Blog(): def __init__(self): self.base_path = "/cloudide/workspace/MyBlogRepository/MyBlogRepository/hexo-blog" self.blog_file_path = f"{self.base_path}/source/_posts" pass
def config_feishu2md(self , app_id , app_secret): os.chdir(self.base_path) os.system(f"./feishu2md config --appId {app_id} --appSecret {app_secret}")
def read_file_in_dir(self, dir_path): file_list = [] for root, dirs, files in os.walk(dir_path): for file in files: file_list.append(os.path.join(root, file)) return file_list
def extract_token_from_feishu_url(self, feishu_file_url): token = feishu_file_url.split('?')[0].split('/')[-1] return token
def download_feishu_doc(self, doc_url, doc_name): token = self.extract_token_from_feishu_url(doc_url) os.chdir(self.base_path) try: os.system(f"./feishu2md dl -o {self.blog_file_path}/{token} {doc_url}") except Exception as e: st.error(f"下载失败:{e}") src_blog_file_name_list = self.read_file_in_dir(f"{self.blog_file_path}/{token}") os.rename(src_blog_file_name_list[0], f"{self.blog_file_path}/{doc_name}.md")
image_static_file_name_list = self.read_file_in_dir(f"{self.blog_file_path}/{token}/static") for image_static_file_name in image_static_file_name_list: imgage_name = image_static_file_name.split('/')[-1] os.rename(image_static_file_name, f"{self.blog_file_path}/{doc_name}/{imgage_name}")
if os.path.exists(f"{self.blog_file_path}/{token}/static"): os.rmdir(f"{self.blog_file_path}/{token}/static") if os.path.exists(f"{self.blog_file_path}/{token}"): os.rmdir(f"{self.blog_file_path}/{token}")
return f"{self.blog_file_path}/{doc_name}.md"
def save_uploaded_file(self, uploaded_file, directory="uploaded_files"): image_home_file_path = f'{self.blog_file_path}/{directory}' if not os.path.exists(image_home_file_path): os.makedirs(image_home_file_path)
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
image_home_file_name = f"{uploaded_file.name.split('.')[0]}_{timestamp}.{uploaded_file.name.split('.')[-1]}" file_path = os.path.join(image_home_file_path, image_home_file_name)
with open(file_path, "wb") as f: f.write(uploaded_file.getbuffer())
return image_home_file_name
def write_blog_header(self, blog_title, blog_tags, blog_category, home_image_file_name): date = datetime.now().strftime("%Y-%m-%d %H:%M:%S") if home_image_file_name is None: blog_header = f"---\ntitle: {blog_title}\ndate: {date}\ntags: {blog_tags}\ncategories: {blog_category}\n---\n" else: blog_header = f"---\ntitle: {blog_title}\ndate: {date}\ntags: {blog_tags}\ncategories: {blog_category}\ncover: {home_image_file_name}\n---\n" return blog_header
def build_blog_content(self, doc_content): image_static_file_name_list = re.findall(r'!\[\]\(.*static/.*?\)', doc_content) for image_static_file_name in image_static_file_name_list: doc_content = doc_content.replace(image_static_file_name, f"![]({image_static_file_name.split('/')[-1]})") return doc_content
def read_feishu_doc(self, doc_path): with open(doc_path, 'r', encoding='utf-8') as f: doc_content = f.read() return doc_content
def generate_blog_file(self, blog_title, blog_tags, blog_category, home_image_file_name): blog_file = f"{self.blog_file_path}/{blog_title}.md" doc_content = self.read_feishu_doc(doc_path=blog_file) blog_content = self.write_blog_header(blog_title, blog_tags, blog_category, home_image_file_name) + self.build_blog_content(doc_content) with open(blog_file, 'w', encoding='utf-8') as f: f.write(blog_content)
def create_blog(self, blog_title): os.chdir(self.base_path) os.system(f"hexo new {blog_title}")
def delopy(self): os.chdir(self.base_path) os.system("hexo clean && hexo generate && hexo deploy")
def main(self): st.title("飞书文档到博客发布工具")
app_id = st.sidebar.text_input("飞书应用的 App ID", help="请输入飞书应用的 App ID") app_secret = st.sidebar.text_input("飞书应用的 App Secret", type="password", help="请输入飞书应用的 App Secret")
if app_id and app_secret: doc_url = st.text_input("请输入飞书文档 URL") blog_title = st.text_input("博客标题") home_image = st.file_uploader("主页图片", type=["png", "jpg", "jpeg"], accept_multiple_files=False) blog_tags = st.text_input("博客标签(用逗号分隔)") blog_category = st.text_input("博客分类(用逗号分隔)")
if st.button("生成并发布"): if not doc_url or not blog_title: st.error("请填写所有必填项:飞书文档 URL 和 博客标题") else: home_image_file_name = None if home_image: home_image_file_name = self.save_uploaded_file(home_image , blog_title) with st.spinner('生成并发布博客中...'): self.config_feishu2md(app_id, app_secret) self.create_blog(blog_title) self.download_feishu_doc(doc_url, blog_title) self.generate_blog_file(blog_title , blog_tags, blog_category, home_image_file_name) self.delopy() st.success("博客发布成功!") else: st.error("请输入飞书应用的 App ID 和 App Secret")
if __name__ == "__main__": feishu2blog = Feishu2Blog() feishu2blog.main()
|