2016-07-18 18:37:29 +02:00
|
|
|
-- You need a Google API key and a Google Custom Search Engine set up to use this, in config.google_api_key and config.google_cse_key, respectively.
|
|
|
|
-- You must also sign up for the CSE in the Google Developer Console, and enable image results.
|
|
|
|
|
|
|
|
local gImages_nsfw = {}
|
|
|
|
|
|
|
|
function gImages_nsfw:init(config)
|
|
|
|
if not cred_data.google_apikey then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: google_apikey.')
|
|
|
|
print('gImages_nsfw.lua wird nicht aktiviert.')
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
elseif not cred_data.google_cse_id then
|
2016-08-01 21:07:27 +02:00
|
|
|
print('Fehlender Key: google_cse_id.')
|
|
|
|
print('gImages_nsfw.lua wird nicht aktiviert.')
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
gImages_nsfw.triggers = {
|
|
|
|
"^/[Ii][Mm][Gg]2 (.*)$",
|
|
|
|
"^/[Ii]2 (.*)$"
|
|
|
|
}
|
|
|
|
gImages_nsfw.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[img2* _<Suchbegriff>_
|
|
|
|
Sucht Bild mit Google und versendet es
|
|
|
|
Alias: *]]..config.cmd_pat..[[i2*]]
|
|
|
|
end
|
|
|
|
|
|
|
|
gImages_nsfw.command = 'img2 <Suchbegriff>'
|
|
|
|
|
2016-09-11 14:07:55 +02:00
|
|
|
function gImages_nsfw:is_blacklisted(msg)
|
2016-09-08 01:35:10 +02:00
|
|
|
_blacklist = redis:smembers("telegram:img_blacklist")
|
|
|
|
local var = false
|
|
|
|
for v,word in pairs(_blacklist) do
|
|
|
|
if string.find(string.lower(msg), string.lower(word)) then
|
|
|
|
print("Wort steht auf der Blacklist!")
|
|
|
|
var = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return var
|
|
|
|
end
|
|
|
|
|
2016-07-18 18:37:29 +02:00
|
|
|
-- Yes, the callback is copied from below, but I can't think of another method :\
|
|
|
|
function gImages_nsfw:callback(callback, msg, self, config, input)
|
|
|
|
if not msg then return end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_callback_query(callback, 'Suche nochmal nach "'..URL.unescape(input)..'"')
|
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-07-18 18:37:29 +02:00
|
|
|
local hash = 'telegram:cache:gImages_nsfw'
|
|
|
|
local results = redis:smembers(hash..':'..string.lower(URL.unescape(input)))
|
|
|
|
|
|
|
|
if not results[1] then
|
|
|
|
print('doing web request')
|
|
|
|
results = gImages_nsfw:get_image(input)
|
|
|
|
if results == 403 then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.quotaexceeded, true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
elseif not results then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results, true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
gImages_nsfw:cache_result(results, input)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Random image from table
|
|
|
|
local i = math.random(#results)
|
|
|
|
|
|
|
|
-- Thanks to Amedeo for this!
|
|
|
|
local failed = true
|
|
|
|
local nofTries = 0
|
|
|
|
|
|
|
|
while failed and nofTries < #results do
|
|
|
|
if results[i].image then
|
|
|
|
img_url = results[i].link
|
|
|
|
mimetype = results[i].mime
|
|
|
|
context = results[i].image.contextLink
|
|
|
|
else -- from cache
|
|
|
|
img_url = results[i]
|
|
|
|
mimetype = redis:hget(hash..':'..img_url, 'mime')
|
|
|
|
context = redis:hget(hash..':'..img_url, 'contextLink')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- It's important to save the image with the right ending!
|
|
|
|
if mimetype == 'image/gif' then
|
|
|
|
file = download_to_file(img_url)
|
|
|
|
elseif mimetype == 'image/png' then
|
|
|
|
file = download_to_file(img_url)
|
|
|
|
elseif mimetype == 'image/jpeg' then
|
|
|
|
file = download_to_file(img_url)
|
|
|
|
else
|
|
|
|
file = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if not file then
|
|
|
|
nofTries = nofTries + 1
|
|
|
|
i = i+1
|
|
|
|
if i > #results then
|
|
|
|
i = 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
failed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if failed then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Fehler beim Herunterladen eines Bildes.', true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if mimetype == 'image/gif' then
|
2016-08-24 17:18:17 +02:00
|
|
|
result = utilities.send_document(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..input..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
result = utilities.send_photo(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..input..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not result then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..input..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function gImages_nsfw:get_image(input)
|
2016-08-01 21:07:27 +02:00
|
|
|
local apikey = cred_data.google_apikey
|
2016-07-18 18:37:29 +02:00
|
|
|
local cseid = cred_data.google_cse_id
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/customsearch/v1'
|
|
|
|
local url = BASE_URL..'/?searchType=image&alt=json&num=10&key='..apikey..'&cx='..cseid..'&q=' .. input .. '&fields=items(link,mime,image(contextLink))'
|
2016-08-01 21:07:27 +02:00
|
|
|
local jstr, res = https.request(url)
|
|
|
|
local jdat = json.decode(jstr).items
|
2016-07-18 18:37:29 +02:00
|
|
|
|
|
|
|
if not jdat then
|
|
|
|
return 'NORESULTS'
|
|
|
|
end
|
|
|
|
|
|
|
|
if jdat.error then
|
|
|
|
if jdat.error.code == 403 then
|
|
|
|
return 403
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return jdat
|
|
|
|
end
|
|
|
|
|
|
|
|
function gImages_nsfw:cache_result(results, text)
|
|
|
|
local cache = {}
|
|
|
|
for v in pairs(results) do
|
2016-08-15 23:14:28 +02:00
|
|
|
cache[v] = results[v].link
|
2016-07-18 18:37:29 +02:00
|
|
|
end
|
|
|
|
for n, link in pairs(cache) do
|
|
|
|
redis:hset('telegram:cache:gImages_nsfw:'..link, 'mime', results[n].mime)
|
|
|
|
redis:hset('telegram:cache:gImages_nsfw:'..link, 'contextLink', results[n].image.contextLink)
|
|
|
|
redis:expire('telegram:cache:gImages_nsfw:'..link, 1209600)
|
|
|
|
end
|
|
|
|
cache_data('gImages_nsfw', string.lower(text), cache, 1209600, 'set')
|
|
|
|
end
|
|
|
|
|
|
|
|
function gImages_nsfw:action(msg, config, matches)
|
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_message(msg.chat.id, gImages_nsfw.doc, true, msg.message_id, true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-01 21:07:27 +02:00
|
|
|
print ('Überprüfe, ob das Wort auf der Blackliste steht: '..input)
|
2016-09-11 14:07:55 +02:00
|
|
|
if gImages_nsfw:is_blacklisted(input) then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, 'Vergiss es!')
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_typing(msg.chat.id, 'upload_photo')
|
2016-07-18 18:37:29 +02:00
|
|
|
|
|
|
|
local hash = 'telegram:cache:gImages_nsfw'
|
|
|
|
local results = redis:smembers(hash..':'..string.lower(input))
|
|
|
|
|
|
|
|
if not results[1] then
|
|
|
|
print('doing web request')
|
|
|
|
results = gImages_nsfw:get_image(URL.escape(input))
|
|
|
|
if results == 403 then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.quotaexceeded, true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
elseif not results or results == 'NORESULTS' then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, config.errors.results, true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
gImages_nsfw:cache_result(results, input)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Random image from table
|
|
|
|
local i = math.random(#results)
|
|
|
|
|
|
|
|
-- Thanks to Amedeo for this!
|
|
|
|
local failed = true
|
|
|
|
local nofTries = 0
|
|
|
|
|
|
|
|
while failed and nofTries < #results do
|
|
|
|
if results[i].image then
|
|
|
|
img_url = results[i].link
|
|
|
|
mimetype = results[i].mime
|
|
|
|
context = results[i].image.contextLink
|
|
|
|
else -- from cache
|
|
|
|
img_url = results[i]
|
|
|
|
mimetype = redis:hget(hash..':'..img_url, 'mime')
|
|
|
|
context = redis:hget(hash..':'..img_url, 'contextLink')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- It's important to save the image with the right ending!
|
|
|
|
if mimetype == 'image/gif' then
|
2016-08-01 21:07:27 +02:00
|
|
|
file = download_to_file(img_url)
|
2016-07-18 18:37:29 +02:00
|
|
|
elseif mimetype == 'image/png' then
|
2016-08-01 21:07:27 +02:00
|
|
|
file = download_to_file(img_url)
|
2016-07-18 18:37:29 +02:00
|
|
|
elseif mimetype == 'image/jpeg' then
|
2016-08-01 21:07:27 +02:00
|
|
|
file = download_to_file(img_url)
|
2016-07-18 18:37:29 +02:00
|
|
|
else
|
|
|
|
file = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if not file then
|
|
|
|
nofTries = nofTries + 1
|
|
|
|
i = i+1
|
|
|
|
if i > #results then
|
|
|
|
i = 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
failed = false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
if failed then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Fehler beim Herunterladen eines Bildes.', true)
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if mimetype == 'image/gif' then
|
2016-08-24 17:18:17 +02:00
|
|
|
result = utilities.send_document(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..URL.escape(input)..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
else
|
2016-08-24 17:18:17 +02:00
|
|
|
result = utilities.send_photo(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Seite aufrufen","url":"'..context..'"},{"text":"Bild aufrufen","url":"'..img_url..'"},{"text":"Nochmal suchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..URL.escape(input)..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not result then
|
2016-08-24 17:18:17 +02:00
|
|
|
utilities.send_reply(msg, config.errors.connection, true, '{"inline_keyboard":[[{"text":"Nochmal versuchen","callback_data":"@'..self.info.username..' gImages_nsfw:'..URL.escape(input)..'"}]]}')
|
2016-07-18 18:37:29 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return gImages_nsfw
|