#!/usr/bin/env python3 import json import os from datetime import datetime # 读取视觉设计岗位简历.json文件 json_file = '网页未导入数据/视觉设计产业/视觉设计岗位简历.json' mock_file = 'src/mocks/resumeInterviewMock.js' print("正在读取面试题数据...") with open(json_file, 'r', encoding='utf-8') as f: positions_data = json.load(f) # 读取当前mock文件 with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() # 创建备份 backup_file = f"{mock_file}.backup_simple_{datetime.now().strftime('%Y%m%d_%H%M%S')}" with open(backup_file, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份:{backup_file}") # 要更新的产业列表 industries = [ ("包装设计师", "包装设计", "q2", "group_q2"), ("插画师", "插画设计", "q3", "group_q3"), ("影视灯光", "灯光", "q4", "group_q4"), ("角色原画师", "动画设计", "q5", "group_q5"), ("特效设计师", "后期特效", "q6", "group_q6"), ("剪辑师", "剪辑", "q7", "group_q7"), ("品牌视觉内容策划", "品牌设计", "q8", "group_q8"), ("平面设计师", "平面设计", "q9", "group_q9"), ("3D建模师", "三维设计", "q10", "group_q10"), ("影视摄像", "摄影/摄像", "q11", "group_q11"), ("室内设计师", "室内设计", "q12", "group_q12"), ("调色师", "调色", "q13", "group_q13"), ("自媒体运营专员", "新媒体运营", "q14", "group_q14"), ("音效设计师", "音频处理", "q15", "group_q15"), ("导演", "影视节目策划", "q16", "group_q16"), ("直播运营", "直播", "q17", "group_q17"), ] updated_count = 0 for position_name, industry_name, q_prefix, group_id in industries: print(f"\n处理 {industry_name}...") # 提取该岗位的面试题 questions = [] for position in positions_data: if position.get("岗位名称") == position_name: interview_content = position.get("面试题内容", "") if not interview_content: continue lines = interview_content.split('\n') current_question = None current_answer = '' collecting_answer = False for line in lines: line_stripped = line.strip() # 检测问题行 if line_stripped and line_stripped[0].isdigit() and ('问题' in line_stripped or ':' in line_stripped): # 保存上一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: answer = answer[:400] + "..." questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) # 提取新问题 if '问题:' in line_stripped: current_question = line_stripped.split('问题:', 1)[1].strip() elif '问题:' in line_stripped: current_question = line_stripped.split('问题:', 1)[1].strip() elif '. ' in line_stripped: parts = line_stripped.split('. ', 1) if len(parts) > 1: current_question = parts[1].strip() current_answer = '' collecting_answer = False # 检测答案开始 elif any(marker in line for marker in ['参考回答:', '参考答案:', '答案:']): collecting_answer = True for marker in ['参考回答:', '参考答案:', '答案:']: if marker in line: answer_part = line.split(marker, 1)[1].strip() if answer_part: current_answer = answer_part break # 收集答案内容 elif collecting_answer and line.strip(): if line_stripped.startswith('#') or (line_stripped and line_stripped[0].isdigit() and '问题' in line_stripped): collecting_answer = False else: if current_answer: current_answer += ' ' current_answer += line.strip() # 保存最后一个问题 if current_question: answer = current_answer.strip() if len(answer) > 400: answer = answer[:400] + "..." questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) break if not questions: print(f" ✗ 未找到 {position_name} 的面试题") continue # 限制为最多16个问题 if len(questions) > 16: questions = questions[:16] print(f" 提取了 {len(questions)} 个问题") # 构建subQuestions sub_questions = [] for i, qa in enumerate(questions, 1): sub_questions.append({ "id": f"{q_prefix}_{i}", "question": qa['question'].replace('"', '\\"'), "answer": qa['answer'].replace('"', '\\"') }) # 查找并替换 # 找到 "# 一、专业能力与行业认知" 这种占位内容并替换为新内容 old_pattern = f'"id": "{group_id}",\\n "question": "# 一、专业能力与行业认知",\\n "subQuestions": \\[\\n {{\\n "id": "{q_prefix}_1",\\n "question": "[^"]+",\\n "answer": "[^"]+"\n }}\\n \\]' # 构建新的questions内容 sub_questions_str = json.dumps(sub_questions, ensure_ascii=False, indent=10) new_questions = f'''{{ "id": "{group_id}", "question": "# {industry_name}面试题", "subQuestions": {sub_questions_str} }}''' # 使用简单的字符串替换 search_str = f'"id": "{group_id}",' if search_str in content: # 找到这个ID的位置 pos = content.find(search_str) # 找到这个对象的结束位置(下一个 } 且后面跟着 ]) end_pos = pos bracket_count = 1 found_start = False for i in range(pos, len(content)): if content[i] == '{' and not found_start: found_start = True elif content[i] == '{': bracket_count += 1 elif content[i] == '}': bracket_count -= 1 if bracket_count == 0: end_pos = i + 1 break if end_pos > pos: # 提取原始内容 old_content = content[pos:end_pos] # 检查是否只有一个子问题(占位符) if '"q' + q_prefix[-1] + '_1"' in old_content and '"q' + q_prefix[-1] + '_2"' not in old_content: print(f" 更新 {industry_name} 的面试题...") # 替换为新内容 content = content[:pos-8] + new_questions + content[end_pos:] updated_count += 1 print(f" ✓ 已更新") # 写入文件 if updated_count > 0: with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) # 验证语法 print(f"\n验证语法...") result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 语法错误:{result}") # 恢复备份 with open(backup_file, 'r', encoding='utf-8') as f: content = f.read() with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print("已恢复备份") else: print("✓ 语法验证通过") print(f"\n成功更新 {updated_count} 个产业的面试题!") else: print("\n未更新任何产业")