#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 更新简历与面试题页面的面试题数据 将大健康岗位简历.json中的面试题数据替换到resumeInterviewMock.js中 """ import json import re import sys from datetime import datetime def load_health_resume_data(): """加载大健康岗位简历数据""" try: with open('网页未导入数据/大健康产业/大健康岗位简历.json', 'r', encoding='utf-8') as f: return json.load(f) except Exception as e: print(f"Error loading health resume data: {e}") return None def load_current_mock_data(): """加载当前的resumeInterviewMock.js数据""" try: with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: content = f.read() # 提取JavaScript数据部分 start_marker = "const industries = [" end_marker = "];" start_index = content.find(start_marker) if start_index == -1: print("Cannot find industries declaration in resumeInterviewMock.js") return None, None # 找到对应的结束括号 bracket_count = 0 in_brackets = False end_index = start_index + len("const industries = ") for i, char in enumerate(content[end_index:], end_index): if char == '[': bracket_count += 1 in_brackets = True elif char == ']': bracket_count -= 1 if bracket_count == 0 and in_brackets: end_index = i + 1 break if bracket_count != 0: print("Cannot find matching bracket for industries array") return None, None js_data_part = content[start_index + len("const industries = "):end_index] # 查找文件剩余部分的开始 remaining_start = content.find("\n", end_index) if remaining_start == -1: remaining_start = end_index other_content = content[remaining_start:] return js_data_part, other_content except Exception as e: print(f"Error loading current mock data: {e}") return None, None def parse_interview_content(content): """解析面试题内容,转换为结构化数据""" if not content: return [] questions = [] # 按大标题分割(# 一、二、三等) sections = re.split(r'\n# ([一二三四五六七八九十]+、[^#\n]+)', content) if len(sections) < 2: return questions for i in range(1, len(sections), 2): if i + 1 < len(sections): section_title = sections[i].strip() section_content = sections[i + 1].strip() # 解析每个section中的问题 sub_questions = [] # 按问题编号分割 (1. 2. 3. 等) question_parts = re.split(r'\n\s*(\d+\.)\s+', section_content) for j in range(1, len(question_parts), 2): if j + 1 < len(question_parts): question_num = question_parts[j].strip() question_block = question_parts[j + 1].strip() # 提取问题和答案 lines = question_block.split('\n') question_text = "" answer_text = "" in_answer = False for line in lines: line = line.strip() if line.startswith('示例答案:'): in_answer = True continue if not in_answer and line and not line.startswith('示例答案:'): if question_text: question_text += " " question_text += line elif in_answer and line: if answer_text: answer_text += "\\n" answer_text += line if question_text: sub_questions.append({ "id": f"q{len(questions) + 1}_{len(sub_questions) + 1}", "question": question_text, "answer": answer_text }) if sub_questions: questions.append({ "id": f"group_q{len(questions) + 1}", "question": f"# {section_title}", "subQuestions": sub_questions }) return questions def update_interview_questions(health_data): """更新面试题数据""" js_data_part, other_content = load_current_mock_data() if not js_data_part or not other_content: return False try: # 解析现有的JavaScript数据 # 先转换成可解析的JSON格式 json_like = js_data_part.strip().rstrip(';') # 替换JavaScript特有的语法 json_like = re.sub(r'(\w+):', r'"\1":', json_like) # 给属性名加引号 json_like = re.sub(r'"\s*(\w+)"\s*:', r'"\1":', json_like) # 清理多余空格 # 解析JSON industries_data = json.loads(json_like) # 创建岗位到面试题的映射 position_to_questions = {} for item in health_data: position_name = item.get('岗位名称', '') interview_content = item.get('面试题内容', '') if position_name and interview_content: questions = parse_interview_content(interview_content) position_to_questions[position_name] = questions # 更新现有数据的面试题 for industry in industries_data: for position in industry.get('positions', []): position_title = position.get('title', '') if position_title in position_to_questions: position['questions'] = position_to_questions[position_title] print(f"Updated interview questions for position: {position_title}") # 将更新后的数据转换回JavaScript格式 js_content = json.dumps(industries_data, ensure_ascii=False, indent=2) # 恢复JavaScript语法 js_content = re.sub(r'"(\w+)":', r'\1:', js_content) # 写回文件 full_content = f"""// 简历与面试题Mock数据 // 岗位群列表 const industries = {js_content}; {other_content}""" with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(full_content) print("Interview questions updated successfully!") return True except Exception as e: print(f"Error updating interview questions: {e}") return False def main(): """主函数""" print("Starting to update interview questions...") # 加载数据 health_data = load_health_resume_data() if not health_data: print("Failed to load health resume data") return False # 更新面试题数据 success = update_interview_questions(health_data) if success: print("Interview questions update completed!") else: print("Interview questions update failed!") return success if __name__ == "__main__": main()