2023-04-27 17:53:28 +03:00
|
|
|
//! This module provides the functionality to scrape and gathers all the results from the upstream
|
|
|
|
//! search engines and then removes duplicate results.
|
|
|
|
|
2023-05-07 21:18:19 +03:00
|
|
|
use std::{collections::HashMap, time::Duration};
|
|
|
|
|
2023-07-11 19:44:38 +03:00
|
|
|
use error_stack::Report;
|
2023-05-07 21:18:19 +03:00
|
|
|
use rand::Rng;
|
2023-07-11 19:44:38 +03:00
|
|
|
use tokio::task::JoinHandle;
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-04-25 16:30:04 +03:00
|
|
|
use super::{
|
2023-07-14 21:27:23 +03:00
|
|
|
aggregation_models::{EngineErrorInfo, RawSearchResult, SearchResult, SearchResults},
|
2023-04-25 16:30:04 +03:00
|
|
|
user_agent::random_user_agent,
|
|
|
|
};
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-11 19:44:38 +03:00
|
|
|
use crate::engines::{
|
|
|
|
duckduckgo,
|
|
|
|
engine_models::{EngineError, SearchEngine},
|
|
|
|
searx,
|
|
|
|
};
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-15 13:36:46 +03:00
|
|
|
/// Aliases for long type annotations
|
2023-07-14 21:27:23 +03:00
|
|
|
type FutureVec = Vec<JoinHandle<Result<HashMap<String, RawSearchResult>, Report<EngineError>>>>;
|
|
|
|
|
2023-07-17 10:47:29 +03:00
|
|
|
/// The function aggregates the scraped results from the user-selected upstream search engines.
|
|
|
|
/// These engines can be chosen either from the user interface (UI) or from the configuration file.
|
|
|
|
/// The code handles this process by matching the selected search engines and adding them to a vector.
|
|
|
|
/// This vector is then used to create an asynchronous task vector using `tokio::spawn`, which returns
|
|
|
|
/// a future. This future is awaited in another loop. Once the results are collected, they are filtered
|
|
|
|
/// to remove any errors and ensure only proper results are included. If an error is encountered, it is
|
|
|
|
/// sent to the UI along with the name of the engine and the type of error. This information is finally
|
|
|
|
/// placed in the returned `SearchResults` struct.
|
|
|
|
///
|
|
|
|
/// Additionally, the function eliminates duplicate results. If two results are identified as coming from
|
|
|
|
/// multiple engines, their names are combined to indicate that the results were fetched from these upstream
|
|
|
|
/// engines. After this, all the data in the `HashMap` is removed and placed into a struct that contains all
|
|
|
|
/// the aggregated results in a vector. Furthermore, the query used is also added to the struct. This step is
|
|
|
|
/// necessary to ensure that the search bar in the search remains populated even when searched from the query URL.
|
|
|
|
///
|
|
|
|
/// Overall, this function serves to aggregate scraped results from user-selected search engines, handling errors,
|
|
|
|
/// removing duplicates, and organizing the data for display in the UI.
|
2023-04-27 17:53:28 +03:00
|
|
|
///
|
|
|
|
/// # Example:
|
|
|
|
///
|
|
|
|
/// If you search from the url like `https://127.0.0.1/search?q=huston` then the search bar should
|
|
|
|
/// contain the word huston and not remain empty.
|
2023-05-07 21:18:19 +03:00
|
|
|
///
|
2023-04-27 17:53:28 +03:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `query` - Accepts a string to query with the above upstream search engines.
|
2023-05-02 11:58:21 +03:00
|
|
|
/// * `page` - Accepts an u32 page number.
|
2023-05-22 01:13:06 +00:00
|
|
|
/// * `random_delay` - Accepts a boolean value to add a random delay before making the request.
|
2023-07-15 13:36:46 +03:00
|
|
|
/// * `debug` - Accepts a boolean value to enable or disable debug mode option.
|
|
|
|
/// * `upstream_search_engines` - Accepts a vector of search engine names which was selected by the
|
|
|
|
/// user through the UI or the config file.
|
2023-04-27 17:53:28 +03:00
|
|
|
///
|
|
|
|
/// # Error
|
|
|
|
///
|
2023-05-07 21:18:19 +03:00
|
|
|
/// Returns an error a reqwest and scraping selector errors if any error occurs in the results
|
2023-04-27 17:53:28 +03:00
|
|
|
/// function in either `searx` or `duckduckgo` or both otherwise returns a `SearchResults struct`
|
|
|
|
/// containing appropriate values.
|
2023-04-22 14:35:07 +03:00
|
|
|
pub async fn aggregate(
|
2023-07-11 19:44:38 +03:00
|
|
|
query: String,
|
2023-05-02 11:58:21 +03:00
|
|
|
page: u32,
|
2023-05-22 01:13:06 +00:00
|
|
|
random_delay: bool,
|
2023-05-29 21:28:09 +03:00
|
|
|
debug: bool,
|
2023-07-11 19:44:38 +03:00
|
|
|
upstream_search_engines: Vec<String>,
|
2023-04-22 14:35:07 +03:00
|
|
|
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
2023-04-25 16:30:04 +03:00
|
|
|
let user_agent: String = random_user_agent();
|
2023-04-22 14:35:07 +03:00
|
|
|
let mut result_map: HashMap<String, RawSearchResult> = HashMap::new();
|
|
|
|
|
2023-05-07 21:18:19 +03:00
|
|
|
// Add a random delay before making the request.
|
2023-05-29 21:28:09 +03:00
|
|
|
if random_delay || !debug {
|
2023-05-22 01:13:06 +00:00
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let delay_secs = rng.gen_range(1..10);
|
|
|
|
std::thread::sleep(Duration::from_secs(delay_secs));
|
|
|
|
}
|
2023-05-07 21:18:19 +03:00
|
|
|
|
|
|
|
// fetch results from upstream search engines simultaneously/concurrently.
|
2023-07-14 12:56:06 +03:00
|
|
|
let search_engines: Vec<Box<dyn SearchEngine + Send + Sync>> = upstream_search_engines
|
2023-07-11 19:44:38 +03:00
|
|
|
.iter()
|
|
|
|
.map(|engine| match engine.to_lowercase().as_str() {
|
2023-07-14 12:56:06 +03:00
|
|
|
"duckduckgo" => Box::new(duckduckgo::DuckDuckGo) as Box<dyn SearchEngine + Send + Sync>,
|
|
|
|
"searx" => Box::new(searx::Searx) as Box<dyn SearchEngine + Send + Sync>,
|
|
|
|
&_ => panic!("Config Error: Incorrect config file option provided"),
|
2023-07-11 19:44:38 +03:00
|
|
|
})
|
|
|
|
.collect();
|
2023-05-07 21:18:19 +03:00
|
|
|
|
2023-07-14 12:56:06 +03:00
|
|
|
let task_capacity: usize = search_engines.len();
|
|
|
|
|
2023-07-14 21:27:23 +03:00
|
|
|
let tasks: FutureVec = search_engines
|
|
|
|
.into_iter()
|
|
|
|
.map(|search_engine| {
|
|
|
|
let query: String = query.clone();
|
|
|
|
let user_agent: String = user_agent.clone();
|
|
|
|
tokio::spawn(
|
|
|
|
async move { search_engine.results(query, page, user_agent.clone()).await },
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
2023-06-15 06:27:45 +08:00
|
|
|
|
2023-07-14 12:56:06 +03:00
|
|
|
let mut outputs = Vec::with_capacity(task_capacity);
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-11 19:44:38 +03:00
|
|
|
for task in tasks {
|
2023-07-14 12:56:06 +03:00
|
|
|
if let Ok(result) = task.await {
|
2023-07-14 21:27:23 +03:00
|
|
|
outputs.push(result)
|
2023-07-14 12:56:06 +03:00
|
|
|
}
|
2023-07-11 19:44:38 +03:00
|
|
|
}
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-14 21:27:23 +03:00
|
|
|
let mut engine_errors_info: Vec<EngineErrorInfo> = Vec::new();
|
|
|
|
|
2023-07-17 17:03:50 +03:00
|
|
|
// The code block `outputs.iter()` determines whether it is the first time the code is being run.
|
|
|
|
// It does this by checking the initial flag. If it is the first time, the code selects the first
|
|
|
|
// engine from which results are fetched and adds or extends them into the `result_map`. If the
|
|
|
|
// initially selected engine fails, the code automatically selects another engine to map or extend
|
|
|
|
// into the `result_map`. On the other hand, if an engine selected for the first time successfully
|
|
|
|
// fetches results and maps them into the `result_map`, the initial flag is set to false. Subsequently,
|
|
|
|
// the code iterates through the remaining engines one by one. It compares the fetched results from each
|
|
|
|
// engine with the results already present in the `result_map` to identify any duplicates. If duplicate
|
|
|
|
// results are found, the code groups them together with the name of the engine from which they were
|
|
|
|
// fetched, and automatically removes the duplicate results from the newly fetched data.
|
|
|
|
//
|
|
|
|
// Additionally, the code handles errors returned by the engines. It keeps track of which engines
|
|
|
|
// encountered errors and stores this information in a vector of structures called `EngineErrorInfo`.
|
|
|
|
// Each structure in this vector contains the name of the engine and the type of error it returned.
|
|
|
|
// These structures will later be added to the final `SearchResults` structure. The `SearchResults`
|
|
|
|
// structure is used to display an error box in the UI containing the relevant information from
|
|
|
|
// the `EngineErrorInfo` structure.
|
|
|
|
//
|
|
|
|
// In summary, this code block manages the selection of engines, handling of duplicate results, and tracking
|
|
|
|
// of errors in order to populate the `result_map` and provide informative feedback to the user through the
|
|
|
|
// `SearchResults` structure.
|
2023-07-11 19:44:38 +03:00
|
|
|
let mut initial: bool = true;
|
|
|
|
let mut counter: usize = 0;
|
|
|
|
outputs.iter().for_each(|results| {
|
|
|
|
if initial {
|
|
|
|
match results {
|
2023-07-14 21:27:23 +03:00
|
|
|
Ok(result) => {
|
2023-07-14 12:56:06 +03:00
|
|
|
result_map.extend(result.clone());
|
2023-07-11 19:44:38 +03:00
|
|
|
counter += 1;
|
|
|
|
initial = false
|
|
|
|
}
|
2023-07-14 21:27:23 +03:00
|
|
|
Err(error_type) => {
|
|
|
|
engine_errors_info.push(EngineErrorInfo::new(
|
|
|
|
error_type.downcast_ref::<EngineError>().unwrap(),
|
|
|
|
upstream_search_engines[counter].clone(),
|
|
|
|
));
|
2023-07-11 19:44:38 +03:00
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
match results {
|
2023-07-14 21:27:23 +03:00
|
|
|
Ok(result) => {
|
2023-07-14 12:56:06 +03:00
|
|
|
result.clone().into_iter().for_each(|(key, value)| {
|
|
|
|
result_map
|
|
|
|
.entry(key)
|
|
|
|
.and_modify(|result| {
|
|
|
|
result.add_engines(value.clone().engine());
|
|
|
|
})
|
|
|
|
.or_insert_with(|| -> RawSearchResult {
|
|
|
|
RawSearchResult::new(
|
|
|
|
value.title.clone(),
|
|
|
|
value.visiting_url.clone(),
|
|
|
|
value.description.clone(),
|
|
|
|
value.engine.clone(),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
});
|
2023-07-11 19:44:38 +03:00
|
|
|
counter += 1
|
|
|
|
}
|
2023-07-14 21:27:23 +03:00
|
|
|
Err(error_type) => {
|
|
|
|
engine_errors_info.push(EngineErrorInfo::new(
|
|
|
|
error_type.downcast_ref::<EngineError>().unwrap(),
|
|
|
|
upstream_search_engines[counter].clone(),
|
|
|
|
));
|
2023-07-11 19:44:38 +03:00
|
|
|
counter += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 16:30:04 +03:00
|
|
|
});
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-04-25 16:30:04 +03:00
|
|
|
Ok(SearchResults::new(
|
|
|
|
result_map
|
|
|
|
.into_iter()
|
|
|
|
.map(|(key, value)| {
|
|
|
|
SearchResult::new(
|
|
|
|
value.title,
|
|
|
|
value.visiting_url,
|
|
|
|
key,
|
|
|
|
value.description,
|
|
|
|
value.engine,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
query.to_string(),
|
2023-07-14 21:27:23 +03:00
|
|
|
engine_errors_info,
|
2023-04-25 16:30:04 +03:00
|
|
|
))
|
2023-04-22 14:35:07 +03:00
|
|
|
}
|