Skip to content

Instantly share code, notes, and snippets.

@LenniCodes
Last active September 20, 2021 18:26
Show Gist options
  • Save LenniCodes/5b9832707860a536c1c9f51516e3f8e8 to your computer and use it in GitHub Desktop.
Save LenniCodes/5b9832707860a536c1c9f51516e3f8e8 to your computer and use it in GitHub Desktop.
WebUntis JavaAPI Wrap
public UntisAuthentication authenticate(String school, String user, String password) {
try {
JSONObject request = new JSONObject();
request.put("method", "authenticate");
JSONObject params = new JSONObject();
params.put("user", user);
params.put("password", password);
params.put("client", "SchoolGuard/Beta");
request.put("params", params);
JSONObject data = postUntisRequest(request, new UntisAuthentication(school, null, null));
JSONObject result = data.getJSONObject("result");
return new UntisAuthentication(school, "" + result.getInt("personId"), result.getString("sessionId"));
} catch (Exception ex) {
return null;
}
}
public JSONObject postUntisRequest(JSONObject requestBody, UntisAuthentication auth) {
try {
requestBody.put("id", "req_3");
requestBody.put("jsonrpc", "2.0");
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, requestBody.toString());
OkHttpClient client = new OkHttpClient();
okhttp3.Request.Builder request = new Request.Builder()
// .header("Content-Type", "application/json")
.header("User-Agent", "SchoolGuard/Beta")
.post(body)
.url("https://kephiso.webuntis.com/WebUntis/jsonrpc.do?school="
+ URLEncoder.encode(auth.school, StandardCharsets.UTF_8));
if (auth.key != null) {
request.header("JSESSIONID", auth.key.trim());
request.header("Cookie", "JSESSIONID=" + auth.key.trim());
}
Response response = client.newCall(request.build()).execute();
String strBody = response.body().string();
JSONObject resp = new JSONObject(strBody);
return resp;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public class UntisAuthentication {
String school;
String classRoom;
String key;
public UntisAuthentication(String school, String classRoom, String key) {
this.school = school;
this.classRoom = classRoom;
this.key = key;
}
public String getClassRoom() {
return classRoom;
}
public String getKey() {
return key;
}
public String getSchool() {
return school;
}
}
@SuppressWarnings("deprecation")
public List<SchoolLesson> loadTimeTable(UntisAuthentication auth) {
List<SchoolLesson> resultLessons = new ArrayList<SchoolLesson>();
Date today = new Date();
LocalDate localDate = today.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
String todayString = formatNumber(localDate.getYear()) + formatNumber(localDate.getMonthValue())
+ formatNumber(localDate.getDayOfMonth());
// String todayString = "20210222";
JSONObject request = new JSONObject(
"{\"id\": \"req_3\", \"method\": \"getTimetable\", \"params\": {\"options\":\r\n" + " {\r\n"
+ " \"startDate\":" + todayString + ",\r\n" + " \"endDate\":" + todayString + ",\r\n"
+ " \"element\": {\"id\": 218, \"type\": 1, \"keyType\": \"id\"},\r\n"
+ " \"showStudentgroup\": true,\r\n" + " \"showLsText\": true,\r\n"
+ " \"showLsNumber\": true,\r\n" + " \"showInfo\": true,\r\n"
+ " \"subjectFields\": [\"name\",\"longname\"]\r\n" + " }\r\n" + "}, \"jsonrpc\": \"2.0\"}");
JSONObject data = postUntisRequest(request, auth);
JSONArray lessons = data.getJSONArray("result");
for (int i = 0; i < lessons.length(); i++) {
JSONObject json = lessons.getJSONObject(i);
JSONObject jsonLesson = (JSONObject) json;
String date = "" + jsonLesson.getInt("date");
int year = Integer.parseInt(date.substring(0, 4));
int month = Integer.parseInt(date.substring(4, 6));
int day = Integer.parseInt(date.substring(6, 8));
String start = formatTime(jsonLesson.getInt("startTime"));
int startHour = Integer.parseInt(start.substring(0, 2));
int startMinute = Integer.parseInt(start.substring(2, 4));
String end = formatTime(jsonLesson.getInt("endTime"));
int endHour = Integer.parseInt(end.substring(0, 2));
int endMinute = Integer.parseInt(end.substring(2, 4));
Date startDate = new Date(year, month, day, startHour, startMinute);
Date endDate = new Date(year, month, day, endHour, endMinute);
resultLessons.add(new SchoolLesson(startDate, endDate, "",
jsonLesson.getJSONArray("su").getJSONObject(0).getString("longname")));
}
Collections.sort(resultLessons, new Comparator<SchoolLesson>() {
@Override
public int compare(SchoolLesson l1, SchoolLesson l2) {
int start1 = Integer.parseInt("" + l1.getStart().getHours() + l1.getStart().getMinutes());
int start2 = Integer.parseInt("" + l2.getStart().getHours() + l2.getStart().getMinutes());
if (start1 < start2)
return -1;
else
return 1;
}
});
return resultLessons;
}
public static String formatTime(int time) {
String format = "" + time;
if (format.length() <= 3) {
format = "0" + time;
}
return format;
}
public static String formatNumber(int number) {
String format = "" + number;
if (format.length() <= 1) {
format = "0" + format;
}
return format;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment