import configparser import getpass import logging import os # 配置文件路径 CONFIG_FILE = "config.ini" # 默认配置 DEFAULT_CONFIG = { "General": { "chrome_user_dir": f"C:\\Users\\{getpass.getuser()}\\AppData\\Local\\Google\\Chrome\\User Data", "articles_path": "articles", "images_path": "picture", "title_file": "文章链接.xlsx", "max_threads": "3" }, "Coze": { "workflow_id": "", "access_token": "", "is_async": "false", "input_data_template": "{\"article\": \"{article_text}\", \"link\":\"{link}\", \"weijin\":\"{weijin}\"}", "last_used_template": "", "last_used_template_type": "文章" }, "Database": { "host": "27.106.125.150", "user": "root", "password": "taiyi.1224", "database": "toutiao" }, "Dify": { "api_key": "app-87gssUKFBs9BwJw4m95uUcyF", "user_id": "toutiao", "url": "http://27.106.125.150/v1/workflows/run" }, "Baidu": { "api_key": "", "secret_key": "" }, "ImageModify": { "crop_percent": "0.02", "min_rotation": "0.3", "max_rotation": "3.0", "min_brightness": "0.8", "max_brightness": "1.2", "watermark_text": "Qin Quan Shan Chu", "watermark_opacity": "128", "overlay_opacity": "30" }, "Keywords": { "banned_words": "珠海,落马,股票,股市,股民,爆炸,火灾,死亡,抢劫,诈骗,习大大,习近平,政府,官员,扫黑,警察,落网,嫌疑人,通报,暴力执法,执法,暴力,气象,天气,暴雨,大雨" } } # 加载配置 def load_config(): config = configparser.ConfigParser() # 如果配置文件不存在,创建默认配置 if not os.path.exists(CONFIG_FILE): for section, options in DEFAULT_CONFIG.items(): config[section] = options with open(CONFIG_FILE, 'w', encoding='utf-8') as f: config.write(f) else: config.read(CONFIG_FILE, encoding='utf-8') # 检查并添加缺失的配置项 for section, options in DEFAULT_CONFIG.items(): if not config.has_section(section): config[section] = {} for option, value in options.items(): if not config.has_option(section, option): config[section][option] = value # 保存更新后的配置 with open(CONFIG_FILE, 'w', encoding='utf-8') as f: config.write(f) return config # 保存配置 def save_config(config): with open(CONFIG_FILE, 'w', encoding='utf-8') as f: config.write(f) # 加载配置 CONFIG = load_config() # 更新全局变量 USER_DIR_PATH = CONFIG['General']['chrome_user_dir'] ARTICLES_BASE_PATH = CONFIG['General']['articles_path'] IMGS_BASE_PATH = CONFIG['General']['images_path'] TITLE_BASE_PATH = CONFIG['General']['title_file'] MAX_THREADS = int(CONFIG['General']['max_threads']) # 创建必要的目录 if not os.path.exists(ARTICLES_BASE_PATH): os.makedirs(ARTICLES_BASE_PATH) os.chmod(ARTICLES_BASE_PATH, 0o777) if not os.path.exists(IMGS_BASE_PATH): os.makedirs(IMGS_BASE_PATH) os.chmod(IMGS_BASE_PATH, 0o777) # 日志配置 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler("article_replace.log", encoding='utf-8'), logging.StreamHandler() ]) logger = logging.getLogger(__name__) # 日志文件保存路径 LOG_FILE = "article_replace.log"