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_nsfw.lua

58 lines
1.3 KiB
Lua
Raw Normal View History

2015-04-14 20:21:23 +02:00
do
2015-04-20 18:52:25 +02:00
function getGoogleImage2(text)
2015-04-14 20:21:23 +02: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)
if google.responseStatus ~= 200 then
return nil
end
local data = google.responseData
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
end
function run(msg, matches)
local receiver = get_receiver(msg)
local text = matches[1]
2015-04-20 18:52:25 +02:00
local url = getGoogleImage2(text)
2015-04-14 20:21:23 +02:00
if not url then
2015-04-17 16:29:30 +02:00
return "Kein Bild gefunden."
2015-04-14 20:21:23 +02:00
end
if string.ends(url, ".svg") then
return "Fehler beim laden des Bildes."
end
2015-04-14 20:21:23 +02:00
print("Bilder-URL: ", url)
if string.ends(url, ".gif") then
send_document_from_url(receiver, url)
return "Source: "..url
else
2015-04-14 20:21:23 +02:00
send_photo_from_url(receiver, url)
2015-06-07 22:49:11 +02:00
return "Source: "..url
end
2015-04-14 20:21:23 +02:00
end
return {
2015-04-28 17:49:11 +02:00
description = "Sucht Bild mit Google-API und versendet es [NSFW]",
usage = {"/img2 [Suchbegriff]","/nsfwimg [Suchbegriff]"},
patterns = {"^/img2 (.*)$","^/nsfwimg (.*)$"},
2015-04-14 20:21:23 +02:00
run = run
}
2015-05-22 17:01:33 +02:00
end