Spaces:
Runtime error
Runtime error
neon_arch
commited on
Commit
•
2d47e8d
1
Parent(s):
6d3396b
✨ feat: provide the functionality to use the new config option
Browse files- src/engines/duckduckgo.rs +2 -1
- src/engines/engine_models.rs +3 -1
- src/engines/searx.rs +5 -3
- src/results/aggregator.rs +6 -3
- src/server/routes.rs +2 -0
src/engines/duckduckgo.rs
CHANGED
@@ -41,6 +41,7 @@ impl SearchEngine for DuckDuckGo {
|
|
41 |
query: String,
|
42 |
page: u32,
|
43 |
user_agent: String,
|
|
|
44 |
) -> Result<HashMap<String, RawSearchResult>, EngineError> {
|
45 |
// Page number can be missing or empty string and so appropriate handling is required
|
46 |
// so that upstream server recieves valid page number.
|
@@ -90,7 +91,7 @@ impl SearchEngine for DuckDuckGo {
|
|
90 |
);
|
91 |
|
92 |
let document: Html = Html::parse_document(
|
93 |
-
&DuckDuckGo::fetch_html_from_upstream(self, url, header_map).await?,
|
94 |
);
|
95 |
|
96 |
let no_result: Selector = Selector::parse(".no-results")
|
|
|
41 |
query: String,
|
42 |
page: u32,
|
43 |
user_agent: String,
|
44 |
+
request_timeout: u8,
|
45 |
) -> Result<HashMap<String, RawSearchResult>, EngineError> {
|
46 |
// Page number can be missing or empty string and so appropriate handling is required
|
47 |
// so that upstream server recieves valid page number.
|
|
|
91 |
);
|
92 |
|
93 |
let document: Html = Html::parse_document(
|
94 |
+
&DuckDuckGo::fetch_html_from_upstream(self, url, header_map, request_timeout).await?,
|
95 |
);
|
96 |
|
97 |
let no_result: Selector = Selector::parse(".no-results")
|
src/engines/engine_models.rs
CHANGED
@@ -50,11 +50,12 @@ pub trait SearchEngine {
|
|
50 |
&self,
|
51 |
url: String,
|
52 |
header_map: reqwest::header::HeaderMap,
|
|
|
53 |
) -> Result<String, EngineError> {
|
54 |
// fetch the html from upstream search engine
|
55 |
Ok(reqwest::Client::new()
|
56 |
.get(url)
|
57 |
-
.timeout(Duration::from_secs(
|
58 |
.headers(header_map) // add spoofed headers to emulate human behaviour
|
59 |
.send()
|
60 |
.await
|
@@ -71,5 +72,6 @@ pub trait SearchEngine {
|
|
71 |
query: String,
|
72 |
page: u32,
|
73 |
user_agent: String,
|
|
|
74 |
) -> Result<HashMap<String, RawSearchResult>, EngineError>;
|
75 |
}
|
|
|
50 |
&self,
|
51 |
url: String,
|
52 |
header_map: reqwest::header::HeaderMap,
|
53 |
+
request_timeout: u8,
|
54 |
) -> Result<String, EngineError> {
|
55 |
// fetch the html from upstream search engine
|
56 |
Ok(reqwest::Client::new()
|
57 |
.get(url)
|
58 |
+
.timeout(Duration::from_secs(request_timeout as u64)) // Add timeout to request to avoid DDOSing the server
|
59 |
.headers(header_map) // add spoofed headers to emulate human behaviour
|
60 |
.send()
|
61 |
.await
|
|
|
72 |
query: String,
|
73 |
page: u32,
|
74 |
user_agent: String,
|
75 |
+
request_timeout: u8,
|
76 |
) -> Result<HashMap<String, RawSearchResult>, EngineError>;
|
77 |
}
|
src/engines/searx.rs
CHANGED
@@ -4,7 +4,7 @@
|
|
4 |
|
5 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, COOKIE, REFERER, USER_AGENT};
|
6 |
use scraper::{Html, Selector};
|
7 |
-
use std::collections::HashMap;
|
8 |
|
9 |
use crate::results::aggregation_models::RawSearchResult;
|
10 |
|
@@ -40,6 +40,7 @@ impl SearchEngine for Searx {
|
|
40 |
query: String,
|
41 |
page: u32,
|
42 |
user_agent: String,
|
|
|
43 |
) -> Result<HashMap<String, RawSearchResult>, EngineError> {
|
44 |
// Page number can be missing or empty string and so appropriate handling is required
|
45 |
// so that upstream server recieves valid page number.
|
@@ -70,8 +71,9 @@ impl SearchEngine for Searx {
|
|
70 |
);
|
71 |
header_map.insert(COOKIE, "categories=general; language=auto; locale=en; autocomplete=duckduckgo; image_proxy=1; method=POST; safesearch=2; theme=simple; results_on_new_tab=1; doi_resolver=oadoi.org; simple_style=auto; center_alignment=1; query_in_title=1; infinite_scroll=0; disabled_engines=; enabled_engines=\"archive is__general\\054yep__general\\054curlie__general\\054currency__general\\054ddg definitions__general\\054wikidata__general\\054duckduckgo__general\\054tineye__general\\054lingva__general\\054startpage__general\\054yahoo__general\\054wiby__general\\054marginalia__general\\054alexandria__general\\054wikibooks__general\\054wikiquote__general\\054wikisource__general\\054wikiversity__general\\054wikivoyage__general\\054dictzone__general\\054seznam__general\\054mojeek__general\\054naver__general\\054wikimini__general\\054brave__general\\054petalsearch__general\\054goo__general\"; disabled_plugins=; enabled_plugins=\"searx.plugins.hostname_replace\\054searx.plugins.oa_doi_rewrite\\054searx.plugins.vim_hotkeys\"; tokens=; maintab=on; enginetab=on".parse().into_report().change_context(EngineError::UnexpectedError)?);
|
72 |
|
73 |
-
let document: Html =
|
74 |
-
|
|
|
75 |
|
76 |
let no_result: Selector = Selector::parse("#urls>.dialog-error>p")
|
77 |
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
4 |
|
5 |
use reqwest::header::{HeaderMap, CONTENT_TYPE, COOKIE, REFERER, USER_AGENT};
|
6 |
use scraper::{Html, Selector};
|
7 |
+
use std::{collections::HashMap};
|
8 |
|
9 |
use crate::results::aggregation_models::RawSearchResult;
|
10 |
|
|
|
40 |
query: String,
|
41 |
page: u32,
|
42 |
user_agent: String,
|
43 |
+
request_timeout: u8,
|
44 |
) -> Result<HashMap<String, RawSearchResult>, EngineError> {
|
45 |
// Page number can be missing or empty string and so appropriate handling is required
|
46 |
// so that upstream server recieves valid page number.
|
|
|
71 |
);
|
72 |
header_map.insert(COOKIE, "categories=general; language=auto; locale=en; autocomplete=duckduckgo; image_proxy=1; method=POST; safesearch=2; theme=simple; results_on_new_tab=1; doi_resolver=oadoi.org; simple_style=auto; center_alignment=1; query_in_title=1; infinite_scroll=0; disabled_engines=; enabled_engines=\"archive is__general\\054yep__general\\054curlie__general\\054currency__general\\054ddg definitions__general\\054wikidata__general\\054duckduckgo__general\\054tineye__general\\054lingva__general\\054startpage__general\\054yahoo__general\\054wiby__general\\054marginalia__general\\054alexandria__general\\054wikibooks__general\\054wikiquote__general\\054wikisource__general\\054wikiversity__general\\054wikivoyage__general\\054dictzone__general\\054seznam__general\\054mojeek__general\\054naver__general\\054wikimini__general\\054brave__general\\054petalsearch__general\\054goo__general\"; disabled_plugins=; enabled_plugins=\"searx.plugins.hostname_replace\\054searx.plugins.oa_doi_rewrite\\054searx.plugins.vim_hotkeys\"; tokens=; maintab=on; enginetab=on".parse().into_report().change_context(EngineError::UnexpectedError)?);
|
73 |
|
74 |
+
let document: Html = Html::parse_document(
|
75 |
+
&Searx::fetch_html_from_upstream(self, url, header_map, request_timeout).await?,
|
76 |
+
);
|
77 |
|
78 |
let no_result: Selector = Selector::parse("#urls>.dialog-error>p")
|
79 |
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
src/results/aggregator.rs
CHANGED
@@ -64,6 +64,7 @@ pub async fn aggregate(
|
|
64 |
random_delay: bool,
|
65 |
debug: bool,
|
66 |
upstream_search_engines: Vec<String>,
|
|
|
67 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
68 |
let user_agent: String = random_user_agent();
|
69 |
let mut result_map: HashMap<String, RawSearchResult> = HashMap::new();
|
@@ -92,9 +93,11 @@ pub async fn aggregate(
|
|
92 |
.map(|search_engine| {
|
93 |
let query: String = query.clone();
|
94 |
let user_agent: String = user_agent.clone();
|
95 |
-
tokio::spawn(
|
96 |
-
|
97 |
-
|
|
|
|
|
98 |
})
|
99 |
.collect();
|
100 |
|
|
|
64 |
random_delay: bool,
|
65 |
debug: bool,
|
66 |
upstream_search_engines: Vec<String>,
|
67 |
+
request_timeout: u8,
|
68 |
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
69 |
let user_agent: String = random_user_agent();
|
70 |
let mut result_map: HashMap<String, RawSearchResult> = HashMap::new();
|
|
|
93 |
.map(|search_engine| {
|
94 |
let query: String = query.clone();
|
95 |
let user_agent: String = user_agent.clone();
|
96 |
+
tokio::spawn(async move {
|
97 |
+
search_engine
|
98 |
+
.results(query, page, user_agent.clone(), request_timeout)
|
99 |
+
.await
|
100 |
+
})
|
101 |
})
|
102 |
.collect();
|
103 |
|
src/server/routes.rs
CHANGED
@@ -146,6 +146,7 @@ async fn results(
|
|
146 |
config.aggregator.random_delay,
|
147 |
config.debug,
|
148 |
cookie_value.engines,
|
|
|
149 |
)
|
150 |
.await?
|
151 |
}
|
@@ -156,6 +157,7 @@ async fn results(
|
|
156 |
config.aggregator.random_delay,
|
157 |
config.debug,
|
158 |
config.upstream_search_engines.clone(),
|
|
|
159 |
)
|
160 |
.await?
|
161 |
}
|
|
|
146 |
config.aggregator.random_delay,
|
147 |
config.debug,
|
148 |
cookie_value.engines,
|
149 |
+
config.request_timeout,
|
150 |
)
|
151 |
.await?
|
152 |
}
|
|
|
157 |
config.aggregator.random_delay,
|
158 |
config.debug,
|
159 |
config.upstream_search_engines.clone(),
|
160 |
+
config.request_timeout,
|
161 |
)
|
162 |
.await?
|
163 |
}
|