2015-12-06 17:19:49 +01:00
|
|
|
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
|
|
|
|
-- DO NOT USE WITHOUT PERMISSION
|
2015-12-03 20:39:14 +01:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
do
|
2015-12-03 20:39:14 +01:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local _blacklist
|
2015-04-07 22:58:33 +02:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function getGoogleImage(text)
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
local cseid = cred_data.google_cse_id
|
|
|
|
local url = 'https://www.googleapis.com/customsearch/v1?cx='..cseid..'&key='..apikey..'&searchType=image&num=10&fields=items(link)&safe=high&q='..URL.escape(text)
|
2015-12-03 20:39:14 +01:00
|
|
|
local res, code = https.request(url)
|
2015-12-06 17:19:49 +01:00
|
|
|
if code ~= 200 then return nil end
|
2015-12-03 20:39:14 +01:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local google = json:decode(res).items
|
2015-12-03 20:39:14 +01:00
|
|
|
return google
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function is_blacklisted(msg)
|
|
|
|
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
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
2015-12-06 17:19:49 +01:00
|
|
|
return var
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function show_blacklist()
|
|
|
|
if not _blacklist[1] then
|
|
|
|
return "Keine Wörter geblacklisted!\nBlackliste welche mit !imgblacklist add [Wort]"
|
|
|
|
else
|
|
|
|
local sort_alph = function( a,b ) return a < b end
|
|
|
|
table.sort( _blacklist, sort_alph )
|
|
|
|
local blacklist = "Folgende Wörter stehen auf der Blacklist:\n"
|
|
|
|
for v,word in pairs(_blacklist) do
|
|
|
|
blacklist = blacklist..'- '..word..'\n'
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
2015-12-06 17:19:49 +01:00
|
|
|
return blacklist
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function add_blacklist()
|
|
|
|
print('Blacklisting '..word..' - saving to redis set telegram:img_blacklist')
|
|
|
|
if redis:sismember("telegram:img_blacklist", word) == true then
|
|
|
|
return '"'..word..'" steht schon auf der Blacklist.'
|
|
|
|
else
|
|
|
|
redis:sadd("telegram:img_blacklist", word)
|
|
|
|
return '"'..word..'" blacklisted!'
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
2015-12-06 17:19:49 +01:00
|
|
|
end
|
2015-12-03 20:39:14 +01:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function remove_blacklist()
|
|
|
|
print('De-blacklisting '..word..' - removing from redis set telegram:img_blacklist')
|
|
|
|
if redis:sismember("telegram:img_blacklist", word) == true then
|
|
|
|
redis:srem("telegram:img_blacklist", word)
|
|
|
|
return '"'..word..'" erfolgreich von der Blacklist gelöscht!'
|
|
|
|
else
|
|
|
|
return '"'..word..'" steht nicht auf der Blacklist.'
|
|
|
|
end
|
|
|
|
end
|
2015-12-03 20:39:14 +01:00
|
|
|
|
2015-12-06 17:19:49 +01:00
|
|
|
local function cache_google_image(results, text)
|
|
|
|
local cache = {}
|
|
|
|
for v in pairs(results) do
|
|
|
|
table.insert(cache, results[v].link)
|
|
|
|
end
|
|
|
|
cache_data('img_google', string.lower(text), cache, 1209600, 'set')
|
2015-11-12 17:42:03 +01:00
|
|
|
end
|
|
|
|
|
2015-12-03 20:39:14 +01:00
|
|
|
function run(msg, matches)
|
2014-11-04 16:09:08 +01:00
|
|
|
local receiver = get_receiver(msg)
|
2015-04-07 22:58:33 +02:00
|
|
|
local text = matches[1]
|
2015-12-06 17:19:49 +01:00
|
|
|
if matches[2] then word = string.lower(matches[2]) end
|
|
|
|
|
|
|
|
_blacklist = redis:smembers("telegram:img_blacklist")
|
|
|
|
|
|
|
|
if text == "/imgblacklist show" then
|
|
|
|
if is_sudo(msg) then
|
|
|
|
return show_blacklist()
|
|
|
|
else
|
|
|
|
return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if text == "/imgblacklist add" and word == nil then
|
|
|
|
return "Benutzung: !imgblacklist add [Wort]"
|
|
|
|
elseif text == "!imgblacklist add" and word then
|
|
|
|
if is_sudo(msg) then
|
|
|
|
return add_blacklist()
|
|
|
|
else
|
|
|
|
return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if text == "/imgblacklist remove" and word == nil then
|
|
|
|
return "Benutzung: !imgblacklist remove [Wort]"
|
|
|
|
elseif text == "!imgblacklist remove" and word then
|
|
|
|
if is_sudo(msg) then
|
|
|
|
return remove_blacklist()
|
|
|
|
else
|
|
|
|
return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
print ('Checking if search contains blacklisted words: '..text)
|
|
|
|
if is_blacklisted(text) then
|
|
|
|
return "Vergiss es ._."
|
|
|
|
end
|
|
|
|
|
|
|
|
local hash = 'telegram:cache:img_google:'..string.lower(text)
|
|
|
|
local results = redis:smembers(hash)
|
|
|
|
if not results[1] then
|
|
|
|
print('doing web request')
|
|
|
|
results = getGoogleImage(text)
|
|
|
|
if not results then
|
|
|
|
return "Kein Bild gefunden!"
|
|
|
|
end
|
|
|
|
cache_google_image(results, text)
|
|
|
|
end
|
|
|
|
-- Random image from table
|
|
|
|
local i = math.random(#results)
|
|
|
|
local url = nil
|
|
|
|
|
|
|
|
local failed = true
|
|
|
|
local nofTries = 0
|
|
|
|
while failed and nofTries < #results do
|
|
|
|
if not results[i].link then
|
|
|
|
url = results[i]
|
|
|
|
else
|
|
|
|
url = results[i].link
|
|
|
|
end
|
|
|
|
print("Bilder-URL: ", url)
|
|
|
|
|
|
|
|
if string.ends(url, ".gif") then
|
|
|
|
failed = not send_document_from_url(receiver, url, nil, nil, true)
|
|
|
|
return 'Source: '..url
|
|
|
|
elseif string.ends(url, ".jpg") or string.ends(url, ".jpeg") or string.ends(url, ".png") then
|
|
|
|
failed = not send_photo_from_url(receiver, url, nil, nil, true)
|
|
|
|
return 'Source: '..url
|
|
|
|
end
|
|
|
|
|
|
|
|
nofTries = nofTries + 1
|
|
|
|
i = i+1
|
|
|
|
if i > #results then
|
|
|
|
i = 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if failed then
|
|
|
|
return "Fehler beim Herunterladen eines Bildes."
|
2015-06-24 02:32:35 +02:00
|
|
|
end
|
2014-11-04 16:09:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-12-06 17:19:49 +01:00
|
|
|
description = "Sucht Bild mit Google-API und versendet es (SafeSearch aktiv)",
|
|
|
|
usage = {
|
|
|
|
"/img [Suchbegriff]",
|
|
|
|
"/imgblacklist show: Zeigt Blacklist (nur Superuser)",
|
|
|
|
"/imgblacklist add [Wort]: Fügt Wort der Blacklist hinzu (nur Superuser)",
|
|
|
|
"/imgblacklist remove [Wort]: Entfernt Wort aus der Blacklist (nur Superuser)"
|
2015-12-03 20:39:14 +01:00
|
|
|
},
|
2015-12-06 17:19:49 +01:00
|
|
|
patterns = {
|
|
|
|
"^/img (.*)$",
|
|
|
|
"^(/imgblacklist show)$",
|
|
|
|
"^(/imgblacklist add) (.*)$",
|
|
|
|
"^(/imgblacklist remove) (.*)$"
|
|
|
|
},
|
|
|
|
run = run
|
2014-11-04 16:09:08 +01:00
|
|
|
}
|
2015-12-06 17:19:49 +01:00
|
|
|
end
|