added endpoint for tomorrow timetable

This commit is contained in:
Denys Konovalov 2021-11-03 17:43:20 +01:00
parent 2e3e856d72
commit 0f87ca8b13
2 changed files with 218 additions and 2 deletions

@ -1,7 +1,7 @@
use crate::config; use crate::config;
use crate::schema::timetable; use crate::schema::timetable;
use crate::DbConn; use crate::DbConn;
use chrono::Local; use chrono::{Duration, Local};
use diesel::{Insertable, Queryable}; use diesel::{Insertable, Queryable};
use quickxml_to_serde::{xml_string_to_json, Config}; use quickxml_to_serde::{xml_string_to_json, Config};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@ -48,6 +48,24 @@ async fn get_today_timetable_xml() -> serde_json::value::Value {
xml_string_to_json(resp, &Config::new_with_defaults()).unwrap() xml_string_to_json(resp, &Config::new_with_defaults()).unwrap()
} }
async fn get_tomorrow_timetable_xml() -> serde_json::value::Value {
let client = reqwest::Client::new();
let resp = client
.get(format!(
"{}/PlanKl{}.xml",
config::TIMETABLE_URL,
(Local::today() + Duration::days(1)).format("%Y%m%d")
))
.basic_auth(config::TIMETABLE_USER, config::TIMETABLE_PASSWORD)
.send()
.await
.unwrap()
.text()
.await
.unwrap();
xml_string_to_json(resp, &Config::new_with_defaults()).unwrap()
}
async fn get_timetable_xml() -> serde_json::value::Value { async fn get_timetable_xml() -> serde_json::value::Value {
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let resp = client let resp = client
@ -78,6 +96,22 @@ async fn get_today_timetable_xml_data() -> Vec<serde_json::value::Value> {
classes.to_owned() classes.to_owned()
} }
async fn get_tomorrow_timetable_xml_data() -> Vec<serde_json::value::Value> {
let xml = get_tomorrow_timetable_xml().await;
let classes = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("Klassen")
.unwrap()
.get("Kl")
.unwrap()
.as_array()
.unwrap();
classes.to_owned()
}
async fn get_timetable_xml_data() -> Vec<serde_json::value::Value> { async fn get_timetable_xml_data() -> Vec<serde_json::value::Value> {
let xml = get_timetable_xml().await; let xml = get_timetable_xml().await;
let classes = xml let classes = xml
@ -187,6 +221,99 @@ pub async fn get_today_timetable(_conn: DbConn) -> Vec<Timetable> {
timetable timetable
} }
pub async fn get_tomorrow_timetable(_conn: DbConn) -> Vec<Timetable> {
let xml = get_tomorrow_timetable_xml().await;
let classes = get_tomorrow_timetable_xml_data().await;
let mut timetable: Vec<Timetable> = Vec::new();
for i in classes.iter() {
let mut courses: Vec<rocket::serde::json::Value> = Vec::new();
let nothing = json!([""]);
let std = i
.as_object()
.unwrap()
.get("Pl")
.unwrap()
.as_object()
.unwrap()
.get("Std")
.unwrap_or(&nothing);
let mut plan = vec![];
if std.is_array() {
plan.extend(std.as_array().unwrap().iter().cloned())
} else if std.is_object() {
plan.push(std.clone())
}
for i in &plan {
if i.as_object() != None {
courses.push(i.to_owned());
} else {
dbg!("Failed: {:?}", &i);
}
}
let empty_list = serde_json::Value::Array(vec![]);
let empty_obj = serde_json::Value::Object(Map::new());
let empty_vec = Vec::new();
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap_or(&empty_vec);
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let response = TimetableData {
count: plan.len(),
courses,
info,
};
let timetable_element = Timetable {
date: String::from(
xml.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("Kopf")
.unwrap()
.get("DatumPlan")
.unwrap()
.as_str()
.unwrap(),
),
updated: String::from(
xml.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("Kopf")
.unwrap()
.get("zeitstempel")
.unwrap()
.as_str()
.unwrap(),
),
class: String::from(
i.as_object()
.unwrap()
.get("Kurz")
.unwrap()
.as_str()
.unwrap(),
),
timetable_data: serde_json::from_str(&json!(response).to_string()).unwrap(),
};
timetable.push(timetable_element)
}
timetable
}
pub async fn get_timetable(_conn: DbConn) -> Vec<Timetable> { pub async fn get_timetable(_conn: DbConn) -> Vec<Timetable> {
let xml = get_timetable_xml().await; let xml = get_timetable_xml().await;
let classes = get_timetable_xml_data().await; let classes = get_timetable_xml_data().await;
@ -348,6 +475,74 @@ pub async fn get_today_class_timetable(_conn: DbConn, class: String) -> Timetabl
response response
} }
pub async fn get_tomorrow_class_timetable(_conn: DbConn, class: String) -> TimetableData {
let xml = get_tomorrow_timetable_xml().await;
let classes = get_tomorrow_timetable_xml_data().await;
let courses: Vec<rocket::serde::json::Value> = Vec::new();
let empty_list = serde_json::Value::Array(vec![]);
let empty_obj = serde_json::Value::Object(Map::new());
let empty_vec = Vec::new();
let info_list = xml
.as_object()
.unwrap()
.get("VpMobil")
.unwrap()
.get("ZusatzInfo")
.unwrap_or(&empty_obj)
.get("ZiZeile")
.unwrap_or(&empty_list)
.as_array()
.unwrap_or(&empty_vec);
let mut info = String::new();
for item in info_list {
info.push_str(item.as_str().unwrap_or("\r\n"));
info.push_str("\r\n");
}
let mut response = TimetableData {
count: 0,
courses,
info,
};
for i in classes.iter() {
if i.as_object()
.unwrap()
.get("Kurz")
.unwrap()
.as_str()
.unwrap()
.replace("/", "_")
== class
{
let nothing = json!([""]);
let std = i
.as_object()
.unwrap()
.get("Pl")
.unwrap()
.as_object()
.unwrap()
.get("Std")
.unwrap_or(&nothing);
let mut plan = vec![];
if std.is_array() {
plan.extend(std.as_array().unwrap().iter().cloned())
} else if std.is_object() {
plan.push(std.clone())
}
response.count = plan.len();
for i in plan {
if i.as_object() != None {
response.courses.push(i.to_owned());
} else {
dbg!("Failed: {:?}", &i);
}
}
break;
}
}
response
}
pub async fn get_class_timetable(_conn: DbConn, class: String) -> TimetableData { pub async fn get_class_timetable(_conn: DbConn, class: String) -> TimetableData {
let xml = get_timetable_xml().await; let xml = get_timetable_xml().await;
let classes = get_timetable_xml_data().await; let classes = get_timetable_xml_data().await;

@ -180,6 +180,15 @@ async fn get_today_timetable(
Json::from(timetable) Json::from(timetable)
} }
#[get("/tomorrow")]
async fn get_tomorrow_timetable(
conn: DbConn,
_key: ApiKey<'_>,
) -> Json<Vec<timetable_connector::Timetable>> {
let timetable = timetable_connector::get_tomorrow_timetable(conn).await;
Json::from(timetable)
}
#[get("/<class>")] #[get("/<class>")]
async fn get_class_timetable( async fn get_class_timetable(
conn: DbConn, conn: DbConn,
@ -200,6 +209,16 @@ async fn get_today_class_timetable(
Json::from(timetable) Json::from(timetable)
} }
#[get("/<class>/tomorrow")]
async fn get_tomorrow_class_timetable(
conn: DbConn,
class: String,
_key: ApiKey<'_>,
) -> Json<timetable_connector::TimetableData> {
let timetable = timetable_connector::get_tomorrow_class_timetable(conn, class).await;
Json::from(timetable)
}
#[get("/")] #[get("/")]
async fn get_classes(_key: ApiKey<'_>) -> Json<Vec<String>> { async fn get_classes(_key: ApiKey<'_>) -> Json<Vec<String>> {
let class_list = timetable_connector::get_classes().await; let class_list = timetable_connector::get_classes().await;
@ -227,7 +246,9 @@ fn rocket() -> _ {
get_timetable, get_timetable,
get_class_timetable, get_class_timetable,
get_today_timetable, get_today_timetable,
get_today_class_timetable get_today_class_timetable,
get_tomorrow_timetable,
get_tomorrow_class_timetable
], ],
) )
.mount("/api/classes", routes![get_classes]) .mount("/api/classes", routes![get_classes])