Skip to content

Commit

Permalink
Add yahoo
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeerv committed Oct 31, 2024
1 parent 7cbd2ed commit de2a449
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
3 changes: 3 additions & 0 deletions api/url.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function Url.parse_with_params(input, query_params) end
--- @return Url
function Url.from_template(template, values) end

--- @return table<string, string>
function Url:params() end

--- @return string|nil
function Url:domain() end

Expand Down
5 changes: 1 addition & 4 deletions plugins/engines/google.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
-- (c) 2024 Dragynfruit

add_engine('google', function(client, query, _)
local offset = (query.page - 1) * 10
local offset = query.page * 10

local url = Url.parse_with_params(
--tostring(
-- 'https://google.com/search?filter=0&asearch=arc&oe=utf8&async=use_ac:true,_fmt:prog&start={start}&q={query}'
--),
'https://google.com/search',
{
filter = '0',
Expand Down
6 changes: 6 additions & 0 deletions plugins/engines/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
-- -- Licensed MIT.
-- -- (c) 2024 Dragynfruit

--- More advanced way to get nested keys based on / separator
---
--- @param data table
--- @param key string
---
--- @return any
local function get_key(data, key)
if key == nil then
return data
Expand Down
68 changes: 68 additions & 0 deletions plugins/engines/yahoo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
-- Yahoo scraper for Searched
-- Licensed MIT.
-- (c) 2024 Dragynfruit

--- Unquote the url
--- @param url string
---
--- @return string
local function unquote(url)
return url:gsub('%%(%x%x)', function(hex)
return string.char(tonumber(hex, 16))
end)
end

--- Converts the yahoo tracking url to a normal url
--- @param url_string string
---
--- @return string
local function remove_tracking(url_string)
local start = url_string:find('RU=') + 3
url_string = url_string:sub(start)
local end_ = url_string:find('/')
url_string = url_string:sub(1, end_ - 1)
return unquote(url_string)
end

add_engine('yahoo', function(client, query, opts)
local offset = query.page * 7 + 1

local url = Url.from_template(
tostring(
'https://search.yahoo.com/search?ei=UTF-8&o=json&p={query}&b={offset}'
),
{
query = query.query,
offset = tostring(offset),
}
):string()

local res = client:get(url, {
['Referer'] = 'https://yahoo.com/',
})
local data = parse_json(res)
local scr = Scraper.new(data.body)
assert(scr ~= nil)

local links = scr:select('.title>a')
local snippets = scr:select('.compText>p')

assert(table.pack(links).n == table.pack(snippets).n, 'snippets broken')

local results = {}
for i, _ in ipairs(links) do
local result_url = remove_tracking(links[i]:attr('href'))

if result_url ~= nil then
results[i] = {
url = result_url,
title = links[i]:attr('aria-label'),
general = {
snippet = snippets[i].inner_html,
},
}
end
end

return results
end)
9 changes: 9 additions & 0 deletions plugins/providers.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ description = "The most popular search engine"
kinds = ["sear"]
[google.features]
safe_search = "yes"
pagination = "0"

[wiby]
engine = "json"
Expand Down Expand Up @@ -149,4 +150,12 @@ description = "A privacy-respecting search engine"
kinds = ["sear"]
[qwant.features]
pagination = "1"
safe_search = "multilevel"

[yahoo]
name = "Yahoo"
description = "A search engine"
kinds = ["sear"]
[yahoo.features]
pagination = "0"
safe_search = "multilevel"
9 changes: 9 additions & 0 deletions src/lua_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl UrlWrapper {
.map(|x| UrlWrapper(x))
.into_lua_err()
}
fn params(lua: &Lua, this: &Self, _: ()) -> LuaResult<LuaValue> {
Ok(this
.0
.query_pairs()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect::<HashMap<String, String>>()
.into_lua(lua)?)
}
fn domain(lua: &Lua, this: &Self, _: ()) -> LuaResult<LuaValue> {
Ok(if let Some(domain) = this.0.domain() {
domain.to_string().into_lua(lua)?
Expand All @@ -82,6 +90,7 @@ impl LuaUserData for UrlWrapper {
methods.add_function("from_template", Self::from_template);
methods.add_function("parse", Self::parse);
methods.add_function("parse_with_params", Self::parse_with_params);
methods.add_method("params", Self::params);
methods.add_method("domain", Self::domain);
methods.add_method("authority", Self::authority);
methods.add_method("path", Self::path);
Expand Down

0 comments on commit de2a449

Please sign in to comment.