added async support with tokio, modularised lua and removed the test functions from backend

This commit is contained in:
2026-11-24 13:53:38 +00:00
parent e6e93fc38f
commit eefb89c51d
8 changed files with 254 additions and 236 deletions

View File

@@ -1,6 +1,6 @@
use curl::easy::Easy;
pub use scraper;
use scraper::{ElementRef, Html, Selector};
use scraper::Html;
#[derive(Debug)]
pub struct SearchResult {
@@ -15,84 +15,27 @@ pub struct SiteHtmlFilter {
pub url_selector: String,
}
fn return_results_vec() -> Vec<SearchResult> {
let url = "https://lite.duckduckgo.com/lite?q=test";
let returned_html = scrape_url(url);
let html_tree = get_sanitised_html_graph(&returned_html);
let ddg: SiteHtmlFilter = SiteHtmlFilter {
title_selector: "tbody tr td a.result-link".to_string(),
snippet_selector: "tbody tr td.result-snippet".to_string(),
url_selector: "tbody tr td a.result-link".to_string(),
};
let filtered_results = filter_html(&html_tree, &ddg);
println!("{:#?}", filtered_results);
return filtered_results;
}
pub fn filter_html(html_tree: &Html, filter_set: &SiteHtmlFilter) -> Vec<SearchResult> {
let mut found_results: Vec<SearchResult> = Vec::new();
let title_selector = Selector::parse(&filter_set.title_selector).unwrap();
let snippet_selector = Selector::parse(&filter_set.snippet_selector).unwrap();
let url_selector = Selector::parse(&filter_set.url_selector).unwrap();
let titles: Vec<_> = html_tree.select(&title_selector).collect();
let snippets: Vec<_> = html_tree.select(&snippet_selector).collect();
let urls: Vec<_> = html_tree.select(&url_selector).collect();
for (index, title) in titles.iter().enumerate() {
let element_to_string =
|element: ElementRef<'_>| -> String { element.text().collect::<Vec<_>>().join(" ") };
let snippet = snippets
.get(index)
.map(|element| element_to_string(*element))
.unwrap_or_default();
let link = urls
.get(index)
.and_then(|element| element.value().attr("href"))
.unwrap_or_default()
.to_string();
let new_result = SearchResult {
title: element_to_string(*title),
snippet,
link,
};
println!("{:#?}", new_result);
found_results.push(new_result);
}
found_results
}
pub fn get_sanitised_html_graph(plaintext: &str) -> Html {
let tree = Html::parse_document(plaintext);
return tree;
}
pub fn scrape_url(url: &str) -> String {
pub fn scrape_url_blocking(url: &str) -> String {
let mut data = Vec::new();
let mut handle = Easy::new();
handle.url(url).unwrap();
{
let mut transfer = handle.transfer();
transfer
.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
})
.unwrap();
transfer.perform().unwrap();
}