-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3155b2b
commit 14a8fa5
Showing
5 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.0.32014.148 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "CurriculumGenerator", "CurriculumGenerator\CurriculumGenerator.pyproj", "{B07BBA6E-83E6-473A-8E08-F4BBF365097E}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B07BBA6E-83E6-473A-8E08-F4BBF365097E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B07BBA6E-83E6-473A-8E08-F4BBF365097E}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {8034F14A-3A84-4FD1-A89D-0E9C4E57483A} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
import datetime, uuid | ||
from enum import Enum | ||
class CourseRepetitionType(Enum): | ||
weekly = 0 | ||
biweekly = 1 | ||
|
||
class Course: | ||
def __init__(self, kwargs): | ||
self.event_data = kwargs | ||
|
||
def __turn_to_string__(self): | ||
self.event_text = "BEGIN:VEVENT\n" | ||
for item,data in self.event_data.items(): | ||
item = str(item).replace("_","-") | ||
if item not in ["ORGANIZER","DTSTART","DTEND"]: | ||
self.event_text += "%s:%s\n"%(item,data) | ||
else: | ||
self.event_text += "%s;%s\n"%(item,data) | ||
self.event_text += "END:VEVENT\n" | ||
return self.event_text | ||
|
||
class Curriculum: | ||
def __init__(self): | ||
self.__courses__ = {} | ||
self.__course_id__ = 0 | ||
self.calendar_name = "课程表" | ||
|
||
def add_course(self, **kwargs): | ||
course = Course(kwargs) | ||
course_id = self.__course_id__ | ||
self.__courses__[self.__course_id__] = course | ||
self.__course_id__ += 1 | ||
return course_id | ||
|
||
def get_ics_text(self): | ||
self.__calendar_text__ = """BEGIN:VCALENDAR\nVERSION:2.0\nX-WR-CALNAME:课程表\n""" | ||
for key,value in self.__courses__.items(): | ||
self.__calendar_text__ += value.__turn_to_string__() | ||
self.__calendar_text__ += "END:VCALENDAR" | ||
return self.__calendar_text__ | ||
|
||
def save_as_ics_file(self): | ||
ics_text = self.get_ics_text() | ||
open("%s.ics"%self.calendar_name,"w",encoding="utf8").write(ics_text) | ||
|
||
def add_course(curriculum, name, start_time, end_time, location, week, term_end): | ||
""" | ||
向Curriculum对象添加事件的方法 | ||
:param curriculum: curriculum实例 | ||
:param name: 课程名 | ||
:param start_time: 课程开始时间 | ||
:param end_time: 课程结束时间 | ||
:param location: 时间地点 | ||
:param week: 上课周次(每周=0,隔周=1) | ||
:param term_end: 学期结束日期 | ||
:return: | ||
""" | ||
time_format = "TZID=Asia/Shanghai:{date.year}{date.month:0>2d}{date.day:0>2d}T{date.hour:0>2d}{date.minute:0>2d}{date.second:0>2d}" | ||
dt_start = time_format.format(date=start_time) | ||
dt_end = time_format.format(date=end_time) | ||
create_time = datetime.datetime.today().strftime("%Y%m%dT%H%M%SZ") | ||
if week == CourseRepetitionType.weekly: | ||
rrule = "FREQ=WEEKLY;UNTIL={date.year}{date.month:0>2d}{date.day:0>2d}T{date.hour:0>2d}{date.minute:0>2d}{date.second:0>2d}".format(date=term_end) | ||
else: | ||
rrule = "FREQ=WEEKLY;UNTIL={date.year}{date.month:0>2d}{date.day:0>2d}T{date.hour:0>2d}{date.minute:0>2d}{date.second:0>2d};INTERVAL=2".format(date=term_end) | ||
curriculum.add_course( | ||
SUMMARY=name, | ||
CREATED=create_time, | ||
DTSTART=dt_start, | ||
DTSTAMP=create_time, | ||
DTEND=dt_end, | ||
UID=str(uuid.uuid4()), | ||
SEQUENCE="0", | ||
LAST_MODIFIED=create_time, | ||
LOCATION=location, | ||
RRULE=rrule | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
import openpyxl, sys, Curriculum, re, datetime | ||
|
||
def __course_start_time(num): | ||
time = { | ||
"1" : datetime.time(8), | ||
"3" : datetime.time(10, 20), | ||
"5" : datetime.time(14), | ||
"7" : datetime.time(16, 20), | ||
"9" : datetime.time(19) | ||
} | ||
return time.get(num) | ||
def __course_end_time(num): | ||
time = { | ||
"2" : datetime.time(9, 50), | ||
"4" : datetime.time(12, 10), | ||
"6" : datetime.time(15, 50), | ||
"8" : datetime.time(18, 10), | ||
"10" : datetime.time(20, 50), | ||
"11" : datetime.time(21, 50) | ||
} | ||
return time.get(num) | ||
|
||
workbook = openpyxl.load_workbook(sys.argv[1]) | ||
sheet = workbook.active | ||
term_start = sys.argv[2] | ||
term_start_date = datetime.date(int(term_start[0:4]), int(term_start[4:6]), int(term_start[6:])) | ||
term_end = sys.argv[3] | ||
term_end_date = datetime.date(int(term_end[0:3]), int(term_end[4:6]), int(term_start[6:])) | ||
curriculum = Curriculum.Curriculum() | ||
day = 0 | ||
for col in sheet.iter_cols(min_col=2, max_col=6, min_row=4, max_row=9): | ||
for course in col: | ||
if course.value: | ||
val = course.value.split("\n") | ||
for i in range(len(val)//4): | ||
name = val[i * 4] | ||
info = val[i * 4 + 3].split("][") | ||
num = re.findall("\d+", sheet["A%d"%course.row].value) | ||
if info[0][-2] == '单': | ||
type = Curriculum.CourseRepetitionType.biweekly | ||
start_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day), __course_start_time(num[0])) | ||
end_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day), __course_end_time(num[1])) | ||
elif info[0][-2] == '双': | ||
type = Curriculum.CourseRepetitionType.biweekly | ||
start_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day+7), __course_start_time(num[0])) | ||
end_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day+7), __course_end_time(num[1])) | ||
else: | ||
type = Curriculum.CourseRepetitionType.weekly | ||
start_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day), __course_start_time(num[0])) | ||
end_time = datetime.datetime.combine(term_start_date + datetime.timedelta(days=day), __course_end_time(num[1])) | ||
location = "南方科技大学-" + info[1] | ||
term_start_date = datetime.datetime.combine(term_start_date, datetime.time()) | ||
term_end_date = datetime.datetime.combine(term_end_date, datetime.time.max) | ||
Curriculum.add_course(curriculum, name, start_time, end_time, location, type, term_end_date) | ||
day += 1 | ||
|
||
curriculum.save_as_ics_file() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectGuid>b07bba6e-83e6-473a-8e08-f4bbf365097e</ProjectGuid> | ||
<ProjectHome> | ||
</ProjectHome> | ||
<StartupFile>CurriculumGenerator.py</StartupFile> | ||
<SearchPath> | ||
</SearchPath> | ||
<WorkingDirectory>.</WorkingDirectory> | ||
<OutputPath>.</OutputPath> | ||
<Name>CurriculumGenerator</Name> | ||
<RootNamespace>CurriculumGenerator</RootNamespace> | ||
<LaunchProvider>Standard Python launcher</LaunchProvider> | ||
<CommandLineArguments>export.xlsx 20220214 20220605</CommandLineArguments> | ||
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging> | ||
<PublishUrl> | ||
</PublishUrl> | ||
<SuppressEnvironmentCreationPrompt>True</SuppressEnvironmentCreationPrompt> | ||
<IsWindowsApplication>False</IsWindowsApplication> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="Curriculum.py" /> | ||
<Compile Include="CurriculumGenerator.py" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Content Include="requirements.txt" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" /> | ||
<!-- Uncomment the CoreCompile target to enable the Build command in | ||
Visual Studio and specify your pre- and post-build commands in | ||
the BeforeBuild and AfterBuild targets below. --> | ||
<!--<Target Name="CoreCompile" />--> | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
openpyxl==3.0.9 |