From ba38ac07dc702e7d734ec648fdb8d552c03c1458 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 29 Sep 2021 22:42:49 +0200 Subject: feat: login is now a private function It's called by the selection method. In this way it could make a re-login when a session expires. --- src/browser/mod.rs | 10 ---------- src/browser/web_browser.rs | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/main.rs | 11 ----------- 3 files changed, 42 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 9b36c94..5c078b4 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -14,16 +14,6 @@ pub async fn init(config: &Config) { } } -/// Login using the credentials from the `Config`. 'Cause its kind of nature -/// this is an `unsafe` block, so the function is defined like that -pub async unsafe fn login(credentials: &Config) -> WebDriverResult<()> { - if let Some(driver) = &WEB_BROWSER { - driver._login(credentials).await?; - } - - Ok(()) -} - /// Get the faculties available for booking a room pub async unsafe fn get_faculties() -> WebDriverResult>> { if let Some(driver) = &WEB_BROWSER { diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index d014bd6..552d72c 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -53,20 +53,46 @@ impl Browser { // Ok(()) // } + /// Compare `current_url` with the login path. If they are the same, it + /// needs to re-login + async unsafe fn check_login(&self) -> WebDriverResult { + if let Some(_d) = &self.driver { + if !_d.current_url().await?.starts_with(LOGIN_URL) { + return Ok(false); + } + + log::debug!("Logging..."); + match self._login().await { + Ok(_) => { + log::info!("Logged in Smartedu!"); + } + Err(e) => { + // Using the bot when the user is not logged in, is simply useless. + panic!( + "You can't connect: `{}`, credentials are {:?}", + e, self.credentials + ); + } + }; + } + + Ok(true) + } + /// Login on `LOGIN_URL` - pub async unsafe fn _login(&self, credentials: &Config) -> WebDriverResult<()> { + async unsafe fn _login(&self) -> WebDriverResult<()> { if let Some(_d) = &self.driver { _d.get(LOGIN_URL).await?; let cf_input = _d.find_element(By::Name("ctl01$contents$UserName")).await?; - cf_input.send_keys(&credentials.cf).await?; + cf_input.send_keys(&self.credentials.cf).await?; thread::sleep(time::Duration::from_millis(3000)); let psw_input = _d .find_element(By::Name("ctl01$contents$UserPassword")) .await?; - psw_input.send_keys(&credentials.password).await?; + psw_input.send_keys(&self.credentials.password).await?; thread::sleep(time::Duration::from_millis(3000)); @@ -108,6 +134,19 @@ impl Browser { if url != "" { _d.get(url).await?; } + + unsafe { + match self.check_login().await { + Ok(true) => { + // Login has been made, so reload the url + if url != "" { + _d.get(url).await?; + } + } + Ok(false) => {} + Err(e) => panic!("{:?}", e), + }; + } thread::sleep(time::Duration::from_millis(1000)); _d.find_element(By::Css( diff --git a/src/main.rs b/src/main.rs index 136db38..abd70b8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,17 +21,6 @@ async fn main() -> Result<(), Box> { unsafe { // Open the browser browser::init(&config).await; - - // Login using the credentials inside the `config` - match browser::login(&config).await { - Ok(_) => { - log::info!("Logged in Smartedu"); - } - Err(e) => { - // Using the bot when the user is not logged in, is simply useless. - panic!("You can't connect: `{}`, credentials are {:?}", e, config); - } - } } Dispatcher::new(bot) -- cgit v1.2.3-18-g5258