Skip to content

Commit 7e51489

Browse files
committed
Support new undergraduate schedule endpoint.
1 parent fd8e544 commit 7e51489

File tree

2 files changed

+24
-30
lines changed

2 files changed

+24
-30
lines changed

DL444.CquSchedule.Backend/Models/UpstreamScheduleResponseModel.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ namespace DL444.CquSchedule.Backend.Models
44
{
55
internal struct UpstreamScheduleResponseModel
66
{
7-
[JsonPropertyName("status")]
8-
public string Status { get; set; }
9-
[JsonPropertyName("msg")]
10-
public string Message { get; set; }
11-
[JsonPropertyName("data")]
7+
[JsonPropertyName("classTimetableVOList")]
128
public ScheduleDataEntry[] Data { get; set; }
139
}
1410

@@ -24,15 +20,9 @@ internal struct ScheduleDataEntry
2420
public string DayOfWeek { get; set; }
2521
[JsonPropertyName("period")]
2622
public string Session { get; set; }
27-
[JsonPropertyName("classTimetableInstrVOList")]
28-
public LecturerEntry[] Lecturers { get; set; }
23+
[JsonPropertyName("instructorName")]
24+
public string LecturersNotation { get; set; }
2925
[JsonPropertyName("classType")]
3026
public string ClassType { get; set; }
3127
}
32-
33-
internal struct LecturerEntry
34-
{
35-
[JsonPropertyName("instructorName")]
36-
public string Lecturer { get; set; }
37-
}
3828
}

DL444.CquSchedule.Backend/Services/ScheduleService.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Net;
55
using System.Net.Http;
6+
using System.Net.Http.Json;
67
using System.Net.Sockets;
78
using System.Text.RegularExpressions;
89
using System.Threading.Tasks;
@@ -168,29 +169,18 @@ public async Task<Schedule> GetScheduleAsync(string username, string termId, ISi
168169
throw new ArgumentException("Sign in context is not for undergraduate.");
169170
}
170171

171-
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://my.cqu.edu.cn/api/enrollment/enrollment-batch/user-switch-batch");
172-
request.Content = new FormUrlEncodedContent(new[]
173-
{
174-
new KeyValuePair<string, string>("sessionId", termId)
175-
});
176-
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
177-
HttpResponseMessage response = await httpClient.SendAsync(request);
178-
response.EnsureSuccessStatusCode();
179-
180172
string examStudentId = examStudentIdService.GetExamStudentId(username);
181173
Task<HttpResponseMessage> examTask = httpClient.GetAsync($"https://my.cqu.edu.cn/api/exam/examTask/get-student-exam-list-outside?studentId={examStudentId}");
182174

183-
request = new HttpRequestMessage(HttpMethod.Get, $"https://my.cqu.edu.cn/api/enrollment/timetable/student/{username}");
175+
var request = new HttpRequestMessage(HttpMethod.Post, $"https://my.cqu.edu.cn/api/timetable/class/timetable/student/my-table-detail?sessionId={termId}");
176+
request.Content = JsonContent.Create<string[]>([username]);
184177
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
185-
response = await httpClient.SendAsync(request);
178+
HttpResponseMessage response = await httpClient.SendAsync(request);
186179
var responseModel = await UpstreamScheduleResponseModelSerializerContext.Default.DeserializeFromStringAsync(await response.Content.ReadAsStreamAsync());
187180
Schedule schedule = new Schedule(username);
188-
if (!responseModel.Status.Equals("success", StringComparison.Ordinal) || responseModel.Data == null)
181+
if (responseModel.Data == null)
189182
{
190-
throw new UpstreamRequestException("Upstream server did not return success status for schedule request.")
191-
{
192-
ErrorDescription = responseModel.Message
193-
};
183+
throw new UpstreamRequestException("Upstream server did not return success status for schedule request.");
194184
}
195185
foreach (var responseEntry in responseModel.Data)
196186
{
@@ -201,9 +191,22 @@ public async Task<Schedule> GetScheduleAsync(string username, string termId, ISi
201191
string name = responseEntry.Name;
202192
if (expClassTypes.Contains(responseEntry.ClassType))
203193
{
194+
// This is empirically dead code. The site does not return valid ClassType anymore.
204195
name = name.Contains("实验") ? name : $"{name}实验";
205196
}
206-
string lecturer = responseEntry.Lecturers == null ? string.Empty : responseEntry.Lecturers.FirstOrDefault().Lecturer;
197+
string lecturer = string.Empty;
198+
if (!string.IsNullOrEmpty(responseEntry.LecturersNotation))
199+
{
200+
Match lecturerMatch = lecturerExtractionRegex.Match(responseEntry.LecturersNotation);
201+
if (lecturerMatch.Success)
202+
{
203+
lecturer = lecturerMatch.Groups[1].Value;
204+
}
205+
else
206+
{
207+
lecturer = responseEntry.LecturersNotation;
208+
}
209+
}
207210
ScheduleEntry entry = new ScheduleEntry()
208211
{
209212
Name = name,
@@ -406,6 +409,7 @@ private struct SigninInfo
406409
private readonly IUpstreamCredentialEncryptionService encryptionService;
407410
private readonly IExamStudentIdService examStudentIdService;
408411
private readonly int vacationServeDays;
412+
private static readonly Regex lecturerExtractionRegex = new Regex("(.*?)-\\d");
409413
private static readonly Regex roomSimplifyRegex = new Regex("(室|机房|中心|分析系统|创新设计|展示与分析).*?-(.*?)$");
410414
private static readonly string[] expClassTypes = new[] { "上机", "实验" };
411415
}

0 commit comments

Comments
 (0)