2015-07-03 00:15:52 +02:00
|
|
|
local PLUGIN = {}
|
|
|
|
|
|
|
|
PLUGIN.doc = [[
|
2015-07-08 09:38:04 +02:00
|
|
|
/images <query>
|
2015-07-09 02:11:33 +02:00
|
|
|
This command performs a Google Images search for the given query. One random top result is returned. Safe search is enabled by default; use '/insfw' to get potentially NSFW results.
|
2015-07-21 01:29:40 +02:00
|
|
|
Want images sent directly to chat? Try @ImageBot.
|
2015-07-03 00:15:52 +02:00
|
|
|
]]
|
|
|
|
|
|
|
|
PLUGIN.triggers = {
|
2015-07-08 09:38:04 +02:00
|
|
|
'^/images?',
|
|
|
|
'^/img',
|
|
|
|
'^/i ',
|
|
|
|
'^/insfw'
|
2015-07-03 00:15:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PLUGIN.exts = {
|
|
|
|
'.png$',
|
|
|
|
'.jpg$',
|
|
|
|
'.jpeg$',
|
|
|
|
'.jpe$',
|
|
|
|
'.gif$'
|
|
|
|
}
|
|
|
|
|
|
|
|
function PLUGIN.action(msg)
|
|
|
|
|
|
|
|
local url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8'
|
|
|
|
|
2015-07-08 09:38:04 +02:00
|
|
|
if not string.match(msg.text, '^/insfw ') then
|
2015-07-03 00:15:52 +02:00
|
|
|
url = url .. '&safe=active'
|
|
|
|
end
|
|
|
|
|
|
|
|
local input = get_input(msg.text)
|
|
|
|
if not input then
|
2015-07-15 08:15:23 +02:00
|
|
|
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
|
2015-07-15 08:15:23 +02:00
|
|
|
send_msg(msg, config.locale.errors.connection)
|
2015-07-03 00:15:52 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local jdat = JSON.decode(jstr)
|
|
|
|
|
|
|
|
if #jdat.responseData.results < 1 then
|
2015-07-15 08:15:23 +02:00
|
|
|
send_msg(msg, config.locale.errors.results)
|
2015-07-03 00:15:52 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-08-04 22:11:55 +02:00
|
|
|
local is_real = false
|
|
|
|
local counter = 0
|
2015-07-03 00:15:52 +02:00
|
|
|
while is_real == false do
|
2015-08-04 22:11:55 +02:00
|
|
|
counter = counter + 1
|
|
|
|
if counter > 5 then
|
|
|
|
return send_msg(msg, config.locale.errors.results)
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
local i = math.random(#jdat.responseData.results)
|
|
|
|
result_url = jdat.responseData.results[i].url
|
|
|
|
for i,v in pairs(PLUGIN.exts) do
|
|
|
|
if string.match(string.lower(result_url), v) then
|
|
|
|
is_real = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
send_message(msg.chat.id, result_url, false, msg.message_id)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
return PLUGIN
|