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

125 lines
2.9 KiB
Lua
Raw Normal View History

2015-02-22 22:34:10 +01:00
do
2015-12-03 20:39:14 +01:00
local mime = require("mime")
2015-02-22 22:34:10 +01:00
2015-12-03 20:39:14 +01:00
local google_config = load_from_file('data/google.lua')
local cache = {}
2015-11-12 17:42:03 +01:00
2015-12-03 20:39:14 +01:00
--[[
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"
--]]
2014-11-04 16:09:08 +01:00
2015-12-03 20:39:14 +01:00
if not google_config.cx then
print "cx not present in config file"
2015-02-22 22:34:10 +01:00
return nil
2014-11-04 16:09:08 +01:00
end
2015-02-22 22:34:10 +01:00
2015-12-03 20:39:14 +01:00
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
2015-04-07 22:58:33 +02:00
end
2015-12-03 20:39:14 +01:00
local res, code = https.request(url)
if code ~= 200 then
print("HTTP Error code:", code)
2015-04-07 22:58:33 +02:00
return nil
end
2015-12-03 20:39:14 +01:00
local google = json:decode(res)
return google
2015-11-12 17:42:03 +01:00
end
2015-12-03 20:39:14 +01:00
-- Returns only the useful google data to save on cache
local function simple_google_table(google)
local new_table = {}
new_table.responseData = {}
new_table.responseDetails = google.responseDetails
new_table.responseStatus = google.responseStatus
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
2015-11-12 17:42:03 +01:00
end
2015-12-03 20:39:14 +01:00
return new_table
2015-11-12 17:42:03 +01:00
end
2015-12-03 20:39:14 +01:00
local function save_to_cache(query, data)
-- Saves result on cache
if string.len(query) <= 7 then
local text_b64 = mime.b64(query)
if not cache[text_b64] then
-- local simple_google = simple_google_table(data)
cache[text_b64] = data
2015-11-12 17:42:03 +01:00
end
end
end
2015-12-03 20:39:14 +01:00
local function process_google_data(google, receiver, query)
2014-11-04 16:09:08 +01:00
2015-12-03 20:39:14 +01:00
if not google or not google.items or #google.items == 0 then
local text = 'Image not found.'
send_msg(receiver, text, ok_cb, false)
return false
2015-11-12 17:42:03 +01:00
end
2015-12-03 20:39:14 +01:00
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)
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-03 20:39:14 +01:00
local text_b64 = mime.b64(text)
local cached = cache[text_b64]
if cached then
process_google_data(cached, receiver, text)
else
2015-12-03 20:39:14 +01:00
local data = get_google_data(text)
process_google_data(data, receiver, text)
end
2014-11-04 16:09:08 +01:00
end
return {
2015-12-03 20:39:14 +01:00
description = "",
usage = "",
2015-11-12 17:42:03 +01:00
patterns = {
2015-12-03 20:39:14 +01:00
"^/img (.*)$"
},
run = run
2014-11-04 16:09:08 +01:00
}
2015-12-03 20:39:14 +01:00
end