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

54 lines
1.6 KiB
Lua
Raw Normal View History

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
2016-01-31 19:08:46 +01:00
local api = BASE_URL.."/?key="..apikey.."&cx="..cseid.."&gl=de&num="..number.."&fields=searchInformation%28formattedSearchTime,formattedTotalResults%29,items%28title,link%29&"
print(api)
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-11-12 17:42:03 +01:00
local results={}
for key,result in ipairs(data.items) do
table.insert(results, {
2015-11-12 17:42:03 +01:00
result.title,
result.link
})
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",
2015-11-12 17:42:03 +01:00
patterns = {
"^#[Gg][Oo][Oo][Gg][Ll][Ee] (.*)$",
2015-11-12 17:42:03 +01:00
"^%.[Gg][Oo][Oo][Gg][Ll][Ee] (.*)$"
},
run = run
2015-05-27 18:55:36 +02:00
}