Added lessons endpoint (Unterricht)

This commit is contained in:
Denys Konovalov 2021-11-02 22:26:53 +01:00
parent 57150ce7f1
commit 3cca060844
2 changed files with 61 additions and 0 deletions

@ -23,6 +23,13 @@ pub struct TimetableData {
pub info: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Lesson {
pub subject: String,
pub teacher: String,
pub id: i64,
}
async fn get_today_timetable_xml() -> serde_json::value::Value {
let client = reqwest::Client::new();
let resp = client
@ -420,3 +427,47 @@ pub async fn get_classes() -> Vec<String> {
}
class_list
}
pub async fn get_class_lessons(class: String) -> Vec<Lesson> {
let classes = get_timetable_xml_data().await;
let mut lesson_list: Vec<Lesson> = Vec::new();
for i in classes.iter() {
let empty_list = serde_json::Value::Array(Vec::new());
if i.as_object()
.unwrap()
.get("Kurz")
.unwrap()
.as_str()
.unwrap()
.replace("/", "_")
== class
{
let class_lessons = i
.as_object()
.unwrap()
.get("Unterricht")
.unwrap()
.as_object()
.unwrap()
.get("Ue")
.unwrap_or(&empty_list)
.as_array()
.unwrap();
for lesson in class_lessons.iter() {
let lesson = lesson
.as_object()
.unwrap()
.get("UeNr")
.unwrap()
.as_object()
.unwrap();
lesson_list.push(Lesson {
subject: lesson.get("@UeFa").unwrap().as_str().unwrap().to_string(),
teacher: lesson.get("@UeLe").unwrap().as_str().unwrap().to_string(),
id: lesson.get("#text").unwrap().as_i64().unwrap(),
})
}
}
}
lesson_list
}

@ -203,6 +203,15 @@ async fn get_classes(_key: ApiKey<'_>) -> Json<Vec<String>> {
Json::from(class_list)
}
#[get("/<class>")]
async fn get_class_lessons(
_key: ApiKey<'_>,
class: String,
) -> Json<Vec<timetable_connector::Lesson>> {
let lesson_list = timetable_connector::get_class_lessons(class).await;
Json::from(lesson_list)
}
#[launch]
fn rocket() -> _ {
rocket::build()
@ -219,5 +228,6 @@ fn rocket() -> _ {
],
)
.mount("/api/classes", routes![get_classes])
.mount("/api/lessons", routes![get_class_lessons])
.mount("/api/userinfo", routes![get_userinfo])
}