From 35b7b0a19def70b8fd03be4b9969e9bcf20b41bb Mon Sep 17 00:00:00 2001 From: mat Date: Tue, 2 Jan 2024 22:33:13 -0600 Subject: [PATCH] don't always search with marginalia --- src/engines/mod.rs | 8 +++++++- src/engines/search/marginalia.rs | 12 ++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 0338763..689457b 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -306,7 +306,13 @@ pub async fn search_with_engines( start_time, ))?; - let response = engine.parse_response(&body)?; + let response = match engine.parse_response(&body) { + Ok(response) => response, + Err(e) => { + eprintln!("parse error: {}", e); + EngineResponse::new() + } + }; progress_tx.send(ProgressUpdate::new( ProgressUpdateData::Engine { diff --git a/src/engines/search/marginalia.rs b/src/engines/search/marginalia.rs index cc8f960..22acba0 100644 --- a/src/engines/search/marginalia.rs +++ b/src/engines/search/marginalia.rs @@ -1,11 +1,18 @@ use reqwest::Url; use crate::{ - engines::{EngineResponse, CLIENT}, + engines::{EngineResponse, RequestResponse, CLIENT}, parse::{parse_html_response_with_opts, ParseOpts}, }; -pub fn request(query: &str) -> reqwest::RequestBuilder { +pub fn request(query: &str) -> RequestResponse { + // if the query is more than 3 words or has any special characters then abort + if query.split_whitespace().count() > 3 + || !query.chars().all(|c| c.is_ascii_alphanumeric() || c == ' ') + { + return RequestResponse::None; + } + CLIENT .get( Url::parse_with_params( @@ -24,6 +31,7 @@ pub fn request(query: &str) -> reqwest::RequestBuilder { "Mozilla/5.0 (X11; Linux x86_64; rv:121.0) Gecko/20100101 Firefox/121.0", ) .header("Accept-Language", "en-US,en;q=0.5") + .into() } pub fn parse_response(body: &str) -> eyre::Result {