130 lines
5.0 KiB
Python
130 lines
5.0 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
|
||
|
|
import json
|
||
|
|
import shutil
|
||
|
|
from datetime import datetime
|
||
|
|
import re
|
||
|
|
|
||
|
|
def main():
|
||
|
|
print("🚀 开始修复导师信息数据...")
|
||
|
|
|
||
|
|
# 创建备份
|
||
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||
|
|
original_file = "src/data/mockData.js"
|
||
|
|
backup_path = f"{original_file}.backup_fix_{timestamp}"
|
||
|
|
shutil.copy2(original_file, backup_path)
|
||
|
|
print(f"📦 已创建备份: {backup_path}")
|
||
|
|
|
||
|
|
# 读取导师信息数据
|
||
|
|
print("📖 读取导师信息数据...")
|
||
|
|
with open("网页未导入数据/导师信息(通用).json", "r", encoding="utf-8") as f:
|
||
|
|
all_teachers = json.load(f)
|
||
|
|
|
||
|
|
# 筛选视觉设计产业的导师和公共课导师
|
||
|
|
visual_design_teachers = [t for t in all_teachers if t.get('就业管家') == '视觉设计']
|
||
|
|
public_teachers = [t for t in all_teachers if t.get('导师类型') == '公共课导师']
|
||
|
|
ai_teachers = [t for t in all_teachers if t.get('导师类型') == 'AI课老师']
|
||
|
|
marketing_teachers = [t for t in all_teachers if t.get('导师类型') == '营销课老师']
|
||
|
|
hr_teachers = [t for t in all_teachers if t.get('导师类型') == '企业资深HR']
|
||
|
|
|
||
|
|
print(f"✅ 找到 {len(visual_design_teachers)} 位视觉设计产业导师")
|
||
|
|
print(f"✅ 找到 {len(public_teachers)} 位公共课导师")
|
||
|
|
print(f"✅ 找到 {len(ai_teachers)} 位AI课老师")
|
||
|
|
print(f"✅ 找到 {len(marketing_teachers)} 位营销课老师")
|
||
|
|
print(f"✅ 找到 {len(hr_teachers)} 位企业资深HR")
|
||
|
|
|
||
|
|
# 合并所有需要的导师
|
||
|
|
selected_teachers = visual_design_teachers + public_teachers + ai_teachers + marketing_teachers + hr_teachers
|
||
|
|
|
||
|
|
# 构建新的teacherData对象内容
|
||
|
|
teacher_data_lines = []
|
||
|
|
for teacher in selected_teachers:
|
||
|
|
name = teacher.get('查询导师名称', '')
|
||
|
|
if not name:
|
||
|
|
continue
|
||
|
|
|
||
|
|
# 处理介绍内容:移除换行符,转义引号
|
||
|
|
introduction = teacher.get('导师介绍', '')
|
||
|
|
# 替换所有换行符为空格
|
||
|
|
introduction = introduction.replace('\n', ' ').replace('\r', ' ')
|
||
|
|
# 移除多余的空格
|
||
|
|
introduction = ' '.join(introduction.split())
|
||
|
|
# 转义引号
|
||
|
|
introduction = introduction.replace('"', '\\"')
|
||
|
|
|
||
|
|
specialties = teacher.get('导师特长', [])
|
||
|
|
avatar_url = teacher.get('❌导师头像url链接', '')
|
||
|
|
teacher_type = teacher.get('导师类型', '')
|
||
|
|
vertical_direction = teacher.get('所属垂直方向', '')
|
||
|
|
|
||
|
|
# 映射导师类型
|
||
|
|
if teacher_type == '任课老师':
|
||
|
|
if vertical_direction:
|
||
|
|
type_str = '垂直课导师'
|
||
|
|
else:
|
||
|
|
type_str = '复合课导师'
|
||
|
|
elif teacher_type == '公共课导师':
|
||
|
|
type_str = '公共课导师'
|
||
|
|
elif teacher_type == 'AI课老师':
|
||
|
|
type_str = 'AI课导师'
|
||
|
|
elif teacher_type == '营销课老师':
|
||
|
|
type_str = '营销课导师'
|
||
|
|
elif teacher_type == '企业资深HR':
|
||
|
|
type_str = 'HR导师'
|
||
|
|
else:
|
||
|
|
type_str = '导师'
|
||
|
|
|
||
|
|
# 格式化特长数组,移除 # 符号
|
||
|
|
specialties_formatted = ', '.join([f'"{s.strip().replace("#", "").strip()}"' for s in specialties])
|
||
|
|
|
||
|
|
teacher_entry = f''' "{name}": {{
|
||
|
|
name: "{name}",
|
||
|
|
introduction: "{introduction}",
|
||
|
|
specialties: [{specialties_formatted}],
|
||
|
|
avatar: "{avatar_url}",
|
||
|
|
type: "{type_str}",
|
||
|
|
verticalDirection: "{vertical_direction}",
|
||
|
|
courses: []
|
||
|
|
}}'''
|
||
|
|
|
||
|
|
teacher_data_lines.append(teacher_entry)
|
||
|
|
print(f"✅ 处理导师: {name}")
|
||
|
|
|
||
|
|
# 构建完整的teacherData内容
|
||
|
|
teacher_data_content = ',\n'.join(teacher_data_lines)
|
||
|
|
|
||
|
|
# 读取原始文件
|
||
|
|
with open(original_file, "r", encoding="utf-8") as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 使用正则表达式查找并替换teacherData
|
||
|
|
pattern = r'(// 导师信息数据\s*\n\s*teacherData: updateTeacherCourses\({)([\s\S]*?)(\}, allCalendarEvents\))'
|
||
|
|
|
||
|
|
replacement = f'''// 导师信息数据
|
||
|
|
teacherData: updateTeacherCourses({{
|
||
|
|
{teacher_data_content}
|
||
|
|
}}, allCalendarEvents)'''
|
||
|
|
|
||
|
|
if re.search(pattern, content):
|
||
|
|
new_content = re.sub(pattern, replacement, content)
|
||
|
|
|
||
|
|
# 写入文件
|
||
|
|
with open(original_file, "w", encoding="utf-8") as f:
|
||
|
|
f.write(new_content)
|
||
|
|
|
||
|
|
print("📊 替换统计:")
|
||
|
|
print(f" - 视觉设计导师数: {len(visual_design_teachers)}")
|
||
|
|
print(f" - 公共课导师数: {len(public_teachers)}")
|
||
|
|
print(f" - AI课导师数: {len(ai_teachers)}")
|
||
|
|
print(f" - 营销课导师数: {len(marketing_teachers)}")
|
||
|
|
print(f" - HR导师数: {len(hr_teachers)}")
|
||
|
|
print(f" - 总导师数: {len(selected_teachers)}")
|
||
|
|
|
||
|
|
print("✅ 已修复导师信息数据格式")
|
||
|
|
print("🎉 处理完成!")
|
||
|
|
else:
|
||
|
|
print("❌ 未找到目标代码段,请检查文件结构")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|