Neue Google IMG Plugins

This commit is contained in:
Akamaru 2015-12-06 17:19:49 +01:00
parent a55d2ea60a
commit 2f93d7968c
3 changed files with 273 additions and 97 deletions

View File

@ -649,6 +649,14 @@ function cache_data(plugin, query, data, timeout, typ)
print('Caching "'..query..'" from plugin '..plugin..' (expires in '..timeout..' seconds)') print('Caching "'..query..'" from plugin '..plugin..' (expires in '..timeout..' seconds)')
if typ == 'key' then if typ == 'key' then
redis:set(hash, data) redis:set(hash, data)
elseif typ == 'set' then
-- make sure that you convert your data into a table:
-- {"foo", "bar", "baz"} instead of
-- {"bar" = "foo", "foo" = "bar", "bar" = "baz"}
-- because other formats are not supported by redis (or I haven't found a way to store them)
for _,str in pairs(data) do
redis:sadd(hash, str)
end
else else
redis:hmset(hash, data) redis:hmset(hash, data)
end end

View File

@ -1,124 +1,173 @@
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
do do
local mime = require("mime")
local google_config = load_from_file('data/google.lua') local _blacklist
local cache = {}
--[[
local function send_request(url)
local t = {}
local options = {
url = url,
sink = ltn12.sink.table(t),
method = "GET"
}
local a, code, headers, status = http.request(options)
return table.concat(t), code, headers, status
end]]--
local function get_google_data(text)
--[[
local url = "http://ajax.googleapis.com/ajax/services/search/images?"
url = url.."v=1.0&rsz=5"
url = url.."&q="..URL.escape(text)
url = url.."&imgsz=small|medium|large"
--]]
if not google_config.cx then
print "cx not present in config file"
return nil
end
local url = "https://www.googleapis.com/customsearch/v1?"
url = url.."cx="..google_config.cx
url = url.."&searchType=image&safe=high"
url = url.."&q="..URL.escape(text)
if google_config.api_keys then
local i = math.random(#google_config.api_keys)
local api_key = google_config.api_keys[i]
if api_key then
url = url.."&key="..api_key
end
end
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)
local res, code = https.request(url) local res, code = https.request(url)
if code ~= 200 then return nil end
if code ~= 200 then local google = json:decode(res).items
print("HTTP Error code:", code)
return nil
end
local google = json:decode(res)
return google return google
end end
-- Returns only the useful google data to save on cache local function is_blacklisted(msg)
local function simple_google_table(google) local var = false
local new_table = {} for v,word in pairs(_blacklist) do
new_table.responseData = {} if string.find(string.lower(msg), string.lower(word)) then
new_table.responseDetails = google.responseDetails print("Wort steht auf der Blacklist!")
new_table.responseStatus = google.responseStatus var = true
new_table.responseData.results = {} break
local results = google.responseData.results
for k,result in pairs(results) do
new_table.responseData.results[k] = {}
new_table.responseData.results[k].unescapedUrl = result.unescapedUrl
new_table.responseData.results[k].url = result.url
end end
return new_table end
return var
end end
local function save_to_cache(query, data) local function show_blacklist()
-- Saves result on cache if not _blacklist[1] then
if string.len(query) <= 7 then return "Keine Wörter geblacklisted!\nBlackliste welche mit !imgblacklist add [Wort]"
local text_b64 = mime.b64(query) else
if not cache[text_b64] then local sort_alph = function( a,b ) return a < b end
-- local simple_google = simple_google_table(data) table.sort( _blacklist, sort_alph )
cache[text_b64] = data local blacklist = "Folgende Wörter stehen auf der Blacklist:\n"
for v,word in pairs(_blacklist) do
blacklist = blacklist..'- '..word..'\n'
end end
return blacklist
end end
end end
local function process_google_data(google, receiver, query) local function add_blacklist()
print('Blacklisting '..word..' - saving to redis set telegram:img_blacklist')
if not google or not google.items or #google.items == 0 then if redis:sismember("telegram:img_blacklist", word) == true then
local text = 'Image not found.' return '"'..word..'" steht schon auf der Blacklist.'
send_msg(receiver, text, ok_cb, false) else
return false redis:sadd("telegram:img_blacklist", word)
return '"'..word..'" blacklisted!'
end end
end
local i = math.random(#google.items) local function remove_blacklist()
local url = google.items[i].link print('De-blacklisting '..word..' - removing from redis set telegram:img_blacklist')
local old_timeout = https.TIMEOUT or 10 if redis:sismember("telegram:img_blacklist", word) == true then
http.TIMEOUT = 10 redis:srem("telegram:img_blacklist", word)
send_photo_from_url(receiver, url) return '"'..word..'" erfolgreich von der Blacklist gelöscht!'
http.TIMEOUT = old_timeout else
return '"'..word..'" steht nicht auf der Blacklist.'
save_to_cache(query, google) end
end
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')
end end
function run(msg, matches) function run(msg, matches)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
local text = matches[1] local text = matches[1]
local text_b64 = mime.b64(text) if matches[2] then word = string.lower(matches[2]) end
local cached = cache[text_b64]
if cached then _blacklist = redis:smembers("telegram:img_blacklist")
process_google_data(cached, receiver, text)
if text == "/imgblacklist show" then
if is_sudo(msg) then
return show_blacklist()
else else
local data = get_google_data(text) return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!"
process_google_data(data, receiver, text) 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."
end end
end end
return { return {
description = "", description = "Sucht Bild mit Google-API und versendet es (SafeSearch aktiv)",
usage = "", 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)"
},
patterns = { patterns = {
"^/img (.*)$" "^/img (.*)$",
"^(/imgblacklist show)$",
"^(/imgblacklist add) (.*)$",
"^(/imgblacklist remove) (.*)$"
}, },
run = run run = run
} }
end end

119
plugins/img_google_nsfw.lua Normal file
View File

@ -0,0 +1,119 @@
-- This is a proprietary plugin, property of Andreas Bielawski, (c) 2015 <andi (dot) b (at) outlook (dot) de>
-- DO NOT USE WITHOUT PERMISSION
do
local _blacklist
local function getNSFWImage(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)&q='..URL.escape(text)
local res, code = https.request(url)
if code ~= 200 then return nil end
local google = json:decode(res).items
return google
end
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
end
return var
end
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'
end
return blacklist
end
end
local function cache_nsfw_image(results, text)
local cache = {}
for v in pairs(results) do
table.insert(cache, results[v].link)
end
cache_data('img_google_nsfw', string.lower(text), cache, 1209600, 'set')
end
function run(msg, matches)
local receiver = get_receiver(msg)
local text = matches[1]
if matches[2] then word = string.lower(matches[2]) end
_blacklist = redis:smembers("telegram:img_blacklist")
print ('Checking if search contains blacklisted words: '..text)
if is_blacklisted(text) then
return "Vergiss es ._."
end
local hash = 'telegram:cache:img_google_nsfw:'..string.lower(text)
local results = redis:smembers(hash)
if not results[1] then
print('doing web request')
results = getNSFWImage(text)
if not results then
return "Kein Bild gefunden!"
end
cache_nsfw_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."
end
end
return {
description = "Sucht Bild mit Google-API und versendet es",
usage = {
"/img2 [Suchbegriff]"
},
patterns = {
"^/img2 (.*)$"
},
run = run
}
end