Spaces:
Runtime error
Runtime error
Merge pull request #13 from neon-mmd/optimise-code-for-server-use
Browse files
src/engines/duckduckgo.rs
CHANGED
@@ -2,9 +2,8 @@
|
|
2 |
//! by querying the upstream duckduckgo search engine with user provided query and with a page
|
3 |
//! number if provided.
|
4 |
|
5 |
-
use std::
|
6 |
|
7 |
-
use rand::Rng;
|
8 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, REFERER, USER_AGENT};
|
9 |
use scraper::{Html, Selector};
|
10 |
|
@@ -47,11 +46,6 @@ pub async fn results(
|
|
47 |
}
|
48 |
};
|
49 |
|
50 |
-
// Add a random delay before making the request.
|
51 |
-
let mut rng = rand::thread_rng();
|
52 |
-
let delay_secs = rng.gen_range(1, 10);
|
53 |
-
std::thread::sleep(Duration::from_secs(delay_secs));
|
54 |
-
|
55 |
// initializing HeaderMap and adding appropriate headers.
|
56 |
let mut header_map = HeaderMap::new();
|
57 |
header_map.insert(USER_AGENT, user_agent.parse()?);
|
|
|
2 |
//! by querying the upstream duckduckgo search engine with user provided query and with a page
|
3 |
//! number if provided.
|
4 |
|
5 |
+
use std::collections::HashMap;
|
6 |
|
|
|
7 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, REFERER, USER_AGENT};
|
8 |
use scraper::{Html, Selector};
|
9 |
|
|
|
46 |
}
|
47 |
};
|
48 |
|
|
|
|
|
|
|
|
|
|
|
49 |
// initializing HeaderMap and adding appropriate headers.
|
50 |
let mut header_map = HeaderMap::new();
|
51 |
header_map.insert(USER_AGENT, user_agent.parse()?);
|
src/engines/searx.rs
CHANGED
@@ -2,10 +2,9 @@
|
|
2 |
//! by querying the upstream searx search engine instance with user provided query and with a page
|
3 |
//! number if provided.
|
4 |
|
5 |
-
use rand::Rng;
|
6 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, REFERER, USER_AGENT};
|
7 |
use scraper::{Html, Selector};
|
8 |
-
use std::
|
9 |
|
10 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
11 |
|
@@ -34,11 +33,6 @@ pub async fn results(
|
|
34 |
// so that upstream server recieves valid page number.
|
35 |
let url: String = format!("https://searx.work/search?q={query}&pageno={page}");
|
36 |
|
37 |
-
// Add random delay before making the request.
|
38 |
-
let mut rng = rand::thread_rng();
|
39 |
-
let delay_secs = rng.gen_range(1, 10);
|
40 |
-
std::thread::sleep(Duration::from_secs(delay_secs));
|
41 |
-
|
42 |
// initializing headers and adding appropriate headers.
|
43 |
let mut header_map = HeaderMap::new();
|
44 |
header_map.insert(USER_AGENT, user_agent.parse()?);
|
|
|
2 |
//! by querying the upstream searx search engine instance with user provided query and with a page
|
3 |
//! number if provided.
|
4 |
|
|
|
5 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, REFERER, USER_AGENT};
|
6 |
use scraper::{Html, Selector};
|
7 |
+
use std::collections::HashMap;
|
8 |
|
9 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
10 |
|
|
|
33 |
// so that upstream server recieves valid page number.
|
34 |
let url: String = format!("https://searx.work/search?q={query}&pageno={page}");
|
35 |
|
|
|
|
|
|
|
|
|
|
|
36 |
// initializing headers and adding appropriate headers.
|
37 |
let mut header_map = HeaderMap::new();
|
38 |
header_map.insert(USER_AGENT, user_agent.parse()?);
|
src/search_results_handler/aggregator.rs
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
//! This module provides the functionality to scrape and gathers all the results from the upstream
|
2 |
//! search engines and then removes duplicate results.
|
3 |
|
4 |
-
use std::collections::HashMap;
|
|
|
|
|
|
|
5 |
|
6 |
use super::{
|
7 |
aggregation_models::{RawSearchResult, SearchResult, SearchResults},
|
@@ -14,14 +17,14 @@ use crate::engines::{duckduckgo, searx};
|
|
14 |
/// then removes duplicate results and if two results are found to be from two or more engines
|
15 |
/// then puts their names together to show the results are fetched from these upstream engines
|
16 |
/// and then removes all data from the HashMap and puts into a struct of all results aggregated
|
17 |
-
/// into a vector and also adds the query used into the struct this is neccessory because
|
18 |
/// otherwise the search bar in search remains empty if searched from the query url
|
19 |
///
|
20 |
/// # Example:
|
21 |
///
|
22 |
/// If you search from the url like `https://127.0.0.1/search?q=huston` then the search bar should
|
23 |
/// contain the word huston and not remain empty.
|
24 |
-
///
|
25 |
/// # Arguments
|
26 |
///
|
27 |
/// * `query` - Accepts a string to query with the above upstream search engines.
|
@@ -29,7 +32,7 @@ use crate::engines::{duckduckgo, searx};
|
|
29 |
///
|
30 |
/// # Error
|
31 |
///
|
32 |
-
/// Returns an error a reqwest and scraping selector errors if any error occurs in the results
|
33 |
/// function in either `searx` or `duckduckgo` or both otherwise returns a `SearchResults struct`
|
34 |
/// containing appropriate values.
|
35 |
pub async fn aggregate(
|
@@ -39,10 +42,19 @@ pub async fn aggregate(
|
|
39 |
let user_agent: String = random_user_agent();
|
40 |
let mut result_map: HashMap<String, RawSearchResult> = HashMap::new();
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
let
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
result_map.extend(ddg_map_results);
|
48 |
|
|
|
1 |
//! This module provides the functionality to scrape and gathers all the results from the upstream
|
2 |
//! search engines and then removes duplicate results.
|
3 |
|
4 |
+
use std::{collections::HashMap, time::Duration};
|
5 |
+
|
6 |
+
use rand::Rng;
|
7 |
+
use tokio::join;
|
8 |
|
9 |
use super::{
|
10 |
aggregation_models::{RawSearchResult, SearchResult, SearchResults},
|
|
|
17 |
/// then removes duplicate results and if two results are found to be from two or more engines
|
18 |
/// then puts their names together to show the results are fetched from these upstream engines
|
19 |
/// and then removes all data from the HashMap and puts into a struct of all results aggregated
|
20 |
+
/// into a vector and also adds the query used into the struct this is neccessory because
|
21 |
/// otherwise the search bar in search remains empty if searched from the query url
|
22 |
///
|
23 |
/// # Example:
|
24 |
///
|
25 |
/// If you search from the url like `https://127.0.0.1/search?q=huston` then the search bar should
|
26 |
/// contain the word huston and not remain empty.
|
27 |
+
///
|
28 |
/// # Arguments
|
29 |
///
|
30 |
/// * `query` - Accepts a string to query with the above upstream search engines.
|
|
|
32 |
///
|
33 |
/// # Error
|
34 |
///
|
35 |
+
/// Returns an error a reqwest and scraping selector errors if any error occurs in the results
|
36 |
/// function in either `searx` or `duckduckgo` or both otherwise returns a `SearchResults struct`
|
37 |
/// containing appropriate values.
|
38 |
pub async fn aggregate(
|
|
|
42 |
let user_agent: String = random_user_agent();
|
43 |
let mut result_map: HashMap<String, RawSearchResult> = HashMap::new();
|
44 |
|
45 |
+
// Add a random delay before making the request.
|
46 |
+
let mut rng = rand::thread_rng();
|
47 |
+
let delay_secs = rng.gen_range(1, 10);
|
48 |
+
std::thread::sleep(Duration::from_secs(delay_secs));
|
49 |
+
|
50 |
+
// fetch results from upstream search engines simultaneously/concurrently.
|
51 |
+
let (ddg_map_results, searx_map_results) = join!(
|
52 |
+
duckduckgo::results(query, page, &user_agent),
|
53 |
+
searx::results(query, page, &user_agent)
|
54 |
+
);
|
55 |
+
|
56 |
+
let ddg_map_results: HashMap<String, RawSearchResult> = ddg_map_results?;
|
57 |
+
let searx_map_results: HashMap<String, RawSearchResult> = searx_map_results?;
|
58 |
|
59 |
result_map.extend(ddg_map_results);
|
60 |
|