# 确保音频目录存在 # Ensure the audio directory exists os.makedirs(AUDIO_DIR, exist_ok=True)
# -------------------------------------------------------------- # 阿里云 TTS API 相关类和函数 # Aliyun TTS API related classes and functions # --------------------------------------------------------------
defget_aliyun_token(): """ 使用 AccessKey ID 和 AccessKey Secret 获取阿里云语音服务的 Token。 Uses AccessKey ID and AccessKey Secret to get a token for Aliyun Speech Service. """ if ALIYUN_AK_ID == 'your_ak_id'or ALIYUN_AK_SECRET == 'your_ak_secret': print("错误:请先在脚本中填写你的 ALIYUN_AK_ID 和 ALIYUN_AK_SECRET。") sys.exit(1)
defremove_tag(content): """ 接收全文内容,删除旧的meting HTML代码和meting Hexo控件代码,返回清理后的文本。 Receives full text content, removes old meting HTML and Hexo tags, and returns the cleaned text. """ # 匹配 aplayer 和 meting 标签的正则表达式模式,现在也包括了 meting-js HTML标签 # Regex pattern to match aplayer and meting tags, now also includes meting-js HTML tags player_pattern = r'\{%\s*(aplayer|meting).*?%}\s*|
# 文章文件夹的相对路径 # Relative path to the posts directory POSTS_DIR = './source/_posts' # 音频文件夹的相对路径,确保它与你的音频文件位置匹配 # Relative path to the audio directory, make sure it matches your audio file location AUDIO_DIR = './source/audio'
# 请在这里填入你的封面图片URL # Please fill in your cover image URL here # Example: COVER_URL = "https://cdn.jsdelivr.net/gh/example/image.jpg" COVER_URL = "https://snowmiku-blog-1326916956.cos.ap-hongkong.myqcloud.com/Screenshot_2024-08-15-00-35-26-521_com.tencent.mm.jpg?imageSlim"
defremove_and_add_meting_js_tag(): """ 遍历Markdown文件,移除旧的APlayer和Meting标签,并添加新的Meting-js HTML标签。 Iterates through Markdown files, removes old APlayer and Meting tags, and adds new Meting-js HTML tags. """ # 匹配 aplayer 和 meting 标签的正则表达式模式,现在也包括了 meting-js HTML标签 # Regex pattern to match aplayer and meting tags, now also includes meting-js HTML tags player_pattern = r'\{%\s*(aplayer|meting).*?%}\s*| ''' # 查找 <!-- more --> 标签 # Find the <!-- more --> tag more_match = re.search(more_pattern, content)
if more_match: # 如果找到了 <!-- more --> 标签,在其后插入 meting 标签 # If the <!-- more --> tag is found, insert the meting tag after it more_tag_end = more_match.end() new_content = content[:more_tag_end] + '\n\n' + meting_js_tag + content[more_tag_end:] else: # 如果没有找到 <!-- more --> 标签,则在 front-matter 之后插入 # If the <!-- more --> tag is not found, insert after the front-matter match = front_matter_pattern.search(content) ifmatch: front_matter = match.group(1) body = content[match.end():] new_content = front_matter + '\n\n' + meting_js_tag + body else: # 如果没有 Front-matter,则直接加到开头 # If there's no Front-matter, add it directly to the beginning new_content = meting_js_tag + content
withopen(md_path, 'w', encoding='utf-8') as f: f.write(new_content) print(f"-> 成功更新 {filename},已替换为 Meting-js HTML 标签。")
except Exception as e: print(f"处理文件 {filename} 时出错: {e}")
if __name__ == "__main__": if COVER_URL == "https://example.com/your-default-cover.jpg": print("警告:请在脚本中设置 COVER_URL,否则无法正常工作!") else: remove_and_add_meting_js_tag()
# APIKey定义 APIKey = 'sk-******' # -*- coding: utf-8 -*- import os import re import time from openai import OpenAI
# ==================== # 配置部分 # Configuration Section # ==================== # 请将此路径替换为你的Hexo博客文章目录,通常是 'source/_posts' # Please replace this with the path to your Hexo blog posts, usually 'source/_posts' POSTS_DIR = 'source/_posts' # 传递给大模型进行摘要生成的文章内容最大字符数 # Maximum number of characters for the summary to be sent to the LLM SUMMARY_CHARS_LIMIT = 250
# 配置通义千问 API # Configure Tongyi Qianwen API try: client = OpenAI( # 如果没有配置环境变量,请用你的阿里云百炼API Key替换os.getenv() # Replace with your actual Dashscope API Key if not set as an environment variable api_key=APIKey, base_url="https://dashscope.aliyuncs.com/compatible-mode/v1", ) except Exception as e: print(f"Failed to initialize Tongyi Qianwen client: {e}") client = None
# 用于匹配和查找 '<!-- more -->' 标签的字符串 # String to match and find the '<!-- more -->' tag MORE_TAG = '<!-- more -->' # 用于匹配 Front-matter 块的正则表达式 # Regex to match the Front-matter block at the beginning of the file FRONT_MATTER_PATTERN = re.compile(r'^(---[\s\S]*?---)\s*', re.MULTILINE) # 用于匹配 Hexo 的 APlayer 标签的正则表达式 # Regex to match Hexo's APlayer tags APLAYER_TAG_PATTERN = re.compile(r'\{\s*%\s*aplayer.*?%\s*\}', re.DOTALL) # 用于匹配 Markdown 代码块的正则表达式 # Regex to match markdown code blocks CODE_BLOCK_PATTERN = re.compile(r'```[\s\S]*?```', re.DOTALL) # 新增:用于匹配 meting-js HTML 标签和 meting Hexo 标签的正则表达式 # New: Regex to match both meting-js HTML tags and meting Hexo tags METING_PLAYER_PATTERN = re.compile(r'\{\s*%\s*meting.*?%\s*\}|<meting-js[\s\S]*?<\/meting-js>', re.DOTALL)