-
Notifications
You must be signed in to change notification settings - Fork 101
✨ Implement qwant
search engine support
#605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
nrabulinski
wants to merge
7
commits into
neon-mmd:rolling
Choose a base branch
from
nrabulinski:qwant
base: rolling
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fb0c2db
Enable json feature of reqwest
nrabulinski 709425f
Implement Qwant search engine
nrabulinski afefd02
engines/qwant: Parse url instead of using format
nrabulinski 8323f49
Merge branch 'rolling' into qwant
mergify[bot] 4857dcb
Merge branch 'rolling' into qwant
mergify[bot] b3dfb7f
Merge branch 'rolling' into qwant
neon-mmd ec4364c
Merge branch 'rolling' into qwant
neon-mmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
//! The `qwant` module handles the scraping of results from the qwant search engine | ||
//! by querying the upstream qwant search engine with user provided query and with a page | ||
//! number if provided. | ||
|
||
use std::borrow::Cow; | ||
use std::collections::HashMap; | ||
|
||
use reqwest::header::HeaderMap; | ||
use reqwest::{Client, Url}; | ||
use serde::Deserialize; | ||
|
||
use crate::models::aggregation_models::SearchResult; | ||
|
||
use crate::models::engine_models::{EngineError, SearchEngine}; | ||
|
||
use error_stack::{Report, Result, ResultExt}; | ||
|
||
/// A new Qwant 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. | ||
pub struct Qwant; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
/// Web page search result | ||
struct QwantSearchResult { | ||
// NOTE: This object also contains `favicon`, `url_ping_suffix`, `thumbnail_url`, | ||
// `source`, and `is_family_friendly` attributes, | ||
// which we currently don't care about. | ||
/// Title of the result | ||
title: String, | ||
/// Url of the result | ||
url: String, | ||
/// Description of the result | ||
desc: String, | ||
} | ||
|
||
impl From<&QwantSearchResult> for SearchResult { | ||
fn from(value: &QwantSearchResult) -> Self { | ||
SearchResult::new(&value.title, &value.url, &value.desc, &["qwant"]) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "snake_case")] | ||
#[serde(tag = "type")] | ||
/// A result which should be shown to the user | ||
enum QwantItem { | ||
/// Results containing web pages relevant to the query | ||
Web { | ||
// NOTE: This object also contains `count` and `serpContextId` attributes, | ||
// which we currently don't care about. | ||
/// List of web page search results | ||
items: Vec<QwantSearchResult>, | ||
}, | ||
#[serde(other)] | ||
/// Other item type like "related_searches", which aren't relevant. | ||
Other, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct QwantItems { | ||
// NOTE: This object also contains `headline`, `sidebar`, and `bottomline` attributes, | ||
// which we currently don't care about. | ||
/// Results which should be shown in the main section of the page | ||
mainline: Vec<QwantItem>, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct QwantResult { | ||
// NOTE: This object also contains `denied`, `total`, `items`, `filters`, `lastPage`, | ||
// `instrumentation`, `onlyProductAds`, and `topClassification` attributes, | ||
// which we currently don't care about. | ||
/// Entries that should be shown to the user | ||
items: QwantItems, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(rename_all = "snake_case")] | ||
#[serde(tag = "status", content = "data")] | ||
enum QwantApiResponse { | ||
/// Success response | ||
Success { | ||
// NOTE: This object also contains `query` and `cache` attributes, | ||
// which we currently don't care about. | ||
/// Actual results the search produced | ||
result: QwantResult, | ||
}, | ||
// TODO: Use the reported error messages | ||
#[allow(unused)] | ||
/// Error response | ||
Error { | ||
/// Machine-readable error code | ||
error_code: i32, | ||
#[serde(default)] | ||
/// List of human-readable error messages | ||
message: Vec<String>, | ||
}, | ||
} | ||
|
||
impl From<QwantApiResponse> for Result<QwantResult, EngineError> { | ||
fn from(value: QwantApiResponse) -> Self { | ||
match value { | ||
QwantApiResponse::Success { result } => Ok(result), | ||
QwantApiResponse::Error { .. } => Err(Report::new(EngineError::RequestError)), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl SearchEngine for Qwant { | ||
async fn results( | ||
&self, | ||
query: &str, | ||
page: u32, | ||
user_agent: &str, | ||
client: &Client, | ||
safe_search: u8, | ||
) -> Result<Vec<(String, SearchResult)>, EngineError> { | ||
let results_per_page = 10; | ||
let start_result = results_per_page * page; | ||
|
||
let url = Url::parse_with_params( | ||
"https://api.qwant.com/v3/search/web", | ||
&[ | ||
("q", Cow::from(query)), | ||
("count", results_per_page.to_string().into()), | ||
("locale", "en_US".into()), | ||
("offset", start_result.to_string().into()), | ||
("safesearch", safe_search.to_string().into()), | ||
("device", "desktop".into()), | ||
("tgb", "2".into()), | ||
("displayed", "true".into()), | ||
], | ||
) | ||
.change_context(EngineError::UnexpectedError)?; | ||
|
||
let header_map = HeaderMap::try_from(&HashMap::from([ | ||
("User-Agent".to_string(), user_agent.to_string()), | ||
("Referer".to_string(), "https://qwant.com/".to_string()), | ||
("Origin".to_string(), "https://qwant.com".to_string()), | ||
])) | ||
.change_context(EngineError::UnexpectedError)?; | ||
|
||
let result: QwantApiResponse = client | ||
.get(url) | ||
.headers(header_map) | ||
.send() | ||
.await | ||
.change_context(EngineError::RequestError)? | ||
.json() | ||
.await | ||
.change_context(EngineError::RequestError)?; | ||
|
||
let result = Result::from(result)?; | ||
|
||
let results: Vec<_> = result | ||
.items | ||
.mainline | ||
.into_iter() | ||
.filter_map(|item| match item { | ||
QwantItem::Web { items } => Some(items), | ||
_ => None, | ||
}) | ||
.flatten() | ||
.map(|result| { | ||
let search_result = SearchResult::from(&result); | ||
(result.url, search_result) | ||
}) | ||
.collect(); | ||
|
||
if results.is_empty() { | ||
Err(Report::new(EngineError::EmptyResultSet)) | ||
} else { | ||
Ok(results) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you could have used the already available
json_as_bytes_from_upstream
function to fetch data and deserialize it using something likebincode
. What do you think? 🙂 .