diff options
author | Santo Cariotti <santo@dcariotti.me> | 2021-08-04 17:22:21 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2021-08-04 17:22:21 +0200 |
commit | 909e2cbdf8cbb311f4abef2c0c1d176987580a76 (patch) | |
tree | dbcfa17a56a12c7549c9867842cd44b14c917de1 /src | |
parent | 1be956d27668dcf2052ea82b191ae1917e6cb11b (diff) |
feat: get faculties for the /room command
Diffstat (limited to 'src')
-rw-r--r-- | src/browser/mod.rs | 17 | ||||
-rw-r--r-- | src/browser/web_browser.rs | 34 | ||||
-rw-r--r-- | src/commands.rs | 25 |
3 files changed, 73 insertions, 3 deletions
diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 8359c51..2765c6d 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use self::web_browser::{Browser, WEB_BROWSER}; use crate::Config; use thirtyfour::prelude::WebDriverResult; @@ -17,3 +19,18 @@ pub async unsafe fn login(credentials: &Config) -> WebDriverResult<()> { Ok(()) } + +pub async unsafe fn get_faculties() -> WebDriverResult<Option<HashMap<String, String>>> { + if let Some(driver) = &WEB_BROWSER { + match driver.faculties().await? { + Some(faculties) => { + return Ok(Some(faculties)); + } + None => { + return Ok(Some(HashMap::<String, String>::new())); + } + }; + } + + Ok(Some(HashMap::<String, String>::new())) +} diff --git a/src/browser/web_browser.rs b/src/browser/web_browser.rs index 804c82f..d2ba6c4 100644 --- a/src/browser/web_browser.rs +++ b/src/browser/web_browser.rs @@ -1,10 +1,13 @@ use crate::Config; +use std::collections::HashMap; 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}; const LOGIN_URL: &str = "https://studenti.smartedu.unict.it/WorkFlow2011/Logon/Logon.aspx?ReturnUrl=%2fStudenti%2fDefault.aspx"; +const ROOMS_URL: &str = "https://studenti.smartedu.unict.it/StudentSpaceReserv?Type=unaTantum"; pub struct Browser { driver: Option<WebDriver>, @@ -70,6 +73,37 @@ impl Browser { Ok(()) } + + pub async fn faculties(&self) -> WebDriverResult<Option<HashMap<String, String>>> { + if let Some(_d) = &self.driver { + _d.get(ROOMS_URL).await?; + thread::sleep(time::Duration::from_millis(1000)); + + _d.find_element(By::Css( + "span[aria-labelledby='select2-dipartimentoSelector-container']", + )) + .await? + .click() + .await?; + + let list_elements = _d + .find_elements(By::Css("#select2-dipartimentoSelector-results li")) + .await?; + + let mut faculties_ids = HashMap::<String, String>::new(); + + for i in list_elements { + faculties_ids.insert( + i.get_attribute("data-select2-id").await.unwrap().unwrap(), + i.text().await.unwrap(), + ); + } + + return Ok(Some(faculties_ids)); + } + + Ok(None) + } } pub static mut WEB_BROWSER: Option<Browser> = None; diff --git a/src/commands.rs b/src/commands.rs index a3a3694..2653600 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,3 +1,4 @@ +use crate::browser; use crate::config::Config; use std::error::Error; use teloxide::payloads::SendMessageSetters; @@ -39,9 +40,27 @@ pub async fn handler( cx.answer(Command::descriptions()).await?; } Command::Room => { - let url_button = - InlineKeyboardButton::callback("hello".to_string(), "hello_call".to_string()); - let keyboard = InlineKeyboardMarkup::default().append_row(vec![url_button]); + let faculties; + let mut faculties_array: Vec<Vec<InlineKeyboardButton>> = vec![]; + unsafe { + faculties = browser::get_faculties().await.unwrap(); + } + + if let Some(faculties_texts) = faculties { + for (key, value) in faculties_texts { + faculties_array.push(vec![InlineKeyboardButton::callback( + value, + format!("faculty_{}", key), + )]); + } + } else { + faculties_array.push(vec![InlineKeyboardButton::callback( + "No such element".to_string(), + "".into(), + )]); + } + + let keyboard = InlineKeyboardMarkup::new(faculties_array); cx.answer("Where?").reply_markup(keyboard).await?; } } |