This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/gSearch.lua

66 lines
1.4 KiB
Lua
Raw Normal View History

2015-07-03 00:15:52 +02:00
local PLUGIN = {}
PLUGIN.doc = [[
2015-07-08 09:38:04 +02:00
/google <query>
This command performs a Google search for the given query. Four results are returned. Safe search is enabled by default; use '/gnsfw' to get potentially NSFW results. Four results are returned for a group chat, or eight in a private message.
2015-07-03 00:15:52 +02:00
]]
PLUGIN.triggers = {
2015-07-08 09:38:04 +02:00
'^/g ',
'^/g$',
2015-07-08 09:38:04 +02:00
'^/google',
'^/gnsfw'
2015-07-03 00:15:52 +02:00
}
function PLUGIN.action(msg)
local url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0'
2015-07-08 09:38:04 +02:00
if not string.match(msg.text, '^/gnsfw ') then
2015-07-03 00:15:52 +02:00
url = url .. '&safe=active'
end
if not msg.chat.title then
url = url .. '&rsz=8'
end
local input = get_input(msg.text)
if not input then
if msg.reply_to_message then
msg = msg.reply_to_message
input = msg.text
else
return send_msg(msg, PLUGIN.doc)
end
2015-07-03 00:15:52 +02:00
end
url = url .. '&q=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
if res ~= 200 then
return send_msg(msg, config.locale.errors.connection)
2015-07-03 00:15:52 +02:00
end
local jdat = JSON.decode(jstr)
if #jdat.responseData.results < 1 then
return send_msg(msg, config.locale.errors.results)
2015-07-03 00:15:52 +02:00
end
local message = ''
2015-07-03 00:15:52 +02:00
for i = 1, #jdat.responseData.results do
local result_url = jdat.responseData.results[i].unescapedUrl
local result_title = jdat.responseData.results[i].titleNoFormatting
message = message .. ' - ' .. result_title ..'\n'.. result_url .. '\n'
end
local message = message:gsub('&amp;', '&') -- blah
2015-07-03 00:15:52 +02:00
send_msg(msg, message)
end
return PLUGIN