summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-08-18 07:32:25 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-08-18 07:32:25 +0200
commit9b229737e671fba989191409e927e677a4c960f7 (patch)
tree90a0b9aa4b1dcefcd0ca81086d7dcb4f8e2925e0
parent655e487deb427c5b46e47aab50b761be78a1eb90 (diff)
feat: get timetable of a space
-rw-r--r--src/browser/mod.rs11
-rw-r--r--src/browser/web_browser.rs42
-rw-r--r--src/callbacks.rs42
3 files changed, 94 insertions, 1 deletions
diff --git a/src/browser/mod.rs b/src/browser/mod.rs
index 82c9c60..4c7ef8e 100644
--- a/src/browser/mod.rs
+++ b/src/browser/mod.rs
@@ -61,3 +61,14 @@ pub async unsafe fn select_option(
Ok(false)
}
+
+/// Get the timetable of available rooms
+pub async unsafe fn get_timetable() -> WebDriverResult<Option<HashMap<String, String>>> {
+ if let Some(driver) = &WEB_BROWSER {
+ if let Some(timetable) = driver.get_timetable().await? {
+ return Ok(Some(timetable));
+ }
+ }
+
+ Ok(None)
+}
diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs
index 34e78d1..5a855d5 100644
--- a/src/browser/web_browser.rs
+++ b/src/browser/web_browser.rs
@@ -4,7 +4,7 @@ use std::{thread, time};
use thirtyfour::common::capabilities::firefox::FirefoxPreferences;
use thirtyfour::error::{WebDriverError, WebDriverErrorInfo, WebDriverErrorValue};
use thirtyfour::prelude::{By, WebDriverResult};
-use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands};
+use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands, WebElement};
/// This url is used to make the login
const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx?ReturnUrl=%2fStudenti%2fDefault.aspx";
@@ -148,6 +148,46 @@ impl Browser {
Ok(false)
}
+
+ /// Get the timetable of available hours from the driver and returns an hashmap with id->text
+ pub async fn get_timetable(&self) -> WebDriverResult<Option<HashMap<String, String>>> {
+ if let Some(_d) = &self.driver {
+ thread::sleep(time::Duration::from_millis(2000));
+
+ let table_trs = _d
+ .find_elements(By::Css("div[data-select2-id='studyPlan'] table tbody tr"))
+ .await?;
+
+ let mut timetable = HashMap::<String, String>::new();
+
+ for i in table_trs.iter() {
+ let cols: Vec<WebElement> = i.find_elements(By::Css("td")).await?;
+ if cols.len() < 6 {
+ continue;
+ }
+
+ let col_id = i.find_element(By::Css("th")).await?.text().await.unwrap();
+ let mut text_formatted = cols[0].text().await.unwrap();
+ text_formatted.push_str(
+ &format!(
+ ", {} - {}.\n",
+ cols[1].text().await.unwrap(),
+ cols[2].text().await.unwrap()
+ )
+ .to_owned()[..],
+ );
+
+ text_formatted
+ .push_str(&format!("Posti: {}", cols[6].text().await.unwrap()).to_owned()[..]);
+
+ timetable.insert(col_id, text_formatted);
+ }
+
+ return Ok(Some(timetable));
+ }
+
+ Ok(None)
+ }
}
/// The static unsafe variable used to open a web browser
diff --git a/src/callbacks.rs b/src/callbacks.rs
index a82d5e9..53ae07c 100644
--- a/src/callbacks.rs
+++ b/src/callbacks.rs
@@ -49,6 +49,48 @@ pub async fn handler(
}
}
},
+ "space" => unsafe {
+ // Select the sapce
+ match browser::select_option("select2-results__option", "data-select2-id", call[2])
+ .await
+ {
+ Ok(result) => {
+ if result {
+ let timetable = browser::get_timetable().await.unwrap();
+ let keyboard = make_inline_keyboard(
+ &timetable,
+ "timetable",
+ chat_id.parse::<i64>().unwrap(),
+ )
+ .await;
+
+ // Edit the previous spaces message with timetable' buttons
+ cx.requester
+ .edit_message_text(
+ chat_id.to_string(),
+ cx.update.message.clone().unwrap().id,
+ "When?",
+ )
+ .await?;
+
+ cx.requester
+ .edit_message_reply_markup(
+ chat_id.to_string(),
+ cx.update.message.clone().unwrap().id,
+ )
+ .reply_markup(keyboard)
+ .await?;
+
+ return Ok(true);
+ } else {
+ return Ok(false);
+ }
+ }
+ Err(_) => {
+ return Ok(false);
+ }
+ }
+ },
_ => Ok(false),
}
}