returning valus based on lua api
This commit is contained in:
89
Cargo.lock
generated
89
Cargo.lock
generated
@@ -2,6 +2,12 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "backend"
|
||||
version = "0.1.0"
|
||||
@@ -16,6 +22,16 @@ version = "2.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
|
||||
|
||||
[[package]]
|
||||
name = "bstr"
|
||||
version = "1.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a680398c10c7bc0488067c4612226ea905a85f607134b69f3068d505d58c4214"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.11.2"
|
||||
@@ -127,6 +143,12 @@ version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7bd50d24b1b75aa817bb0339cb9f2e0e664bdfab1e160a69a0fda9145b77fab2"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.5.0"
|
||||
@@ -144,6 +166,7 @@ name = "frontend"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"backend",
|
||||
"mlua",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -205,6 +228,25 @@ version = "0.4.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
|
||||
|
||||
[[package]]
|
||||
name = "lua-src"
|
||||
version = "551.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d400ffef0e3d4d29287092bdc5a276d0bf468b2c69709d5bab0d469312d9f947"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luajit-src"
|
||||
version = "210.7.5+63c8f1a"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a98fb6f037124098f4c2283286a0431a4bb0b1d243b90ca1a4d34dd268ea5b96"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"which",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "markup5ever"
|
||||
version = "0.40.0"
|
||||
@@ -222,12 +264,50 @@ version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "mlua"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a7ae6b65bf54b7fdd4cf5b6f44f60862350ebf3fe3779b3a9cc005e7fa14f81"
|
||||
dependencies = [
|
||||
"bstr",
|
||||
"either",
|
||||
"libc",
|
||||
"mlua-sys",
|
||||
"num-traits",
|
||||
"parking_lot",
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mlua-sys"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1631cda17bd6486410ab751d91f5fd4484f441589278cdad879eb3afc721a285"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"lua-src",
|
||||
"luajit-src",
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "new_debug_unreachable"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "openssl-probe"
|
||||
version = "0.1.6"
|
||||
@@ -585,6 +665,15 @@ dependencies = [
|
||||
"string_cache_codegen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "which"
|
||||
version = "8.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bae2f2b2b816647a1cab1acc91f5bd20812d53cb344382635ec2181940c8034f"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use curl::easy::Easy;
|
||||
pub use scraper;
|
||||
use scraper::{ElementRef, Html, Selector};
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -34,7 +35,7 @@ pub fn return_results_vec() -> Vec<SearchResult> {
|
||||
return filtered_results;
|
||||
}
|
||||
|
||||
fn filter_html(html_tree: &Html, filter_set: &SiteHtmlFilter) -> Vec<SearchResult> {
|
||||
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();
|
||||
@@ -74,12 +75,12 @@ fn filter_html(html_tree: &Html, filter_set: &SiteHtmlFilter) -> Vec<SearchResul
|
||||
found_results
|
||||
}
|
||||
|
||||
fn get_sanitised_html_graph(plaintext: &str) -> Html {
|
||||
pub fn get_sanitised_html_graph(plaintext: &str) -> Html {
|
||||
let tree = Html::parse_document(plaintext);
|
||||
return tree;
|
||||
}
|
||||
|
||||
fn scrape_url(url: &str) -> String {
|
||||
pub fn scrape_url(url: &str) -> String {
|
||||
let mut data = Vec::new();
|
||||
|
||||
let mut handle = Easy::new();
|
||||
|
||||
19
config.lua
Normal file
19
config.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
local function duckduckgo(query)
|
||||
-- local result = vs.fetchUrl("https://lite.duckduckgo.com/lite?q=" .. query)
|
||||
|
||||
-- local htmlTree = vs.decodeHtml(result)
|
||||
|
||||
local selector = {
|
||||
title = "tbody tr td a.result-link",
|
||||
snippet = "tbody tr td.result-snippet",
|
||||
url = "tbody tr td a.result-link/href",
|
||||
}
|
||||
|
||||
return { title = "something", some = "some" }
|
||||
end
|
||||
|
||||
local function ddgBuilder(values)
|
||||
return "<p>" .. values.some .. "</p>"
|
||||
end
|
||||
|
||||
SearchEngines = { duckduckgo = { grabber = duckduckgo, builder = ddgBuilder } }
|
||||
@@ -5,3 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
backend = {path = "../backend/"}
|
||||
mlua = { version = "0.12.2", features = ["lua54", "vendored"] }
|
||||
|
||||
124
frontend/src/lua/mod.rs
Normal file
124
frontend/src/lua/mod.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
extern crate backend;
|
||||
use backend::scraper;
|
||||
use mlua::{Lua, Table, UserData, UserDataMethods};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
struct LuaHtml {
|
||||
document: scraper::Html,
|
||||
}
|
||||
|
||||
impl UserData for LuaHtml {
|
||||
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
|
||||
methods.add_method("scrape", |lua, this, config: Table| {
|
||||
let mut selectors = Vec::new();
|
||||
|
||||
// Compile all configured selectors
|
||||
for pair in config.pairs::<String, String>() {
|
||||
let (name, selector_string) = pair?;
|
||||
|
||||
let parts: Vec<&str> = selector_string.splitn(2, '/').collect();
|
||||
|
||||
let css_selector = Selector::parse(parts[0]).map_err(mlua::Error::external)?;
|
||||
|
||||
let attribute = parts.get(1).copied();
|
||||
|
||||
selectors.push((name, css_selector, attribute));
|
||||
}
|
||||
|
||||
// Determine result containers.
|
||||
// For now, use the first configured selector as the source
|
||||
// of result elements.
|
||||
let mut results = Vec::new();
|
||||
|
||||
if let Some((_, first_selector, _)) = selectors.first() {
|
||||
for element in this.document.select(first_selector) {
|
||||
let mut result = HashMap::new();
|
||||
|
||||
for (name, selector, attribute) in &selectors {
|
||||
let value = element.select(selector).next();
|
||||
|
||||
let extracted = match (value, attribute) {
|
||||
(Some(element), Some(attribute)) => {
|
||||
element.value().attr(attribute).unwrap_or("").to_string()
|
||||
}
|
||||
|
||||
(Some(element), None) => {
|
||||
element.text().collect::<String>().trim().to_string()
|
||||
}
|
||||
|
||||
(None, _) => String::new(),
|
||||
};
|
||||
|
||||
result.insert(name.clone(), extracted);
|
||||
}
|
||||
|
||||
results.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
// Convert results into Lua tables
|
||||
let output = lua.create_table()?;
|
||||
|
||||
for (index, result) in results.iter().enumerate() {
|
||||
let result_table = lua.create_table()?;
|
||||
|
||||
for (key, value) in result {
|
||||
result_table.set(key.as_str(), value.as_str())?;
|
||||
}
|
||||
|
||||
output.set(index + 1, result_table)?;
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
struct DataSelector {
|
||||
title: String,
|
||||
snippet: String,
|
||||
url: String,
|
||||
}
|
||||
|
||||
pub fn create_instance() -> mlua::Lua {
|
||||
mlua::Lua::new()
|
||||
}
|
||||
|
||||
pub fn intialise_lua(instance: &mut mlua::Lua, path: Option<String>) -> mlua::Result<Table> {
|
||||
let lua = instance;
|
||||
|
||||
let function_table = create_lua_function_table(&lua)?;
|
||||
|
||||
lua.globals().set("vs", function_table)?;
|
||||
|
||||
let path = path.unwrap_or("./config.lua".to_string());
|
||||
let file_info = fs::read_to_string(path)?;
|
||||
|
||||
lua.load(&file_info).exec()?;
|
||||
|
||||
let search_engines: Table = lua.globals().get("SearchEngines")?;
|
||||
Ok(search_engines)
|
||||
}
|
||||
|
||||
fn create_lua_function_table(lua: &Lua) -> mlua::Result<Table> {
|
||||
let vs_table = lua.create_table()?;
|
||||
|
||||
vs_table.set(
|
||||
"fetchUrl",
|
||||
lua.create_function(|_, url: String| {
|
||||
let scraped_data = backend::scrape_url(&url);
|
||||
Ok(scraped_data)
|
||||
})?,
|
||||
)?;
|
||||
|
||||
vs_table.set(
|
||||
"decodeHtml",
|
||||
lua.create_function(|_, result: String| {
|
||||
let document = backend::get_sanitised_html_graph(&result);
|
||||
Ok(LuaHtml { document })
|
||||
})?,
|
||||
)?;
|
||||
|
||||
Ok(vs_table)
|
||||
}
|
||||
@@ -1,7 +1,69 @@
|
||||
mod lua;
|
||||
|
||||
use backend::return_results_vec;
|
||||
use std::{env, process};
|
||||
|
||||
fn main() {
|
||||
let something = return_results_vec();
|
||||
let mut args = env::args();
|
||||
// let something = return_results_vec();
|
||||
|
||||
// println!("{}", something);
|
||||
let mut config_location: Option<String> = None;
|
||||
|
||||
while let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
"--config" | "-c" => {
|
||||
config_location = args.next().or_else(|| {
|
||||
eprintln!("--config needs a path");
|
||||
process::exit(1)
|
||||
})
|
||||
}
|
||||
&_ => {
|
||||
eprintln!("Unknown command line option {}", arg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Long lived lua instance
|
||||
let mut lua_instance = lua::create_instance();
|
||||
|
||||
let search_engines: mlua::Table =
|
||||
lua::intialise_lua(&mut lua_instance, config_location).expect("Failed to initialise lua");
|
||||
|
||||
for pair in search_engines.pairs::<String, mlua::Value>() {
|
||||
let (name, functions) = pair.expect("Invalid Search Engine Pair");
|
||||
|
||||
let mut grabber_function: Option<mlua::Function> = None;
|
||||
let mut builder_function: Option<mlua::Function> = lua_instance
|
||||
.globals()
|
||||
.get("DefaultBuilder")
|
||||
.expect("Could Not Unwrap DefaultBuilder");
|
||||
|
||||
match functions {
|
||||
mlua::Value::Function(function) => grabber_function = Some(function),
|
||||
mlua::Value::Table(table) => {
|
||||
for inner_pair in table.pairs::<String, mlua::Function>() {
|
||||
let (name, function) = inner_pair.expect("Error decoding pairs");
|
||||
|
||||
match name.as_str() {
|
||||
"grabber" => grabber_function = Some(function),
|
||||
"builder" => builder_function = Some(function),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ => {
|
||||
eprintln!("Unexpected value in returned lua table")
|
||||
}
|
||||
}
|
||||
|
||||
let result = grabber_function
|
||||
.expect("No function found")
|
||||
.call::<mlua::Table>("test");
|
||||
let built_node = builder_function
|
||||
.expect("Invalid Builder Function")
|
||||
.call::<String>(result);
|
||||
|
||||
println!("{:#?}", built_node)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user