0
0
mirror of https://github.com/neon-mmd/websurfx.git synced 2024-11-22 05:58:21 -05:00

feat: implement code to fetch results based on parameters, cookies & config (#210)

This commit is contained in:
neon_arch 2023-09-22 19:54:26 +03:00
parent d2e482251e
commit 72fec47892

View File

@ -67,61 +67,48 @@ pub async fn search(
None => 1, None => 1,
}; };
let safe_search: u8 = match config.safe_search {
3..=4 => config.safe_search,
_ => match &params.safesearch {
Some(safesearch) => match safesearch {
0..=2 => *safesearch,
_ => 1,
},
None => config.safe_search,
},
};
let (_, results, _) = join!( let (_, results, _) = join!(
results( results(
format!( format!(
"http://{}:{}/search?q={}&page={}&safesearch={}", "http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip, config.binding_ip,
config.port, config.port,
query, query,
page - 1, page - 1,
safe_search
), ),
&config, &config,
&cache, &cache,
query, query,
page - 1, page - 1,
req.clone(), req.clone(),
safe_search &params.safesearch
), ),
results( results(
format!( format!(
"http://{}:{}/search?q={}&page={}&safesearch={}", "http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip, config.port, query, page, safe_search config.binding_ip, config.port, query, page
), ),
&config, &config,
&cache, &cache,
query, query,
page, page,
req.clone(), req.clone(),
safe_search &params.safesearch
), ),
results( results(
format!( format!(
"http://{}:{}/search?q={}&page={}&safesearch={}", "http://{}:{}/search?q={}&page={}&safesearch=",
config.binding_ip, config.binding_ip,
config.port, config.port,
query, query,
page + 1, page + 1,
safe_search
), ),
&config, &config,
&cache, &cache,
query, query,
page + 1, page + 1,
req.clone(), req.clone(),
safe_search &params.safesearch
) )
); );
@ -156,7 +143,7 @@ async fn results(
query: &str, query: &str,
page: u32, page: u32,
req: HttpRequest, req: HttpRequest,
safe_search: u8, safe_search: &Option<u8>,
) -> Result<SearchResults, Box<dyn std::error::Error>> { ) -> Result<SearchResults, Box<dyn std::error::Error>> {
// fetch the cached results json. // fetch the cached results json.
let cached_results = cache.cached_json(&url).await; let cached_results = cache.cached_json(&url).await;
@ -165,7 +152,18 @@ async fn results(
match cached_results { match cached_results {
Ok(results) => Ok(results), Ok(results) => Ok(results),
Err(_) => { Err(_) => {
if safe_search == 4 { let mut safe_search_level: u8 = match config.safe_search {
3..=4 => config.safe_search,
_ => match safe_search {
Some(safesearch) => match safesearch {
0..=2 => *safesearch,
_ => config.safe_search,
},
None => config.safe_search,
},
};
if safe_search_level == 4 {
let mut results: SearchResults = SearchResults::default(); let mut results: SearchResults = SearchResults::default();
let mut _flag: bool = let mut _flag: bool =
is_match_from_filter_list(file_path(FileType::BlockList)?, query)?; is_match_from_filter_list(file_path(FileType::BlockList)?, query)?;
@ -176,6 +174,7 @@ async fn results(
results.add_style(&config.style); results.add_style(&config.style);
results.set_page_query(query); results.set_page_query(query);
cache.cache_results(&results, &url).await?; cache.cache_results(&results, &url).await?;
results.set_safe_search_level(safe_search_level);
return Ok(results); return Ok(results);
} }
} }
@ -195,6 +194,17 @@ async fn results(
.filter_map(|name| EngineHandler::new(name)) .filter_map(|name| EngineHandler::new(name))
.collect(); .collect();
safe_search_level = match config.safe_search {
3..=4 => config.safe_search,
_ => match safe_search {
Some(safesearch) => match safesearch {
0..=2 => *safesearch,
_ => config.safe_search,
},
None => cookie_value.safe_search_level,
},
};
aggregate( aggregate(
query, query,
page, page,
@ -202,7 +212,7 @@ async fn results(
config.debug, config.debug,
&engines, &engines,
config.request_timeout, config.request_timeout,
safe_search, safe_search_level,
) )
.await? .await?
} }
@ -214,7 +224,7 @@ async fn results(
config.debug, config.debug,
&config.upstream_search_engines, &config.upstream_search_engines,
config.request_timeout, config.request_timeout,
safe_search, safe_search_level,
) )
.await? .await?
} }
@ -223,7 +233,10 @@ async fn results(
results.set_filtered(); results.set_filtered();
} }
results.add_style(&config.style); results.add_style(&config.style);
cache.cache_results(&results, &url).await?; cache
.cache_results(&results, &(format!("{url}{safe_search_level}")))
.await?;
results.set_safe_search_level(safe_search_level);
Ok(results) Ok(results)
} }
} }