i am going insane
🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈🐈
This commit is contained in:
Shrecknt 2024-04-16 23:35:24 -07:00 committed by GitHub
parent d1fc33b1fc
commit 5d0d47df9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 56 additions and 42 deletions

View File

@ -2,6 +2,7 @@
name = "metasearch2"
version = "0.1.0"
edition = "2021"
build = "src/build.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,6 +1,7 @@
bind = "0.0.0.0:28019"
engine_list_separator = false
version_info = false
[engines]
google = { weight = 1.05 }

19
src/build.rs Normal file
View File

@ -0,0 +1,19 @@
use std::process::Command;
fn main() {
println!("cargo:rustc-rerun-if-changed=.git/HEAD");
let output = Command::new("git").args(["rev-parse", "HEAD"]).output();
let git_hash = match output {
Ok(output) => String::from_utf8(output.stdout).unwrap_or("unknown".into()),
Err(_) => "unknown".into(),
};
let output = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output();
let git_hash_short = match output {
Ok(output) => String::from_utf8(output.stdout).unwrap_or("unknown".into()),
Err(_) => "unknown".into(),
};
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=GIT_HASH_SHORT={}", git_hash_short);
}

View File

@ -11,6 +11,8 @@ pub struct Config {
pub bind: SocketAddr,
#[serde(default)]
pub engine_list_separator: Option<bool>,
#[serde(default)]
pub version_info: Option<bool>,
pub engines: EnginesConfig,
}
@ -19,7 +21,8 @@ impl Config {
let default_config_str = include_str!("../default-config.toml");
let mut config: Config = toml::from_str(default_config_str)?;
let config_path = Path::new("config.toml");
let config_path = std::env::args().nth(1).unwrap_or("config.toml".into());
let config_path = Path::new(&config_path);
if config_path.exists() {
let given_config = toml::from_str::<Config>(&fs::read_to_string(config_path)?)?;
config.update(given_config);
@ -38,6 +41,8 @@ impl Config {
self.bind = new.bind;
self.engine_list_separator = new.engine_list_separator.or(self.engine_list_separator);
assert_ne!(self.engine_list_separator, None);
self.version_info = new.version_info.or(self.version_info);
assert_ne!(self.version_info, None);
for (key, new) in new.engines.map {
if let Some(existing) = self.engines.map.get_mut(&key) {
existing.update(new);

View File

@ -1,4 +1,4 @@
use scraper::{ElementRef, Html, Selector};
use scraper::{Html, Selector};
use crate::engines::{HttpResponse, Response, CLIENT};
@ -25,14 +25,13 @@ pub fn parse_response(HttpResponse { res, body, .. }: &HttpResponse) -> Option<S
.trim()
.to_string();
let doc_query = Selector::parse(".mw-parser-output").unwrap();
let doc_query = Selector::parse(".mw-parser-output > p").unwrap();
let doc_html = dom
.select(&doc_query)
.next()
.map(|doc| strip_gallery(doc))
.unwrap_or_default()
.join("");
.map(|doc| doc.html())
.unwrap_or_default();
let doc_html = ammonia::Builder::default()
.link_rel(None)
@ -52,37 +51,3 @@ pub fn parse_response(HttpResponse { res, body, .. }: &HttpResponse) -> Option<S
r#"{title_html}<div class="infobox-minecraft_wiki-article">{doc_html}</div>"#
))
}
fn strip_gallery(doc: ElementRef) -> Vec<String> {
let mut gallery = false;
doc.children()
.filter(|elem| {
let value = elem.value();
if gallery {
return false;
}
match value {
scraper::Node::Element(_) => {
let elem = ElementRef::wrap(*elem).unwrap();
let is_gallery_title = elem.first_child().map_or(false, |elem| {
elem.value().as_element().map_or(false, |_| {
let elem = ElementRef::wrap(elem).unwrap();
elem.text().collect::<String>() == "Gallery"
})
});
if is_gallery_title {
gallery = true;
return false;
}
true
}
_ => true,
}
})
.map(|elem| {
ElementRef::wrap(elem)
.map(|elem| elem.html())
.unwrap_or_default()
})
.collect()
}

View File

@ -30,5 +30,6 @@
<input type="submit" value="Search" />
</form>
</div>
%version-info%
</body>
</html>

View File

@ -40,6 +40,11 @@ body {
line-height: 1.2;
height: 100%;
}
.version-info {
position: absolute;
bottom: 16px;
right: 16px;
}
.results-container {
/* enough space for the infobox */
max-width: 73.5rem;

View File

@ -9,16 +9,33 @@ use tracing::info;
use crate::config::Config;
const BASE_COMMIT_URL: &str = "https://github.com/mat-1/metasearch2/commit/";
const VERSION: &str = std::env!("CARGO_PKG_VERSION");
const COMMIT_HASH: &str = std::env!("GIT_HASH");
const COMMIT_HASH_SHORT: &str = std::env!("GIT_HASH_SHORT");
pub async fn run(config: Config) {
let bind_addr = config.bind;
let version_info = if config.version_info.unwrap() {
if COMMIT_HASH == "unknown" || COMMIT_HASH_SHORT == "unknown" {
format!(r#"<span class="version-info">Version {VERSION} (unknown commit)</span>"#)
} else {
format!(
r#"<span class="version-info">Version {VERSION} (<a href="{BASE_COMMIT_URL}{COMMIT_HASH}">{COMMIT_HASH_SHORT}</a>)</span>"#
)
}
} else {
String::new()
};
let app = Router::new()
.route(
"/",
get(|| async {
get(|| async move {
(
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
include_str!("assets/index.html"),
include_str!("assets/index.html").replace("%version-info%", &version_info),
)
}),
)