Neues Google Image Plugin

This commit is contained in:
Akamaru 2015-12-03 20:39:14 +01:00
parent 802d37f76e
commit a68a8cae18

View File

@ -1,166 +1,124 @@
do do
local mime = require("mime")
local _blacklist local google_config = load_from_file('data/google.lua')
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 res, code = https.request(url)
if code ~= 200 then
print("HTTP Error code:", code)
return nil
end
local function getGoogleImage(text)
local text = URL.escape(text)
local api = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&safe=active&q="
local res, code = http.request(api..text)
if code ~= 200 then return nil end
local google = json:decode(res) local google = json:decode(res)
return google
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
return data.results
end end
local function is_blacklisted(msg) -- Returns only the useful google data to save on cache
local var = false local function simple_google_table(google)
for v,word in pairs(_blacklist) do local new_table = {}
if string.find(string.lower(msg), string.lower(word)) then new_table.responseData = {}
print("Wort steht auf der Blacklist!") new_table.responseDetails = google.responseDetails
var = true new_table.responseStatus = google.responseStatus
break new_table.responseData.results = {}
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
end return new_table
return var
end end
local function show_blacklist() local function save_to_cache(query, data)
if not _blacklist[1] then -- Saves result on cache
return "Keine Wörter geblacklisted!\nBlackliste welche mit /imgblacklist add [Wort]" if string.len(query) <= 7 then
else local text_b64 = mime.b64(query)
local sort_alph = function( a,b ) return a < b end if not cache[text_b64] then
table.sort( _blacklist, sort_alph ) -- local simple_google = simple_google_table(data)
local blacklist = "Folgende Wörter stehen auf der Blacklist:\n" cache[text_b64] = data
for v,word in pairs(_blacklist) do
blacklist = blacklist..'- '..word..'\n'
end end
return blacklist
end end
end end
local function add_blacklist() local function process_google_data(google, receiver, query)
print('Blacklisting '..word..' - saving to redis set telegram:img_blacklist')
if redis:sismember("telegram:img_blacklist", word) == true then if not google or not google.items or #google.items == 0 then
return '"'..word..'" steht schon auf der Blacklist.' local text = 'Image not found.'
else send_msg(receiver, text, ok_cb, false)
redis:sadd("telegram:img_blacklist", word) return false
return '"'..word..'" blacklisted!'
end end
local i = math.random(#google.items)
local url = google.items[i].link
local old_timeout = https.TIMEOUT or 10
http.TIMEOUT = 10
send_photo_from_url(receiver, url)
http.TIMEOUT = old_timeout
save_to_cache(query, google)
end end
local function remove_blacklist() function run(msg, matches)
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
local function run(msg, matches)
local receiver = get_receiver(msg) local receiver = get_receiver(msg)
local text = matches[1] local text = matches[1]
if matches[2] then word = string.lower(matches[2]) end local text_b64 = mime.b64(text)
local cached = cache[text_b64]
_blacklist = redis:smembers("telegram:img_blacklist") if cached then
process_google_data(cached, receiver, text)
if text == "/imgblacklist show" then
if is_sudo(msg) then
return show_blacklist()
else else
return "Du bist kein Superuser. Dieser Vorfall wird gemeldet!" local data = get_google_data(text)
end process_google_data(data, receiver, text)
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 del" and word == nil then
return "Benutzung: /imgblacklist del [Wort]"
elseif text == "/imgblacklist del" 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 results = getGoogleImage(text)
if not results then
return "Kein Bild gefunden!"
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
url = results[i].url;
print("Bilder-URL: ", url)
if string.ends(url, ".gif") then
failed = not send_document_from_url(receiver, url, nil, nil, true)
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)
end
nofTries = nofTries + 1
i = i+1
if i > #results then
i = 1
end
end
if failed then
return "Fehler beim Herunterladen eines Bildes."
else
return "Source: "..url
end end
end end
return { return {
description = "Sucht Bild mit Google-API und versendet es (SafeSearch aktiv)", description = "",
usage = { usage = "",
"/img [Suchbegriff]",
"/imgblacklist show: Zeigt Blacklist (nur Superuser)",
"/imgblacklist add [Wort]: Fügt Wort der Blacklist hinzu (nur Superuser)",
"/imgblacklist del [Wort]: Entfernt Wort aus der Blacklist (nur Superuser)"
},
patterns = { patterns = {
"^/img (.*)$", "^/img (.*)$"
"^(/imgblacklist show)$",
"^(/imgblacklist add) (.*)$",
"^(/imgblacklist del) (.*)$"
}, },
run = run run = run
} }
end end