diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-09-29 22:42:49 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-09-29 22:42:49 +0200 |
commit | ba38ac07dc702e7d734ec648fdb8d552c03c1458 (patch) | |
tree | 35355bb6ff7138a20e424356ab189058bb81362f | |
parent | 9f6d275620e29295a41bcca342ee0da531664721 (diff) |
feat: login is now a private function0.1.0
It's called by the selection method. In this way it could make a re-login
when a session expires.
-rw-r--r-- | src/browser/mod.rs | 10 | ||||
-rw-r--r-- | src/browser/web_browser.rs | 45 | ||||
-rw-r--r-- | src/main.rs | 11 |
3 files changed, 42 insertions, 24 deletions
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<Option<HashMap<String, String>>> { 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<bool> { + 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<dyn Error>> { 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) |