Skip to content

Commit

Permalink
Config does stuff now; Wiby works
Browse files Browse the repository at this point in the history
  • Loading branch information
frc4533-lincoln committed Oct 28, 2024
1 parent dacca2a commit 8532c8a
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 69 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ axum-macros = "0.4.2"
#piccolo-util = "0.3.3"
serde_json = "1.0.132"
toml = { version = "0.8.19", default-features = false, features = ["parse"] }
strfmt = "0.2.4"

[profile.dev.package.'*']
opt-level = 3
Expand Down
4 changes: 2 additions & 2 deletions api/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function add_search_provider(name, kind, callback) end
--- Add a engine
---
--- @param name string
--- @param callback fun(query: Query): [Result]
function add_search_provider(name, callback) end
--- @param callback fun(query: Query, url: string|nil): [Result]
function add_engine(name, callback) end

--- Use a engine
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- Licensed MIT.
-- (c) 2024 Dragynfruit

add_search_provider('duckduckgo', 'sear', function (query)
add_engine('duckduckgo', function (query, _)
local offset
if query.page == 2 then
offset = (query.page - 1) * 20
Expand Down
File renamed without changes.
13 changes: 9 additions & 4 deletions plugins/engines/json_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
-- -- Licensed MIT.
-- -- (c) 2024 Dragynfruit

add_engine("json_engine", function(url, query)
local res = get(string.format(url, query.query, query.page), {})
add_engine("json_engine", function(_, url)
if url ~= nil then
local res = get(url, {})
local data = parse_json(res)

local results = {}
Expand All @@ -15,5 +16,9 @@ add_engine("json_engine", function(url, query)
}
end

return results
end)
return results
else
print('wtf')
return {}
end
end)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_search_provider('stackoverflow', 'qans', function (query)
add_engine('stackoverflow', function (query, _)
local res = get('https://api.stackexchange.com/2.3/search/advanced?q=' .. query.query .. '&page=' .. query.page .. '&site=stackoverflow', {})

print(res)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-- -- Licensed MIT.
-- -- (c) 2024 Dragynfruit

add_search_provider('wikipedia', 'wiki', function (query)
add_engine('mediawiki', function (query, _)
local res = get('https://en.wikipedia.org/w/api.php?action=opensearch&format=json&limit=10&namespace=0&search='..query.query, {})

local data = parse_json(res)
Expand Down
1 change: 1 addition & 0 deletions searched.toml → plugins/providers.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ kinds = ["sear"]

[provider.wiby]
engine = "json_engine"
url = "https://wiby.me/json?q={query}&page={page}"
name = "Wiby"
description = "blah blah"
kinds = ["sear"]
7 changes: 0 additions & 7 deletions plugins/scrapers/wiby.lua

This file was deleted.

2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct CfgProvider {
///
/// Uses provider name if unset
pub engine: Option<String>,
/// Formatting string for URL
pub url: Option<String>,
/// Human readable name
pub name: String,
pub description: String,
Expand Down
90 changes: 38 additions & 52 deletions src/lua_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ use std::{
use mlua::prelude::*;
use reqwest::Client;
use scraper::{node::Element, Html, Selector};
use strfmt::{strfmt, Format};
use tokio::{
sync::{oneshot, watch, Mutex},
task::{spawn_local, JoinHandle, JoinSet, LocalSet},
};

use crate::{Kind, Query};
use crate::{config::{self, Config}, Kind, Query};

impl LuaUserData for Query {
fn add_fields<F: LuaUserDataFields<Self>>(fields: &mut F) {
Expand Down Expand Up @@ -74,11 +75,12 @@ pub struct PluginEngine {
}
impl PluginEngine {
pub async fn new() -> Result<Self, Box<dyn Error>> {
let config = Config::load("plugins/providers.toml");
let (query_tx, rx) = watch::channel(Default::default());
let (tx, results_rx) = watch::channel(Default::default());

spawn_local(async move {
Self::inner(rx, tx).await.unwrap();
Self::inner(config.clone(), rx, tx).await.unwrap();
});

Ok(Self {
Expand All @@ -87,17 +89,17 @@ impl PluginEngine {
})
}


/// Actual Lua init and event loop
async fn inner(
config: Config,
mut rx: watch::Receiver<crate::Query>,
tx: watch::Sender<Vec<crate::Result>>,
) -> Result<(), Box<dyn Error>> {
let lua = Lua::new();

// Add Lua interfaces

lua.globals()
.set("__searched_providers__", lua.create_table()?)?;
lua.globals()
.set("__searched_engines__", lua.create_table()?)?;
lua.globals().set("Query", lua.create_proxy::<Query>()?)?;
Expand All @@ -106,24 +108,6 @@ impl PluginEngine {
lua.globals()
.set("Element", lua.create_proxy::<ElementWrapper>()?)?;

async fn add_search_provider(
lua: Lua,
(name, _kind, callback): (String, Kind, LuaFunction),
) -> LuaResult<()> {
lua.globals()
.get::<LuaTable>("__searched_providers__")
.unwrap()
.set(name, callback.clone())
.unwrap();

Ok(())
}

lua.globals().set(
"add_search_provider",
lua.create_async_function(add_search_provider)?,
)?;

async fn add_engine(
lua: Lua,
(name, callback): (String, LuaFunction),
Expand All @@ -136,27 +120,11 @@ impl PluginEngine {

Ok(())
}

lua.globals().set(
"add_engine",
lua.create_async_function(add_engine)?,
)?;

async fn use_engine(
lua: Lua,
name: String,
) -> LuaResult<LuaFunction> {
lua.globals()
.get::<LuaTable>("__searched_engines__")
.unwrap()
.get::<LuaFunction>(name)
}

lua.globals().set(
"use_engine",
lua.create_async_function(use_engine)?,
)?;

async fn get(_: Lua, (url, headers): (String, LuaTable)) -> LuaResult<String> {
let mut req = Client::new().get(url);

Expand Down Expand Up @@ -229,15 +197,15 @@ impl PluginEngine {
})?,
)?;

// Load scrapers
for path in read_dir("plugins/scrapers").unwrap() {
if let Ok(path) = path {
let mut buf = String::new();
let mut f = File::open(path.path()).unwrap();
f.read_to_string(&mut buf).unwrap();
lua.load(&buf).exec_async().await.unwrap();
}
}
//// Load scrapers
//for path in read_dir("plugins/scrapers").unwrap() {
// if let Ok(path) = path {
// let mut buf = String::new();
// let mut f = File::open(path.path()).unwrap();
// f.read_to_string(&mut buf).unwrap();
// lua.load(&buf).exec_async().await.unwrap();
// }
//}

// Load engines
for path in read_dir("plugins/engines").unwrap() {
Expand All @@ -253,15 +221,33 @@ impl PluginEngine {
while let Ok(()) = rx.changed().await {
let query = rx.borrow_and_update().clone();

let provider_ = lua
let provider = config.providers.get(&query.provider).unwrap();

let engine = provider.engine.clone().unwrap_or(query.provider.clone());

let engine_ = lua
.globals()
.get::<LuaTable>("__searched_providers__")
.get::<LuaTable>("__searched_engines__")
.unwrap()
.get::<LuaFunction>(query.provider.clone())
.get::<LuaFunction>(engine)
.unwrap();

let results = provider_
.call_async::<Vec<HashMap<String, String>>>(query)
let url = {
if let Some(url_fmt) = &provider.url {
let query = query.clone();
let map = HashMap::from_iter([
("query".to_string(), query.query),
("page".to_string(), query.page.to_string()),
]);

Some(url_fmt.format(&map).unwrap())
} else {
None
}
};

let results = engine_
.call_async::<Vec<HashMap<String, String>>>((query, url))
.await;

match results {
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async fn main() {
//let (engine, local) = PluginEngine::new().await.unwrap();
let (pool, joinset) = PluginEnginePool::new().await;

let config = Config::load("searched.toml");
let config = Config::load("plugins/providers.toml");

info!("initializing web");
let r = Router::new()
Expand Down

0 comments on commit 8532c8a

Please sign in to comment.