财大课表编排(精简版)

由于识别后还需要校对,如果识别效率不好的话可能得不偿失。所以我故意保留了一部分代码,才知道你用的是我的info2ics.py程序

(完整版见上一篇文章)

这里直接需要用户手动写入课程关键信息到simple.txt中(里面有格式示例),然后用simplify.py程序将simple.txt转化为ics文件(一份是GMT时间,一份是北京时间)

1 # 请勿删除注释 
2 # 格式:
3 # 课程名 开始周 结束周 星期几 开始节 单双周(0:每周,1:单周,2:双周) 教学楼
4 # 示例:
5 # 智慧物流 1 17 4 3 0 N3229 
6 # 外文著作 4 4 3 7 1 N2126
simple.txt
  1 #如果从偶数周开始,而且是双周,代码得重做
  2 
  3 # 北京时间:GMT+8:00
  4 import uuid
  5 from datetime import datetime, timedelta
  6 
  7 def create_ics_file(events,calendar_name):
  8   # 生成ics文件头部信息
  9   ics_file = 'BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//hacksw/handcal//NONSGML v1.0//EN\n'
 10 
 11   # 循环生成每个事件的ics信息
 12   for event in events:
 13     # 生成事件唯一标识符
 14     event_uid = uuid.uuid4().hex
 15 
 16     # 生成事件开始和结束时间
 17     start_time = event['start_time']
 18     end_time = event['end_time']
 19 
 20     # 生成事件的ics信息
 21     ics_event = f'BEGIN:VEVENT\nUID:{event_uid}\nDTSTAMP:{datetime.now().strftime("%Y%m%dT%H%M%SZ")}\nDTSTART:{start_time.strftime("%Y%m%dT%H%M%S")}\nDTEND:{end_time.strftime("%Y%m%dT%H%M%S")}\nSUMMARY:{event["summary"]}\nLOCATION:{event["location"]}\nEND:VEVENT\n'
 22 
 23     # 将事件信息添加到ics文件中
 24     ics_file += ics_event
 25 
 26   # 添加ics文件尾部信息
 27   ics_file += 'END:VCALENDAR\n'
 28 
 29   # 将ics文件保存到本地
 30   with open(calendar_name, 'w', encoding='utf-8') as f:
 31     f.write(ics_file)
 32 
 33 # 将识别文本输入到结构体中
 34 # summary week_start week_end week_time start_course location
 35 path = 'simple.txt'
 36 def sim2eve(path):
 37   with open(path,'r',encoding='utf-8') as f:
 38     contents = f.readlines()
 39     # 从非注释行开始
 40     structs = []
 41     for content in contents[6:]:
 42       sub_dict = {}
 43       con_list = content.split(' ')
 44       sub_dict['summary'] = con_list[0]
 45       sub_dict['week_start'] = int(con_list[1])
 46       sub_dict['week_end'] = int(con_list[2])
 47       sub_dict['week_time'] = con_list[3]
 48       sub_dict['start_course'] = con_list[4]
 49       if con_list[5] == '1' or con_list[5] == '2':
 50         sub_dict['step'] = 2
 51         sub_dict['week_parity'] = int(con_list[5])-1
 52       else:
 53         sub_dict['step'] = 1
 54         sub_dict['week_parity'] = int(con_list[5])
 55       sub_dict['location'] = con_list[6]
 56 
 57       structs.append(sub_dict)
 58   return structs
 59 structs = sim2eve(path)
 60 
 61 def arrange(time_block,structs):
 62   events = []
 63   # 课程遍历
 64   for struct in structs:
 65     # 周数遍历
 66     days = -1
 67     for week in range(struct['week_start']+struct['week_parity'],struct['week_end']+1,struct['step']):
 68       sub_dict2 = {}
 69       # 周的累计
 70       days += 7*(week-1)
 71       # 周具体时间修改
 72       if struct['week_time'] == '1':
 73         days += 1
 74       elif struct['week_time'] == '2':
 75         days += 2
 76       elif struct['week_time'] == '3':
 77         days += 3
 78       elif struct['week_time'] == '4':
 79         days += 4
 80       elif struct['week_time'] == '5':
 81         days += 5
 82       elif struct['week_time'] == '6':
 83         days += 6
 84       elif struct['week_time'] == '7':
 85         days += 7
 86       # 教学楼时间修改
 87       if struct['location'][0] in 'CDEFJM':
 88         # 时段修改
 89         if struct['start_course'] == '1':
 90           hours = 0
 91           minutes = 0
 92         elif struct['start_course'] == '3':
 93           hours = 2
 94           minutes = 10
 95         elif struct['start_course'] == '5':
 96           hours = 5
 97           minutes = 20
 98         elif struct['start_course'] == '7':
 99           hours = 7
100           minutes = 30
101         elif struct['start_course'] == '9':
102           hours = 9
103           minutes = 50
104         elif struct['start_course'] == '11':
105           hours = 11
106           minutes = 40
107         sub_dict2['start_time'] = datetime(2023, 2, 27, time_block, 10, 0)+timedelta(days=days,minutes=minutes,hours=hours)
108         sub_dict2['end_time'] = sub_dict2['start_time']+timedelta(minutes=30,hours=1)
109         sub_dict2['summary'] = struct['summary']
110         sub_dict2['location'] = struct['location']
111       else:
112         if struct['start_course'] == '1':
113           hours = 0
114           minutes = 0
115         elif struct['start_course'] == '3':
116           hours = 2
117           minutes = 0
118         elif struct['start_course'] == '5':
119           hours = 5
120           minutes = 20
121         elif struct['start_course'] == '7':
122           hours = 7
123           minutes = 20
124         elif struct['start_course'] == '9':
125           hours = 10
126           minutes = 0
127         elif struct['start_course'] == '11':
128           hours = 11
129           minutes = 50
130         sub_dict2['start_time'] = datetime(2023, 2, 27, time_block, 0, 0)+timedelta(days=days,minutes=minutes,hours=hours)
131         sub_dict2['end_time'] = sub_dict2['start_time']+timedelta(minutes=30,hours=1)
132         sub_dict2['summary'] = struct['summary']
133         sub_dict2['location'] = struct['location']
134       events.append(sub_dict2)
135       # 结束一周,days归位
136       days = -1
137   print(f'课程总数:{len(events)}')
138   return events
139 GMT_events = arrange(8,structs)
140 Beijing_events = arrange(0,structs)
141 create_ics_file(GMT_events,"GMT.ics")
142 create_ics_file(Beijing_events,"Beijing.ics")
simplify.py

 这是我的simple.txt

# 请勿删除注释
# 格式:
# 课程名 开始周 结束周 星期几 开始节 单双周(0:每周,1:单周,2:双周) 教学楼
# 示例:
# 智慧物流 1 17 4 3 0 N3229
# 外文著作 4 4 3 7 1 N2126
智慧物流 1 17 4 3 0 N3229
市场调研 1 17 2 5 0 N3218
物流外文著作阅读 1 8 1 7 0 N2229
物流外文著作阅读 4 4 3 7 0 N2126
物流前沿讲座 9 17 1 7 0 N2230
形势与政策IV 1 4 2 11 0 L224
习思想 1 17 3 5 2 M206
习思想 1 17 4 7 0 M206
经济法 1 17 3 3 0 N2246

posted @ 2023-02-24 17:18  不撞楠乔  阅读(31)  评论(0)    收藏  举报