2016-04-11 06:04:47 +02:00
|
|
|
local gSearch = {}
|
|
|
|
|
|
|
|
local HTTPS = require('ssl.https')
|
|
|
|
local URL = require('socket.url')
|
2016-04-15 21:07:23 +02:00
|
|
|
local JSON = require('dkjson')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-06-18 18:32:50 +02:00
|
|
|
gSearch.command = 'google <Suchbegriff>'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
function gSearch:init(config)
|
|
|
|
gSearch.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('g', true):t('google', true):t('gnsfw', true).table
|
2016-06-18 18:32:50 +02:00
|
|
|
gSearch.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[google* _<Suchbegriff>_: Sendet Suchergebnisse von Google
|
|
|
|
Alias: _]]..config.cmd_pat..[[g_]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function gSearch:googlethat(query, config)
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/customsearch/v1'
|
2016-06-11 14:52:06 +02:00
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
local cseid = cred_data.google_cse_id
|
2016-06-11 14:46:41 +02:00
|
|
|
local number = 5 -- Set number of results
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
local api = BASE_URL.."/?key="..apikey.."&cx="..cseid.."&gl=de&num="..number.."&safe=medium&fields=searchInformation%28formattedSearchTime,formattedTotalResults%29,items%28title,link,displayLink%29&"
|
|
|
|
local parameters = "q=".. (URL.escape(query) or "")
|
|
|
|
-- Do the request
|
|
|
|
local res, code = HTTPS.request(api..parameters)
|
|
|
|
if code == 403 then
|
2016-06-21 23:23:11 +02:00
|
|
|
return '403'
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
if code ~= 200 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local data = JSON.decode(res)
|
|
|
|
if data.searchInformation.formattedTotalResults == "0" then return nil end
|
|
|
|
|
|
|
|
local results={}
|
|
|
|
for key,result in ipairs(data.items) do
|
|
|
|
table.insert(results, {
|
|
|
|
result.title,
|
|
|
|
result.link,
|
|
|
|
result.displayLink
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
local stats = data.searchInformation.formattedTotalResults..' Ergebnisse, gefunden in '..data.searchInformation.formattedSearchTime..' Sekunden'
|
|
|
|
return results, stats
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function gSearch:stringlinks(results, stats)
|
|
|
|
local stringresults=""
|
|
|
|
for key,val in ipairs(results) do
|
|
|
|
stringresults=stringresults.."["..val[1].."]("..val[2]..") - `"..val[3].."`\n"
|
|
|
|
end
|
|
|
|
return stringresults..stats
|
|
|
|
end
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
function gSearch:action(msg, config)
|
|
|
|
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
|
2015-11-25 03:22:04 +01:00
|
|
|
else
|
2016-06-11 14:46:41 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, gSearch.doc, true, msg.message_id, true)
|
|
|
|
return
|
2016-01-08 14:44:37 +01:00
|
|
|
end
|
2016-06-11 14:46:41 +02:00
|
|
|
end
|
|
|
|
|
2016-06-21 23:23:11 +02:00
|
|
|
local results, stats = gSearch:googlethat(input, onfig)
|
|
|
|
if results == '403' then
|
|
|
|
utilities.send_reply(self, msg, config.errors.quotaexceeded)
|
|
|
|
return
|
|
|
|
end
|
2016-06-11 14:46:41 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, gSearch:stringlinks(results, stats), true, nil, true)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return gSearch
|