Skip to content

Commit

Permalink
engines/qwant: Parse url instead of using format
Browse files Browse the repository at this point in the history
This makes sure that if a user uses & or any other symbol with
special meaning their query won't get broken
  • Loading branch information
nrabulinski committed Sep 7, 2024
1 parent 709425f commit afefd02
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/engines/qwant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
//! 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;
use reqwest::{Client, Url};
use serde::Deserialize;

use crate::models::aggregation_models::SearchResult;
Expand Down Expand Up @@ -118,7 +119,20 @@ impl SearchEngine for Qwant {
let results_per_page = 10;
let start_result = results_per_page * page;

let url: String = format!("https://api.qwant.com/v3/search/web?q={query}&count={results_per_page}&locale=en_US&offset={start_result}&safesearch={safe_search}&device=desktop&tgp=2&displayed=true");
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()),
Expand Down

0 comments on commit afefd02

Please sign in to comment.