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)
|
2015-04-14 20:21:23 +02:00
|
|
|
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&safe=active&q="
|
2015-02-22 22:34:10 +01:00
|
|
|
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
|
|
|
|
2015-04-07 22:58:33 +02:00
|
|
|
local data = google.responseData
|
2015-04-14 20:21:23 +02:00
|
|
|
|
2015-04-07 22:58:33 +02:00
|
|
|
if not data or not data.results then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if #data.results == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Random image from table
|
|
|
|
local i = math.random(#data.results)
|
|
|
|
return data.results[i].url
|
2014-11-04 16:09:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function run(msg, matches)
|
|
|
|
local receiver = get_receiver(msg)
|
2015-04-07 22:58:33 +02:00
|
|
|
local text = matches[1]
|
2014-11-04 16:09:08 +01:00
|
|
|
local url = getGoogleImage(text)
|
2015-04-07 22:58:33 +02:00
|
|
|
|
|
|
|
if not url then
|
2015-04-14 20:21:23 +02:00
|
|
|
return "Kein Bild gefunden. Versuch es nochmal"
|
2015-04-07 22:58:33 +02:00
|
|
|
end
|
|
|
|
|
2015-04-14 20:21:23 +02:00
|
|
|
print("Bilder-URL: ", url)
|
|
|
|
send_photo_from_url(receiver, url)
|
2014-11-04 16:09:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-04-14 20:21:23 +02:00
|
|
|
description = "Sucht Bild mit Google-API und versendet es (SafeSearch aktiv)",
|
|
|
|
usage = "/img [Suchbegriff]",
|
|
|
|
patterns = {"^/img (.*)$"},
|
|
|
|
run = run
|
2014-11-04 16:09:08 +01:00
|
|
|
}
|
|
|
|
|
2015-04-14 20:21:23 +02:00
|
|
|
end
|