returning valus based on lua api
This commit is contained in:
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