2015-11-12 17:42:03 +01:00
|
|
|
function googlethat(query)
|
|
|
|
local BASE_URL = 'https://www.googleapis.com/customsearch/v1'
|
|
|
|
local apikey = cred_data.google_apikey
|
|
|
|
local cseid = cred_data.google_cse_id
|
|
|
|
local number = 5 -- Set number of results
|
|
|
|
|
|
|
|
local api = BASE_URL.."/?key="..apikey.."&cx="..cseid.."&gl=de&num="..number.."&safe=medium&fields=searchInformation%28formattedSearchTime,formattedTotalResults%29,items%28title,link%29&"
|
2015-01-11 13:20:02 +01:00
|
|
|
local parameters = "q=".. (URL.escape(query) or "")
|
|
|
|
|
|
|
|
-- Do the request
|
|
|
|
local res, code = https.request(api..parameters)
|
2015-11-12 17:42:03 +01:00
|
|
|
if code ~=200 then return nil end
|
2015-01-11 13:20:02 +01:00
|
|
|
local data = json:decode(res)
|
2015-11-12 17:42:03 +01:00
|
|
|
if data.searchInformation.formattedTotalResults == "0" then return nil end
|
2015-04-26 19:16:44 +02:00
|
|
|
|
2015-11-12 17:42:03 +01:00
|
|
|
local results={}
|
|
|
|
for key,result in ipairs(data.items) do
|
2015-04-26 19:16:44 +02:00
|
|
|
table.insert(results, {
|
2015-11-12 17:42:03 +01:00
|
|
|
result.title,
|
|
|
|
result.link
|
2015-04-26 19:16:44 +02:00
|
|
|
})
|
2015-01-11 13:20:02 +01:00
|
|
|
end
|
2015-11-12 17:42:03 +01:00
|
|
|
|
|
|
|
local stats = data.searchInformation.formattedTotalResults..' Ergebnisse, gefunden in '..data.searchInformation.formattedSearchTime..' Sekunden'
|
|
|
|
return results, stats
|
2015-01-11 13:20:02 +01:00
|
|
|
end
|
|
|
|
|
2015-11-12 17:42:03 +01:00
|
|
|
function stringlinks(results, stats)
|
2015-01-11 13:20:02 +01:00
|
|
|
local stringresults=""
|
|
|
|
for key,val in ipairs(results) do
|
|
|
|
stringresults=stringresults..val[1].." - "..val[2].."\n"
|
|
|
|
end
|
2015-11-12 17:42:03 +01:00
|
|
|
return stringresults..stats
|
2015-01-11 13:20:02 +01:00
|
|
|
end
|
|
|
|
|
2015-11-12 17:42:03 +01:00
|
|
|
function run(msg, matches)
|
|
|
|
local results, stats = googlethat(matches[1])
|
|
|
|
if results == nil then
|
|
|
|
return 'Nichts gefunden!'
|
|
|
|
else
|
|
|
|
return stringlinks(results, stats)
|
|
|
|
end
|
2015-01-11 13:20:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return {
|
2015-11-12 17:42:03 +01:00
|
|
|
description = "Durchsucht Google",
|
|
|
|
usage = "/google [Suchbegriff]: Durchsucht Google",
|
|
|
|
patterns = {
|
|
|
|
"^/[Gg][Oo][Oo][Gg][Ll][Ee] (.*)$",
|
|
|
|
"^%.[Gg][Oo][Oo][Gg][Ll][Ee] (.*)$"
|
|
|
|
},
|
|
|
|
run = run
|
2015-05-27 18:55:36 +02:00
|
|
|
}
|