#!/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) def extract_questions_from_position(position_name): """从指定岗位提取面试题""" for position in positions_data: if position.get("岗位名称") == position_name: interview_content = position.get("面试题内容", "") if not interview_content: return [] questions = [] 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) > 500: # 找到合适的截断点 cut_point = 500 for sep in ['。', ';', '. ', ',']: pos = answer[:500].rfind(sep) if pos > 300: cut_point = pos + 1 break answer = answer[:cut_point] questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) # 提取新问题 current_question = None if '问题:' in line_stripped: parts = line_stripped.split('问题:', 1) current_question = parts[1].strip() if len(parts) > 1 else None elif '问题:' in line_stripped: parts = line_stripped.split('问题:', 1) current_question = parts[1].strip() if len(parts) > 1 else None elif '. ' in line_stripped: parts = line_stripped.split('. ', 1) current_question = parts[1].strip() if len(parts) > 1 else None 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) > 500: cut_point = 500 for sep in ['。', ';', '. ', ',']: pos = answer[:500].rfind(sep) if pos > 300: cut_point = pos + 1 break answer = answer[:cut_point] questions.append({ 'question': current_question, 'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。" }) # 限制为最多16个问题 return questions[:16] return [] def update_industry_questions(industry_name, position_name, q_prefix, group_id): """更新指定产业的面试题""" print(f"\n处理 {industry_name}...") # 提取面试题 questions = extract_questions_from_position(position_name) if not questions: print(f" ✗ 未找到 {position_name} 的面试题") return False print(f" 找到 {len(questions)} 个面试题") # 读取文件 with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() # 构建新的subQuestions sub_questions = [] for i, qa in enumerate(questions, 1): sub_questions.append({ "id": f"{q_prefix}_{i}", "question": qa['question'].replace('"', '\\"').replace('\n', ' '), "answer": qa['answer'].replace('"', '\\"').replace('\n', ' ') }) # 构建新的questions对象 new_questions = { "id": group_id, "question": f"# {industry_name}面试题", "subQuestions": sub_questions } # 生成JSON字符串 questions_json = json.dumps([new_questions], ensure_ascii=False, indent=6) # 查找并替换 # 先找到产业名称的位置 industry_pos = content.find(f'"name": "{industry_name}"') if industry_pos == -1: print(f" ✗ 未找到产业 {industry_name}") return False # 找到这个产业的questions字段 questions_start = content.find('"questions":', industry_pos) if questions_start == -1 or questions_start > industry_pos + 2000: # 限制搜索范围 print(f" ✗ 未找到 {industry_name} 的questions字段") return False # 找到questions数组的开始和结束 array_start = content.find('[', questions_start) if array_start == -1: print(f" ✗ 未找到questions数组开始") return False # 找到对应的结束括号 bracket_count = 1 pos = array_start + 1 while bracket_count > 0 and pos < len(content): if content[pos] == '[': bracket_count += 1 elif content[pos] == ']': bracket_count -= 1 pos += 1 if bracket_count != 0: print(f" ✗ 未能正确解析questions数组") return False # 替换内容 # 调整缩进 lines = questions_json.split('\n') formatted_lines = [] for line in lines: if line.strip(): formatted_lines.append(' ' + line) else: formatted_lines.append('') questions_json = '\n'.join(formatted_lines).strip() new_content = content[:array_start] + questions_json + content[pos-1:] # 写入文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) # 验证语法 result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f" ❌ 语法错误,回滚") with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) return False print(f" ✓ 成功更新 {industry_name}") return True # 创建备份 backup_file = f"{mock_file}.backup_each_{datetime.now().strftime('%Y%m%d_%H%M%S')}" with open(mock_file, 'r', encoding='utf-8') as f: original_content = f.read() with open(backup_file, 'w', encoding='utf-8') as f: f.write(original_content) print(f"已创建备份:{backup_file}") # 要更新的产业列表 industries_to_update = [ ("包装设计", "包装设计师", "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") ] # 逐个更新 success_count = 0 failed_list = [] for industry_name, position_name, q_prefix, group_id in industries_to_update: if update_industry_questions(industry_name, position_name, q_prefix, group_id): success_count += 1 else: failed_list.append(industry_name) # 输出结果 print("\n" + "="*50) print(f"更新完成!成功:{success_count} 个,失败:{len(failed_list)} 个") if failed_list: print(f"失败的产业:{', '.join(failed_list)}") # 最终验证 print("\n最终语法验证...") result = os.popen(f'node -c {mock_file} 2>&1').read() if result: print(f"❌ 文件有语法错误") print(result[:500]) else: print("✓ 所有语法验证通过") print("所有视觉设计产业的面试题更新完成!")