2023-04-27 16:06:59 +03:00
|
|
|
//! The `duckduckgo` module handles the scraping of results from the duckduckgo search engine
|
|
|
|
//! by querying the upstream duckduckgo search engine with user provided query and with a page
|
|
|
|
//! number if provided.
|
|
|
|
|
2023-07-14 17:16:13 +03:00
|
|
|
use std::collections::HashMap;
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-08-27 20:59:08 +03:00
|
|
|
use reqwest::header::HeaderMap;
|
2023-04-22 14:35:07 +03:00
|
|
|
use scraper::{Html, Selector};
|
|
|
|
|
2023-09-03 20:50:50 +03:00
|
|
|
use crate::models::aggregation_models::SearchResult;
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-09-03 20:50:50 +03:00
|
|
|
use crate::models::engine_models::{EngineError, SearchEngine};
|
2023-06-14 20:42:30 +08:00
|
|
|
|
2023-08-27 20:59:08 +03:00
|
|
|
use error_stack::{Report, Result, ResultExt};
|
2023-05-31 19:54:51 +03:00
|
|
|
|
2023-07-15 13:36:46 +03:00
|
|
|
/// A new DuckDuckGo engine type defined in-order to implement the `SearchEngine` trait which allows to
|
|
|
|
/// reduce code duplication as well as allows to create vector of different search engines easily.
|
2023-07-11 19:42:17 +03:00
|
|
|
pub struct DuckDuckGo;
|
2023-05-02 11:58:21 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl SearchEngine for DuckDuckGo {
|
2023-09-03 19:34:22 +03:00
|
|
|
async fn results(
|
2023-07-11 19:42:17 +03:00
|
|
|
&self,
|
2023-08-27 20:59:08 +03:00
|
|
|
query: &str,
|
2023-07-11 19:42:17 +03:00
|
|
|
page: u32,
|
2023-08-27 20:59:08 +03:00
|
|
|
user_agent: &str,
|
2023-07-30 10:53:48 +03:00
|
|
|
request_timeout: u8,
|
2023-09-02 17:44:05 +03:00
|
|
|
_safe_search: u8,
|
2023-08-18 10:43:53 +02:00
|
|
|
) -> Result<HashMap<String, SearchResult>, EngineError> {
|
2023-07-11 19:42:17 +03:00
|
|
|
// Page number can be missing or empty string and so appropriate handling is required
|
|
|
|
// so that upstream server recieves valid page number.
|
|
|
|
let url: String = match page {
|
2023-07-30 20:14:40 +03:00
|
|
|
1 | 0 => {
|
2023-07-11 19:42:17 +03:00
|
|
|
format!("https://html.duckduckgo.com/html/?q={query}&s=&dc=&v=1&o=json&api=/d.js")
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
format!(
|
|
|
|
"https://duckduckgo.com/html/?q={}&s={}&dc={}&v=1&o=json&api=/d.js",
|
|
|
|
query,
|
|
|
|
(page / 2 + (page % 2)) * 30,
|
|
|
|
(page / 2 + (page % 2)) * 30 + 1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
// initializing HeaderMap and adding appropriate headers.
|
2023-08-27 20:59:08 +03:00
|
|
|
let header_map = HeaderMap::try_from(&HashMap::from([
|
|
|
|
("USER_AGENT".to_string(), user_agent.to_string()),
|
|
|
|
("REFERER".to_string(), "https://google.com/".to_string()),
|
|
|
|
(
|
|
|
|
"CONTENT_TYPE".to_string(),
|
|
|
|
"application/x-www-form-urlencoded".to_string(),
|
|
|
|
),
|
|
|
|
("COOKIE".to_string(), "kl=wt-wt".to_string()),
|
|
|
|
]))
|
|
|
|
.change_context(EngineError::UnexpectedError)?;
|
2023-05-31 19:54:51 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
let document: Html = Html::parse_document(
|
2023-08-27 20:59:08 +03:00
|
|
|
&DuckDuckGo::fetch_html_from_upstream(self, &url, header_map, request_timeout).await?,
|
2023-07-11 19:42:17 +03:00
|
|
|
);
|
2023-05-31 19:54:51 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
let no_result: Selector = Selector::parse(".no-results")
|
|
|
|
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
|
.attach_printable_lazy(|| format!("invalid CSS selector: {}", ".no-results"))?;
|
2023-05-31 19:54:51 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
if document.select(&no_result).next().is_some() {
|
|
|
|
return Err(Report::new(EngineError::EmptyResultSet));
|
|
|
|
}
|
2023-04-22 14:35:07 +03:00
|
|
|
|
2023-07-11 19:42:17 +03:00
|
|
|
let results: Selector = Selector::parse(".result")
|
|
|
|
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
|
.attach_printable_lazy(|| format!("invalid CSS selector: {}", ".result"))?;
|
|
|
|
let result_title: Selector = Selector::parse(".result__a")
|
|
|
|
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
|
.attach_printable_lazy(|| format!("invalid CSS selector: {}", ".result__a"))?;
|
|
|
|
let result_url: Selector = Selector::parse(".result__url")
|
|
|
|
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
|
.attach_printable_lazy(|| format!("invalid CSS selector: {}", ".result__url"))?;
|
|
|
|
let result_desc: Selector = Selector::parse(".result__snippet")
|
|
|
|
.map_err(|_| Report::new(EngineError::UnexpectedError))
|
|
|
|
.attach_printable_lazy(|| format!("invalid CSS selector: {}", ".result__snippet"))?;
|
|
|
|
|
|
|
|
// scrape all the results from the html
|
|
|
|
Ok(document
|
|
|
|
.select(&results)
|
|
|
|
.map(|result| {
|
2023-08-18 10:43:53 +02:00
|
|
|
SearchResult::new(
|
2023-04-25 16:30:04 +03:00
|
|
|
result
|
2023-07-11 19:42:17 +03:00
|
|
|
.select(&result_title)
|
2023-04-25 16:30:04 +03:00
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.inner_html()
|
2023-08-27 20:59:08 +03:00
|
|
|
.trim(),
|
2023-07-11 19:42:17 +03:00
|
|
|
format!(
|
|
|
|
"https://{}",
|
|
|
|
result
|
|
|
|
.select(&result_url)
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.inner_html()
|
|
|
|
.trim()
|
2023-08-27 20:59:08 +03:00
|
|
|
)
|
|
|
|
.as_str(),
|
2023-07-11 19:42:17 +03:00
|
|
|
result
|
|
|
|
.select(&result_desc)
|
|
|
|
.next()
|
|
|
|
.unwrap()
|
|
|
|
.inner_html()
|
2023-08-27 20:59:08 +03:00
|
|
|
.trim(),
|
|
|
|
&["duckduckgo"],
|
2023-07-11 19:42:17 +03:00
|
|
|
)
|
|
|
|
})
|
2023-08-17 22:48:20 +02:00
|
|
|
.map(|search_result| (search_result.url.clone(), search_result))
|
2023-07-11 19:42:17 +03:00
|
|
|
.collect())
|
|
|
|
}
|
2023-04-22 14:35:07 +03:00
|
|
|
}
|