From 9f6d275620e29295a41bcca342ee0da531664721 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 29 Sep 2021 22:34:26 +0200 Subject: chore: save crendetials into the browser struct --- src/browser/mod.rs | 4 ++-- src/browser/web_browser.rs | 17 +++++++++++++++-- src/main.rs | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 528849d..9b36c94 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -8,9 +8,9 @@ mod web_browser; /// Create a new instance of `Browser` and associate it with the static variable `WEB_BROWSER`. /// This is an unsecure type of usage, so the block is inside the `unsafe` block -pub async fn init(driver_url: &String) { +pub async fn init(config: &Config) { unsafe { - WEB_BROWSER = Some(Browser::new(driver_url).await); + WEB_BROWSER = Some(Browser::new(config).await); } } diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index 59b2dff..d014bd6 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -11,15 +11,22 @@ const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/L /// This url is used to go to the page where a student can book a room for study pub const ROOMS_URL: &str = "https://studenti.smartedu.unict.it/StudentSpaceReserv?Type=unaTantum"; +#[derive(Debug)] +struct Credentials { + cf: String, + password: String, +} + /// Browser struct pub struct Browser { /// The driver for Firefox, it could be `None` driver: Option, + credentials: Credentials, } impl Browser { /// Create a new `Browser` with a Firefox driver with a personalized User-Agent - pub async fn new(driver_url: &String) -> Self { + pub async fn new(config: &Config) -> Self { let user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:90.0) Gecko/20100101 Firefox/90.0"; @@ -30,8 +37,14 @@ impl Browser { let _ = caps.set_preferences(prefs); let _ = caps.set_headless(); + let credentials = Credentials { + cf: config.cf.clone(), + password: config.password.clone(), + }; + Self { - driver: Some(WebDriver::new(driver_url, caps).await.unwrap()), + driver: Some(WebDriver::new(&config.driver_url, caps).await.unwrap()), + credentials, } } diff --git a/src/main.rs b/src/main.rs index 6b85559..136db38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ async fn main() -> Result<(), Box> { unsafe { // Open the browser - browser::init(&config.driver_url).await; + browser::init(&config).await; // Login using the credentials inside the `config` match browser::login(&config).await { -- cgit v1.2.3-18-g5258