Skip to content

Commit

Permalink
网络学堂2018版
Browse files Browse the repository at this point in the history
  • Loading branch information
lizy14 committed Feb 17, 2019
1 parent 0e4ace7 commit a740761
Show file tree
Hide file tree
Showing 13 changed files with 294 additions and 523 deletions.
3 changes: 0 additions & 3 deletions Background Tasks/BackgroundTasks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ public async void Run(IBackgroundTaskInstance taskInstance) {
try {
await Appointment.updateDeadlines();
} catch (Exception) { }
try {
if (goRemote) { await Appointment.updateLectures(); }
} catch (Exception) { }


deferral.Complete();
Expand Down
44 changes: 4 additions & 40 deletions Class Library/Appointment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ public static async Task updateDeadlines() {

foreach (var i in to_be_inserted) {
Debug.WriteLine("[updateDeadlines] inserting " + i.Subject);

if (i.StartTime - DateTime.Now < TimeSpan.FromHours(7)) {
// WTF ??
Debug.WriteLine("[updateDeadlines] ignoring " + i.Subject);
continue;
// continue;
}
await ddl_cal.SaveAppointmentAsync(i);
}
Expand Down Expand Up @@ -140,25 +142,6 @@ public static async Task updateCalendar() {
Debug.WriteLine("[Appointment] calendar finish");
}

public static async Task updateLectures() { //always force remote
Debug.WriteLine("[Appointment] lecture begin");

//TODO: possible duplication, lock?

var lectures = await Remote.getHostedLectures();

//get Calendar object
AppointmentCalendar cal = await getAppointmentCalendar(lec_cal_name, lec_storedKey);

await deleteAllAppointments(cal);

foreach (var lec in lectures) {
await cal.SaveAppointmentAsync(getAppointment(lec));
}

Debug.WriteLine("[Appointment] lecture finish");
}

public static async Task updateTimetable(bool forceRemote = false) {
Debug.WriteLine("[Appointment] update start");

Expand Down Expand Up @@ -211,27 +194,8 @@ private static Windows.ApplicationModel.Appointments.Appointment getAppointment(

a.StartTime = DateTime.Parse(e.nq + " " + e.kssj);
a.Duration = DateTime.Parse(e.nq + " " + e.jssj) - a.StartTime;
// 修正考试时间 12 小时制
if (e.fl == "考试") {
if (a.StartTime.Hour < 8) {
a.StartTime += TimeSpan.FromHours(12);
}
a.Subject += "考试";
}
a.AllDay = false;
return a;
}

private static Windows.ApplicationModel.Appointments.Appointment getAppointment(Lecture l) {
var a = new Windows.ApplicationModel.Appointments.Appointment();
a.Subject = l.summary;
a.Location = l.location;
a.StartTime = DateTime.Parse(l.dtstart);
a.Duration = DateTime.Parse(l.dtend) - a.StartTime;
a.DetailsKind = AppointmentDetailsKind.PlainText;
a.Details = l.description;
a.AllDay = false;
a.BusyStatus = AppointmentBusyStatus.Free;
return a;
}

Expand All @@ -240,7 +204,7 @@ private static Windows.ApplicationModel.Appointments.Appointment getAppointment(
Regex re = new Regex("&[^;]+;");
a.Subject = re.Replace(e.name, " ");
a.Location = re.Replace(e.course, " ");
a.StartTime = DateTime.Parse(e.ddl + " 23:59");
a.StartTime = DateTime.Parse(e.ddl);
a.AllDay = false;
a.BusyStatus = e.hasBeenFinished ? AppointmentBusyStatus.Free : AppointmentBusyStatus.Tentative;
if (e.hasBeenFinished)
Expand Down
3 changes: 0 additions & 3 deletions Class Library/Class Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,6 @@
<EmbeddedResource Include="Properties\Class_Library.rd.xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="HtmlAgilityPack">
<Version>1.4.9.5</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>5.1.0</Version>
</PackageReference>
Expand Down
183 changes: 124 additions & 59 deletions Class Library/DataAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ orderby assignment.daysFromNow() descending
return future.Concat(past.Take(limit - futureCount)).ToList();
}

public static async Task<List<Course>> getCourses(bool forceRemote = false) {
public static async Task<List<Course>> getCourses(bool forceRemote = false, string semester = "") {
if (isDemo()) {
var list = new List<Course>();
list.Add(new Course {
Expand All @@ -116,6 +116,9 @@ public static async Task<List<Course>> getCourses(bool forceRemote = false) {

return list;
}
if(semester == "") {
semester = (await getSemester()).id;
}

if (!forceRemote) {
//try memory
Expand All @@ -135,7 +138,7 @@ public static async Task<List<Course>> getCourses(bool forceRemote = false) {


//fetch from remote
var _courses = await Remote.getRemoteCourseList();
var _courses = await Remote.getRemoteCourseList(semester);
courses = _courses;
writeCache(COURSES_FILENAME, JSON.stringify(_courses));

Expand All @@ -144,61 +147,122 @@ public static async Task<List<Course>> getCourses(bool forceRemote = false) {
}

public static async Task<Timetable> getTimetable(bool forceRemote = false) {
if (isDemo()) {
var table = new Timetable();

var start = DateTime.Now.AddDays(-20);
while (start.DayOfWeek != DayOfWeek.Monday)
start = start.AddDays(-1);

for (var i = 0; i < 10; i++) {
table.Add(new Event {
nr = "形式语言与自动机",
dd = "六教 6A301",
nq = start.AddDays(i * 7 + 2).ToString("yyyy-MM-dd"),
kssj = "08:00",
jssj = "09:35"
});
{
if (isDemo()) {
var table = new Timetable();

var start = DateTime.Now.AddDays(-20);
while (start.DayOfWeek != DayOfWeek.Monday)
start = start.AddDays(-1);

for (var i = 0; i < 10; i++) {
table.Add(new Event {
nr = "形式语言与自动机",
dd = "六教 6A301",
nq = start.AddDays(i * 7 + 2).ToString("yyyy-MM-dd"),
kssj = "08:00",
jssj = "09:35"
});

table.Add(new Event {
nr = "高级数据结构",
dd = "六教 6A301",
nq = start.AddDays(i * 7 + 2).ToString("yyyy-MM-dd"),
kssj = "09:50",
jssj = "11:25"
});

table.Add(new Event {
nr = "操作系统",
dd = "六教 6A303",
nq = start.AddDays(i * 7 + 3).ToString("yyyy-MM-dd"),
kssj = "09:50",
jssj = "11:25"
});

table.Add(new Event {
nr = "概率论与数理统计",
dd = "六教 6C102",
nq = start.AddDays(i * 7 + 4).ToString("yyyy-MM-dd"),
kssj = "15:20",
jssj = "16:55"
});

table.Add(new Event {
nr = "概率论与数理统计",
dd = "一教 104",
nq = start.AddDays(i * 7 + 1).ToString("yyyy-MM-dd"),
kssj = "13:30",
jssj = "15:05"
});
}
return table;
}
}
{
//fetch from remote
string[] 大节开始时间 = {
"",
"8:00",
"9:50",
"13:30",
"15:20",
"17:05",
"19:20"
};
int[] 大节开始小节 = { 0, 1, 3, 6, 8, 10, 12 };
string[] 小节结束时间 = {
"",
"8:45",
"9:35",
"10:35",
"11:25",
"12:15",
"14:15",
"15:05",
"16:05",
"16:55",
"17:50",
"18:40",
"20:05",
"20:55",
"21:45"
};

table.Add(new Event {
nr = "高级数据结构",
dd = "六教 6A301",
nq = start.AddDays(i * 7 + 2).ToString("yyyy-MM-dd"),
kssj = "09:50",
jssj = "11:25"
});
var table = new Timetable();

table.Add(new Event {
nr = "操作系统",
dd = "六教 6A303",
nq = start.AddDays(i * 7 + 3).ToString("yyyy-MM-dd"),
kssj = "09:50",
jssj = "11:25"
});
bool[] bools = { false, true };
foreach (bool getNextSemester in bools) {

var semester = await getSemester(forceRemote, getNextSemester);
var start = DateTime.Parse(semester.startDate);

foreach (var course in await getCourses(forceRemote, semester.id)) {
Debug.WriteLine("[getAllDeadlines] Remote " + course.name);
var id = course.id;
try {
var detail = await Remote.getRemoteCourseDetail(course.id);
foreach (var segment in detail) {
for (int weekOffset = 0; weekOffset < segment.skzc.Length; ++weekOffset) {
if (segment.skzc[weekOffset] == '1') {
table.Add(new Event {
nr = course.name,
dd = segment.skdd,
nq = start.AddDays(weekOffset * 7 + segment.skxq - 1).ToString("yyyy-MM-dd"),
kssj = 大节开始时间[segment.skjc],
jssj = 小节结束时间[大节开始小节[segment.skjc] + segment.skxs - 1]
});
}
}
}
} catch { }
}
}

table.Add(new Event {
nr = "概率论与数理统计",
dd = "六教 6C102",
nq = start.AddDays(i * 7 + 4).ToString("yyyy-MM-dd"),
kssj = "15:20",
jssj = "16:55"
});

table.Add(new Event {
nr = "概率论与数理统计",
dd = "一教 104",
nq = start.AddDays(i * 7 + 1).ToString("yyyy-MM-dd"),
kssj = "13:30",
jssj = "15:05"
});
}
Debug.WriteLine("[getTimetable] Returning remote");
return table;
}

//fetch from remote
var _remoteTimetable = await Remote.getRemoteTimetable();
Debug.WriteLine("[getTimetable] Returning remote");
return _remoteTimetable;
}

public static async Task<Semester> getSemester(bool forceRemote = false, bool getNextSemester = false) {
Expand Down Expand Up @@ -255,6 +319,9 @@ public static async Task<Semester> getSemester(bool forceRemote = false, bool ge
return __semesters.nextSemester;
}
Debug.WriteLine("[getCalendar] Returning cache");
if (getNextSemester) {
return semesters.nextSemester;
}
return __semesters.currentSemester;
}
}
Expand All @@ -276,6 +343,9 @@ public static async Task<Semester> getSemester(bool forceRemote = false, bool ge
writeCache(SEMESTERS_FILENAME, JSON.stringify(semesters));

Debug.WriteLine("[getCalendar] Returning remote");
if (getNextSemester) {
return semesters.nextSemester;
}
return semesters.currentSemester;
}

Expand Down Expand Up @@ -331,16 +401,11 @@ static public async Task<List<Deadline>> getAllDeadlines(bool forceRemote = fals
foreach (var course in await getCourses(forceRemote)) {
Debug.WriteLine("[getAllDeadlines] Remote " + course.name);
var id = course.id;
try
{
try {
List<Deadline> __deadlines;
if (course.isNew)
__deadlines = await Remote.getRemoteHomeworkListNew(id);
else
__deadlines = await Remote.getRemoteHomeworkList(id);
__deadlines = await Remote.getRemoteHomeworkList(id);
_deadlines = _deadlines.Concat(__deadlines).ToList();
}
catch { }
} catch { }
}


Expand Down
Loading

0 comments on commit a740761

Please sign in to comment.