public static async Task <Semester> getSemester(bool forceRemote = false, bool getNextSemester = false) { if (isDemo()) { var start = DateTime.Now.AddDays(-20); while (start.DayOfWeek != DayOfWeek.Monday) { start = start.AddDays(-1); } if (!getNextSemester) { return new Semester { startDate = start.ToString("yyyy-MM-dd"), endDate = start.AddDays(10 * 7 - 1).ToString("yyyy-MM-dd"), semesterEname = "2333-2334-Spring", } } ; else { return new Semester { startDate = start.AddDays(10 * 7).ToString("yyyy-MM-dd"), endDate = start.AddDays(20 * 7 - 1).ToString("yyyy-MM-dd"), semesterEname = "2334-2335-Autumn", } }; } if (!forceRemote) { Semesters __semesters = null; //try memory if (semesters != null) { Debug.WriteLine("[getCalendar] memory"); __semesters = semesters; } else //try localSettings { var localJSON = await readCache(SEMESTERS_FILENAME); if (localJSON.Length > 0) { Debug.WriteLine("[getCalendar] local settings"); __semesters = JSON.parse <Semesters>(localJSON); } } //cache hit if (__semesters != null) { if (getNextSemester) { return(__semesters.nextSemester); } if (DateTime.Parse(__semesters.currentSemester.endDate + " 23:59") < DateTime.Now) { //perform a remote update Task task = getSemester(forceRemote: true); task.ContinueWith((_) => 0); Debug.WriteLine("[getCalendar] Returning cache next"); if (__semesters.nextSemester.endDate == null) { //automatically complete missing endDate if (__semesters.nextSemester.semesterEname.IndexOf("Autumn") != -1 || __semesters.nextSemester.semesterEname.IndexOf("Spring") != -1) { __semesters.nextSemester.endDate = DateTime.Parse(__semesters.nextSemester.startDate).AddDays(18 * 7 - 1).ToString(); } } return(__semesters.nextSemester); } Debug.WriteLine("[getCalendar] Returning cache"); return(__semesters.currentSemester); } } //fetch from remote Semesters _remoteSemesters = null; if (credentialAbsent() == false) { try { _remoteSemesters = await Remote.getRemoteSemesters(); } catch (Exception) { } } if (_remoteSemesters == null || _remoteSemesters.currentSemester == null) { Debug.WriteLine("[getCalendar] remote fail, falling back to hosted"); _remoteSemesters = await Remote.getHostedSemesters(); } semesters = _remoteSemesters; writeCache(SEMESTERS_FILENAME, JSON.stringify(semesters)); Debug.WriteLine("[getCalendar] Returning remote"); return(semesters.currentSemester); }
static public async Task <List <Deadline> > getAllDeadlines(bool forceRemote = false) { if (isDemo()) { var list = new List <Deadline>(); var start = DateTime.Now.AddDays(-20); while (start.DayOfWeek != DayOfWeek.Monday) { start = start.AddDays(-1); } for (var i = 0; i <= 3; i++) { list.Add(new Deadline { course = "操作系统", ddl = start.AddDays(i * 7 + 4 + 7).ToString("yyyy-MM-dd"), name = $"代码阅读报告{i + 1}", hasBeenFinished = (i < 3), id = "operating_systems_" + i.ToString(), }); } for (var i = 0; i <= 3; i++) { list.Add(new Deadline { course = "数据结构", ddl = start.AddDays(i * 7 + 3 + 7).ToString("yyyy-MM-dd"), name = $"数据结构习题{i + 1}", hasBeenFinished = (i < 3), id = "data_structure_" + i.ToString(), }); } return(list); } if (!forceRemote) { //try session memory if (deadlines != null) { Debug.WriteLine("[getAllDeadlines] Returning memory"); return(deadlines); } //try local var cache = await readCache(DEADLINES_FILENAME); if (cache.Length > 0) { Debug.WriteLine("[getAllDeadlines] Returning local"); return(JSON.parse <List <Deadline> >(cache)); } } //fetch from remote List <Deadline> _deadlines = new List <Deadline>(); foreach (var course in await getCourses(forceRemote)) { Debug.WriteLine("[getAllDeadlines] Remote " + course.name); var id = course.id; List <Deadline> __deadlines; if (course.isNew) { __deadlines = await Remote.getRemoteHomeworkListNew(id); } else { __deadlines = await Remote.getRemoteHomeworkList(id); } _deadlines = _deadlines.Concat(__deadlines).ToList(); } deadlines = _deadlines; writeCache(DEADLINES_FILENAME, JSON.stringify(deadlines)); Debug.WriteLine("[getAllDeadlines] Returning remote"); return(_deadlines); }
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" }); 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 var _remoteTimetable = await Remote.getRemoteTimetable(); Debug.WriteLine("[getTimetable] Returning remote"); return(_remoteTimetable); }