summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2021-07-30 19:24:41 +0200
committerSanto Cariotti <santo@dcariotti.me>2021-07-30 19:24:41 +0200
commit99968c72a5efbd535362e050baf314f9e0cff709 (patch)
tree910f43a609186a5719b345632fc80d218cb7b627
parentbba0f862251c737e576c949320f2eecf15a74057 (diff)
refactor: browser as modular funcs
-rw-r--r--src/browser.rs43
-rw-r--r--src/browser/mod.rs19
-rw-r--r--src/browser/web_browser.rs68
-rw-r--r--src/main.rs13
4 files changed, 92 insertions, 51 deletions
diff --git a/src/browser.rs b/src/browser.rs
deleted file mode 100644
index 4900f6c..0000000
--- a/src/browser.rs
+++ /dev/null
@@ -1,43 +0,0 @@
-use crate::config::Config;
-use std::{thread, time};
-use thirtyfour::prelude::{By, WebDriverResult};
-use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands};
-
-const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx";
-
-pub async fn init(driver_url: &String) -> WebDriver {
- let driver = match WebDriver::new(driver_url, FirefoxCapabilities::new()).await {
- Ok(driver) => driver,
- Err(_) => {
- panic!("Firefox can't be opened");
- }
- };
-
- return driver;
-}
-
-pub async fn login(driver: &WebDriver, credentials: &Config) -> WebDriverResult<()> {
- driver.get(LOGIN_URL).await?;
-
- let cf_input = driver
- .find_element(By::Name("ctl01$contents$UserName"))
- .await?;
- cf_input.send_keys(&credentials.cf).await?;
-
- let psw_input = driver
- .find_element(By::Name("ctl01$contents$UserPassword"))
- .await?;
- psw_input.send_keys(&credentials.password).await?;
-
- thread::sleep(time::Duration::from_millis(1000));
-
- driver
- .find_element(By::Name("ctl01$contents$LogonButton"))
- .await?
- .click()
- .await?;
-
- thread::sleep(time::Duration::from_millis(2000));
-
- Ok(())
-}
diff --git a/src/browser/mod.rs b/src/browser/mod.rs
new file mode 100644
index 0000000..8359c51
--- /dev/null
+++ b/src/browser/mod.rs
@@ -0,0 +1,19 @@
+use self::web_browser::{Browser, WEB_BROWSER};
+use crate::Config;
+use thirtyfour::prelude::WebDriverResult;
+
+mod web_browser;
+
+pub async fn init(driver_url: &String) {
+ unsafe {
+ WEB_BROWSER = Some(Browser::new(driver_url).await);
+ }
+}
+
+pub async unsafe fn login(credentials: &Config) -> WebDriverResult<()> {
+ if let Some(driver) = &WEB_BROWSER {
+ driver._login(credentials).await?;
+ }
+
+ Ok(())
+}
diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs
new file mode 100644
index 0000000..53b8baa
--- /dev/null
+++ b/src/browser/web_browser.rs
@@ -0,0 +1,68 @@
+use crate::Config;
+use std::{thread, time};
+use thirtyfour::error::{WebDriverError, WebDriverErrorInfo, WebDriverErrorValue};
+use thirtyfour::prelude::{By, WebDriverResult};
+use thirtyfour::{FirefoxCapabilities, WebDriver, WebDriverCommands};
+
+const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx";
+
+pub struct Browser {
+ driver: Option<WebDriver>,
+}
+
+impl Browser {
+ pub async fn new(driver_url: &String) -> Self {
+ Self {
+ driver: Some(
+ WebDriver::new(driver_url, FirefoxCapabilities::new())
+ .await
+ .unwrap(),
+ ),
+ }
+ }
+
+ // TODO: Close the driver fixing the borrowing error
+ // async fn _close(self) -> Result<(), Box<dyn Error>> {
+ // Ok(())
+ // }
+
+ pub async unsafe fn _login(&self, credentials: &Config) -> 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?;
+
+ let psw_input = _d
+ .find_element(By::Name("ctl01$contents$UserPassword"))
+ .await?;
+ psw_input.send_keys(&credentials.password).await?;
+
+ thread::sleep(time::Duration::from_millis(1000));
+
+ _d.find_element(By::Name("ctl01$contents$LogonButton"))
+ .await?
+ .click()
+ .await?;
+
+ thread::sleep(time::Duration::from_millis(2000));
+
+ if _d.current_url().await? == LOGIN_URL {
+ return Err(WebDriverError::SessionNotCreated(WebDriverErrorInfo {
+ status: 400,
+ error: "SessionNotCreated".to_string(),
+ value: WebDriverErrorValue {
+ message: "SessionNotCreated".to_string(),
+ error: None,
+ stacktrace: None,
+ data: None,
+ },
+ }));
+ }
+ }
+
+ Ok(())
+ }
+}
+
+pub static mut WEB_BROWSER: Option<Browser> = None;
diff --git a/src/main.rs b/src/main.rs
index b854f49..203dca2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,5 @@
use std::error::Error;
use teloxide::prelude::*;
-use thirtyfour::WebDriver;
-
mod browser;
mod commands;
mod config;
@@ -18,13 +16,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
let bot = Bot::from_env().auto_send();
let config = Config::from_env().unwrap();
- let driver: WebDriver = browser::init(&config.driver_url).await;
- match browser::login(&driver, &config).await {
- Ok(_) => {}
- Err(e) => {
+ unsafe {
+ browser::init(&config.driver_url).await;
+
+ if let Err(e) = browser::login(&config).await {
panic!("You can't connect: `{}`, credentials are {:?}", e, config);
}
- };
+ }
Dispatcher::new(bot)
.messages_handler(|rx: DispatcherHandlerRx<AutoSend<Bot>, Message>| {
@@ -44,7 +42,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
.await;
log::info!("Closing bot... Goodbye!");
- driver.quit().await?;
Ok(())
}