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/plugins/img_google.lua

35 lines
886 B
Lua
Raw Normal View History

2015-02-22 22:34:10 +01:00
do
2014-11-04 16:09:08 +01:00
function getGoogleImage(text)
2015-02-22 22:34:10 +01:00
local text = URL.escape(text)
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&q="
local res, code = http.request(api..text)
if code ~= 200 then return nil end
local google = json:decode(res)
2014-11-04 16:09:08 +01:00
2015-02-22 22:34:10 +01:00
if google.responseStatus ~= 200 then
return nil
2014-11-04 16:09:08 +01:00
end
2015-02-22 22:34:10 +01:00
-- Google response Ok
local i = math.random(#google.responseData.results) -- Random image from results
return google.responseData.results[i].url
2014-11-04 16:09:08 +01:00
end
function run(msg, matches)
local receiver = get_receiver(msg)
local text = msg.text:sub(6,-1)
local url = getGoogleImage(text)
2015-02-22 22:34:10 +01:00
print("Image URL: ", url)
send_photo_from_url(receiver, url)
2014-11-04 16:09:08 +01:00
end
return {
2015-02-04 23:03:42 +01:00
description = "Search image with Google API and sends it.",
usage = "!img [term]: Random search an image with Google API.",
2014-11-04 22:06:59 +01:00
patterns = {"^!img (.*)$"},
2014-11-04 16:09:08 +01:00
run = run
}
2015-02-22 22:34:10 +01:00
end