Files
astrbot_plugin_njupt_suan/schedule_utils.py

125 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""课表数据处理工具函数"""
def convert_tuple_schedule_to_dict(schedule: list[tuple]) -> list[dict]:
"""将元组格式的课表转换为字典格式。
Args:
schedule: list[tuple],每个元组包含 (name, teacher, classroom, weeks, day, classes)
其中 weeks 可以是 list[int] 或 str"1-17"
Returns:
list[dict]: 标准格式的课程数据
"""
result = []
for course in schedule:
# 确保元组长度足够,不足的补 None
padded = list(course) + [None] * (6 - len(course))
name, teacher, classroom, weeks, day, classes = padded[:6]
# 处理 weeks支持 list 或 str如 "1-17", "1,3,5-10"
weeks_list = []
if isinstance(weeks, list):
weeks_list = weeks
elif isinstance(weeks, str):
weeks_list = parse_weeks_string(weeks)
result.append({
"name": name,
"teacher": teacher,
"classroom": classroom,
"weeks": weeks_list,
"day": day if isinstance(day, int) else 1,
"classes": classes if isinstance(classes, list) else []
})
return result
def parse_weeks_string(weeks_str: str) -> list[int]:
"""将周数字符串解析为整数列表。
支持格式:
"1-17" -> [1,2,3,...,17]
"1,3,5" -> [1,3,5]
"1-5,7,9-11" -> [1,2,3,4,5,7,9,10,11]
Args:
weeks_str: 周数字符串
Returns:
list[int]: 周数列表
"""
if not weeks_str or not isinstance(weeks_str, str):
return []
result = []
parts = weeks_str.split(',')
for part in parts:
part = part.strip()
if '-' in part:
# 范围格式1-17
try:
start, end = part.split('-', 1)
start = int(start.strip())
end = int(end.strip())
result.extend(range(start, end + 1))
except ValueError:
continue
else:
# 单个数字
try:
result.append(int(part))
except ValueError:
continue
# 去重并排序
return sorted(set(result))
def format_weeks(weeks: list[int]) -> str:
"""格式化周数显示,如 [1,2,3,5,6,7] -> "1-3,5-7" """
if not weeks:
return ''
weeks = sorted(set(weeks))
ranges = []
start = end = weeks[0]
for w in weeks[1:]:
if w == end + 1:
end = w
else:
if start == end:
ranges.append(str(start))
else:
ranges.append(f"{start}-{end}")
start = end = w
if start == end:
ranges.append(str(start))
else:
ranges.append(f"{start}-{end}")
return ','.join(ranges)
def get_period_time(period: int) -> str:
"""获取节次对应的时间(南邮标准时间)"""
time_map = {
1: '08:00',
2: '08:50',
3: '09:50',
4: '10:40',
5: '11:30',
6: '13:45',
7: '14:35',
8: '15:35',
9: '16:25',
10: '18:30',
11: '19:20',
12: '20:10'
}
return time_map.get(period, '')