- Script is running!
This commit is contained in:
parent
bcb7703ea6
commit
a3118dcf0a
7 changed files with 132 additions and 88 deletions
|
@ -2,7 +2,7 @@ let items = FIND "gb.rob", "ACTION=EMUL1"
|
||||||
FOR EACH item IN items
|
FOR EACH item IN items
|
||||||
|
|
||||||
PRINT item.company
|
PRINT item.company
|
||||||
let website = GET WEBSITE item.company "website"
|
let website = WEBSITE OF item.company
|
||||||
PRINT website
|
PRINT website
|
||||||
|
|
||||||
WAIT 10
|
WAIT 10
|
||||||
|
@ -12,12 +12,12 @@ FOR EACH item IN items
|
||||||
|
|
||||||
let alias = LLM "Return a single word for {item.company} like a token, no spaces, no special characters, no numbers, no uppercase letters."
|
let alias = LLM "Return a single word for {item.company} like a token, no spaces, no special characters, no numbers, no uppercase letters."
|
||||||
|
|
||||||
CREATE SITE item.company + "bot", website, "site", prompt
|
CREATE SITE item.company + "bot", item.company, website, "site", prompt
|
||||||
|
|
||||||
let to = item.emailcto
|
let to = item.emailcto
|
||||||
let subject = "Simulador criado " + item.company
|
let subject = "Simulador criado " + item.company
|
||||||
let body = "O simulador " + item.company + " foi criado com sucesso. Acesse o site: " + item.company + "bot"
|
let body = "O simulador " + item.company + " foi criado com sucesso. Acesse o site: " + item.company + "bot"
|
||||||
|
|
||||||
CREATE DRAFT to, subject, body
|
CREATE_DRAFT to, subject, body
|
||||||
|
|
||||||
NEXT item
|
NEXT item
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub fn create_draft_keyword(state: &AppState, engine: &mut Engine) {
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_custom_syntax(
|
.register_custom_syntax(
|
||||||
&["CREATE", "DRAFT", "$expr$", ",", "$expr$", ",", "$expr$"],
|
&["CREATE_DRAFT", "$expr$", ",", "$expr$", ",", "$expr$"],
|
||||||
true, // Statement
|
true, // Statement
|
||||||
move |context, inputs| {
|
move |context, inputs| {
|
||||||
// Extract arguments
|
// Extract arguments
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub fn create_site_keyword(_state: &AppState, engine: &mut Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let _name = context.eval_expression_tree(&inputs[0])?;
|
let _name = context.eval_expression_tree(&inputs[0])?;
|
||||||
let company = context.eval_expression_tree(&inputs[1])?;
|
|
||||||
let _website = context.eval_expression_tree(&inputs[2])?;
|
let _website = context.eval_expression_tree(&inputs[2])?;
|
||||||
let _template = context.eval_expression_tree(&inputs[3])?;
|
let _template = context.eval_expression_tree(&inputs[3])?;
|
||||||
let prompt = context.eval_expression_tree(&inputs[4])?;
|
let prompt = context.eval_expression_tree(&inputs[4])?;
|
||||||
|
@ -29,7 +29,7 @@ pub fn create_site_keyword(_state: &AppState, engine: &mut Engine) {
|
||||||
|
|
||||||
// Create the directory structure
|
// Create the directory structure
|
||||||
let base_path = "/opt/gbo/tenants/pragmatismo/proxy/data/websites/sites.pragmatismo.com.br";
|
let base_path = "/opt/gbo/tenants/pragmatismo/proxy/data/websites/sites.pragmatismo.com.br";
|
||||||
let site_name = format!("{}bot", company.to_string());
|
let site_name = format!("{}", _name.to_string());
|
||||||
let full_path = format!("{}/{}", base_path, site_name);
|
let full_path = format!("{}/{}", base_path, site_name);
|
||||||
|
|
||||||
// Create directory if it doesn't exist
|
// Create directory if it doesn't exist
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use rhai::{Dynamic, Engine};
|
use rhai::{Dynamic, Engine};
|
||||||
use reqwest;
|
use reqwest;
|
||||||
use crate::services::state::AppState;
|
use crate::services::state::AppState;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
|
|
@ -11,22 +11,21 @@ pub fn get_website_keyword(state: &AppState, engine: &mut Engine) {
|
||||||
|
|
||||||
engine
|
engine
|
||||||
.register_custom_syntax(
|
.register_custom_syntax(
|
||||||
&["GET", "WEBSITE", "$expr$", "$expr$"],
|
&["WEBSITE", "OF", "$expr$"],
|
||||||
false,
|
false,
|
||||||
move |context, inputs| {
|
move |context, inputs| {
|
||||||
let search_term = context.eval_expression_tree(&inputs[0])?.to_string();
|
let search_term = context.eval_expression_tree(&inputs[0])?.to_string();
|
||||||
let website_hint = context.eval_expression_tree(&inputs[1])?.to_string();
|
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"GET WEBSITE executed - Search: '{}', Hint: '{}'",
|
"GET WEBSITE executed - Search: '{}'",
|
||||||
search_term, website_hint
|
search_term
|
||||||
);
|
);
|
||||||
|
|
||||||
let browser_pool_clone = browser_pool.clone();
|
let browser_pool_clone = browser_pool.clone();
|
||||||
let fut = execute_headless_browser_search(
|
let fut = execute_headless_browser_search(
|
||||||
browser_pool_clone,
|
browser_pool_clone,
|
||||||
&search_term,
|
&search_term
|
||||||
&website_hint,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
let result =
|
let result =
|
||||||
|
@ -41,20 +40,18 @@ pub fn get_website_keyword(state: &AppState, engine: &mut Engine) {
|
||||||
|
|
||||||
pub async fn execute_headless_browser_search(
|
pub async fn execute_headless_browser_search(
|
||||||
browser_pool: Arc<BrowserPool>, // Adjust path as needed
|
browser_pool: Arc<BrowserPool>, // Adjust path as needed
|
||||||
search_term: &str,
|
search_term: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||||
website_hint: &str,
|
|
||||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
|
||||||
println!(
|
println!(
|
||||||
"Starting headless browser search: '{}' targeting '{}'",
|
"Starting headless browser search: '{}' ",
|
||||||
search_term, website_hint
|
search_term
|
||||||
);
|
);
|
||||||
|
|
||||||
let search_term = search_term.to_string();
|
let search_term = search_term.to_string();
|
||||||
let website_hint = website_hint.to_string();
|
|
||||||
|
|
||||||
let result = browser_pool
|
let result = browser_pool
|
||||||
.with_browser(|driver| {
|
.with_browser(|driver| {
|
||||||
Box::pin(async move { perform_search(driver, &search_term, &website_hint).await })
|
Box::pin(async move { perform_search(driver, &search_term).await })
|
||||||
})
|
})
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -63,15 +60,9 @@ pub async fn execute_headless_browser_search(
|
||||||
|
|
||||||
async fn perform_search(
|
async fn perform_search(
|
||||||
driver: WebDriver,
|
driver: WebDriver,
|
||||||
search_term: &str,
|
search_term: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||||
website_hint: &str,
|
|
||||||
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
|
||||||
// Configure the search query
|
// Configure the search query
|
||||||
let query = if website_hint.trim().is_empty() {
|
let query = search_term.to_string();
|
||||||
search_term.to_string()
|
|
||||||
} else {
|
|
||||||
format!("{} site:{}", search_term, website_hint)
|
|
||||||
};
|
|
||||||
|
|
||||||
// Navigate to DuckDuckGo
|
// Navigate to DuckDuckGo
|
||||||
println!("Navigating to DuckDuckGo...");
|
println!("Navigating to DuckDuckGo...");
|
||||||
|
|
|
@ -28,8 +28,8 @@ impl ScriptService {
|
||||||
find_keyword(state, &mut engine);
|
find_keyword(state, &mut engine);
|
||||||
for_keyword(state, &mut engine);
|
for_keyword(state, &mut engine);
|
||||||
llm_keyword(state, &mut engine);
|
llm_keyword(state, &mut engine);
|
||||||
get_keyword(state, &mut engine);
|
|
||||||
get_website_keyword(state, &mut engine);
|
get_website_keyword(state, &mut engine);
|
||||||
|
get_keyword(state, &mut engine);
|
||||||
set_keyword(state, &mut engine);
|
set_keyword(state, &mut engine);
|
||||||
wait_keyword(state, &mut engine);
|
wait_keyword(state, &mut engine);
|
||||||
print_keyword(state, &mut engine);
|
print_keyword(state, &mut engine);
|
||||||
|
@ -127,6 +127,7 @@ impl ScriptService {
|
||||||
/// Preprocesses BASIC-style script to handle semicolon-free syntax
|
/// Preprocesses BASIC-style script to handle semicolon-free syntax
|
||||||
pub fn compile(&self, script: &str) -> Result<rhai::AST, Box<EvalAltResult>> {
|
pub fn compile(&self, script: &str) -> Result<rhai::AST, Box<EvalAltResult>> {
|
||||||
let processed_script = self.preprocess_basic_script(script);
|
let processed_script = self.preprocess_basic_script(script);
|
||||||
|
println!("Processed Script:\n{}", processed_script);
|
||||||
match self.engine.compile(&processed_script) {
|
match self.engine.compile(&processed_script) {
|
||||||
Ok(ast) => Ok(ast),
|
Ok(ast) => Ok(ast),
|
||||||
Err(parse_error) => Err(Box::new(EvalAltResult::from(parse_error))),
|
Err(parse_error) => Err(Box::new(EvalAltResult::from(parse_error))),
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
// wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
|
||||||
|
// sudo dpkg -i google-chrome-stable_current_amd64.deb
|
||||||
|
|
||||||
use crate::services::utils;
|
use crate::services::utils;
|
||||||
use log::debug;
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::env::temp_dir;
|
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
|
@ -91,64 +93,114 @@ impl BrowserSetup {
|
||||||
|
|
||||||
Err("Brave browser not found. Please install Brave first.".into())
|
Err("Brave browser not found. Please install Brave first.".into())
|
||||||
}
|
}
|
||||||
|
async fn setup_chromedriver() -> Result<String, Box<dyn std::error::Error>> {
|
||||||
async fn setup_chromedriver() -> Result<String, Box<dyn std::error::Error>> {
|
// Create chromedriver directory in executable's parent directory
|
||||||
let mut chromedriver_path = env::current_exe()?.parent().unwrap().to_path_buf();
|
let mut chromedriver_dir = env::current_exe()?
|
||||||
chromedriver_path.push("chromedriver");
|
.parent()
|
||||||
|
.unwrap()
|
||||||
// Check if chromedriver exists
|
.to_path_buf();
|
||||||
if fs::metadata(&chromedriver_path).await.is_err() {
|
chromedriver_dir.push("chromedriver");
|
||||||
println!("Downloading chromedriver...");
|
|
||||||
|
// Ensure the directory exists
|
||||||
// Note: This URL structure is outdated. Consider using Chrome for Testing endpoints
|
if !chromedriver_dir.exists() {
|
||||||
let (base_url, platform) =
|
fs::create_dir(&chromedriver_dir).await?;
|
||||||
match (cfg!(target_os = "windows"), cfg!(target_arch = "x86_64")) {
|
|
||||||
(true, true) => (
|
|
||||||
"https://chromedriver.storage.googleapis.com/114.0.5735.90",
|
|
||||||
"win32",
|
|
||||||
),
|
|
||||||
(false, true) if cfg!(target_os = "macos") => (
|
|
||||||
"https://chromedriver.storage.googleapis.com/114.0.5735.90",
|
|
||||||
"mac64",
|
|
||||||
),
|
|
||||||
(false, true) => (
|
|
||||||
"https://chromedriver.storage.googleapis.com/114.0.5735.90",
|
|
||||||
"linux64",
|
|
||||||
),
|
|
||||||
_ => return Err("Unsupported platform".into()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let download_url = format!("{}/chromedriver_{}.zip", base_url, platform);
|
|
||||||
|
|
||||||
let mut zip_path = temp_dir();
|
|
||||||
zip_path.push("chromedriver.zip");
|
|
||||||
|
|
||||||
utils::download_file(&download_url, &zip_path.to_str().unwrap()).await?;
|
|
||||||
|
|
||||||
let extract_result = utils::extract_zip_recursive(&zip_path, &chromedriver_path);
|
|
||||||
if let Err(e) = extract_result {
|
|
||||||
debug!("Error extracting ZIP: {}", e);
|
|
||||||
}
|
|
||||||
// Clean up zip file
|
|
||||||
let _ = fs::remove_file(&zip_path).await;
|
|
||||||
|
|
||||||
if cfg!(target_os = "windows") {
|
|
||||||
chromedriver_path.push("chromedriver.exe");
|
|
||||||
} else {
|
|
||||||
chromedriver_path.push("chromedriver");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(unix)]
|
|
||||||
{
|
|
||||||
use std::os::unix::fs::PermissionsExt;
|
|
||||||
let mut perms = fs::metadata(&chromedriver_path).await?.permissions();
|
|
||||||
perms.set_mode(0o755); // Make executable
|
|
||||||
fs::set_permissions(&chromedriver_path, perms).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(chromedriver_path.to_string_lossy().to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine the final chromedriver path
|
||||||
|
let chromedriver_path = if cfg!(target_os = "windows") {
|
||||||
|
chromedriver_dir.join("chromedriver.exe")
|
||||||
|
} else {
|
||||||
|
chromedriver_dir.join("chromedriver")
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check if chromedriver exists
|
||||||
|
if fs::metadata(&chromedriver_path).await.is_err() {
|
||||||
|
let (download_url, platform) = match (cfg!(target_os = "windows"), cfg!(target_arch = "x86_64")) {
|
||||||
|
(true, true) => (
|
||||||
|
"https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/win64/chromedriver-win64.zip",
|
||||||
|
"win64",
|
||||||
|
),
|
||||||
|
(true, false) => (
|
||||||
|
"https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/win32/chromedriver-win32.zip",
|
||||||
|
"win32",
|
||||||
|
),
|
||||||
|
(false, true) if cfg!(target_os = "macos") && cfg!(target_arch = "aarch64") => (
|
||||||
|
"https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/mac-arm64/chromedriver-mac-arm64.zip",
|
||||||
|
"mac-arm64",
|
||||||
|
),
|
||||||
|
(false, true) if cfg!(target_os = "macos") => (
|
||||||
|
"https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/mac-x64/chromedriver-mac-x64.zip",
|
||||||
|
"mac-x64",
|
||||||
|
),
|
||||||
|
(false, true) => (
|
||||||
|
"https://storage.googleapis.com/chrome-for-testing-public/138.0.7204.183/linux64/chromedriver-linux64.zip",
|
||||||
|
"linux64",
|
||||||
|
),
|
||||||
|
_ => return Err("Unsupported platform".into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut zip_path = std::env::temp_dir();
|
||||||
|
zip_path.push("chromedriver.zip");
|
||||||
|
println!("Downloading chromedriver for {}...", platform);
|
||||||
|
|
||||||
|
// Download the zip file
|
||||||
|
utils::download_file(download_url, &zip_path.to_str().unwrap()).await?;
|
||||||
|
|
||||||
|
// Extract the zip to a temporary directory first
|
||||||
|
let mut temp_extract_dir = std::env::temp_dir();
|
||||||
|
temp_extract_dir.push("chromedriver_extract");
|
||||||
|
let mut temp_extract_dir1 = temp_extract_dir.clone();
|
||||||
|
|
||||||
|
// Clean up any previous extraction
|
||||||
|
let _ = fs::remove_dir_all(&temp_extract_dir).await;
|
||||||
|
fs::create_dir(&temp_extract_dir).await?;
|
||||||
|
|
||||||
|
utils::extract_zip_recursive(&zip_path, &temp_extract_dir)?;
|
||||||
|
|
||||||
|
// Chrome for Testing zips contain a platform-specific directory
|
||||||
|
// Find the chromedriver binary in the extracted structure
|
||||||
|
let mut extracted_binary_path = temp_extract_dir;
|
||||||
|
extracted_binary_path.push(format!("chromedriver-{}", platform));
|
||||||
|
extracted_binary_path.push(if cfg!(target_os = "windows") {
|
||||||
|
"chromedriver.exe"
|
||||||
|
} else {
|
||||||
|
"chromedriver"
|
||||||
|
});
|
||||||
|
|
||||||
|
// Try to move the file, fall back to copy if cross-device
|
||||||
|
match fs::rename(&extracted_binary_path, &chromedriver_path).await {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::CrossesDevices => {
|
||||||
|
// Cross-device move failed, use copy instead
|
||||||
|
fs::copy(&extracted_binary_path, &chromedriver_path).await?;
|
||||||
|
// Set permissions on the copied file
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
let mut perms = fs::metadata(&chromedriver_path).await?.permissions();
|
||||||
|
perms.set_mode(0o755);
|
||||||
|
fs::set_permissions(&chromedriver_path, perms).await?;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => return Err(e.into()),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up
|
||||||
|
let _ = fs::remove_file(&zip_path).await;
|
||||||
|
let _ = fs::remove_dir_all(temp_extract_dir1).await;
|
||||||
|
|
||||||
|
// Set executable permissions (if not already set during copy)
|
||||||
|
#[cfg(unix)]
|
||||||
|
{
|
||||||
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
let mut perms = fs::metadata(&chromedriver_path).await?.permissions();
|
||||||
|
perms.set_mode(0o755);
|
||||||
|
fs::set_permissions(&chromedriver_path, perms).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(chromedriver_path.to_string_lossy().to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Modified BrowserPool initialization
|
// Modified BrowserPool initialization
|
||||||
|
|
Loading…
Add table
Reference in a new issue