2016-04-11 06:04:47 +02:00
|
|
|
local wikipedia = {}
|
|
|
|
|
2016-06-11 14:46:41 +02:00
|
|
|
wikipedia.command = 'wiki <Begriff>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function wikipedia:init(config)
|
2016-06-21 23:23:11 +02:00
|
|
|
wikipedia.triggers = {
|
2016-07-13 14:33:52 +02:00
|
|
|
"^/[Ww]iki(%w+) (search) (.+)$",
|
|
|
|
"^/[Ww]iki (search) ?(.*)$",
|
|
|
|
"^/[Ww]iki(%w+) (.+)$",
|
|
|
|
"^/[Ww]iki ?(.*)$",
|
2017-01-14 14:29:05 +01:00
|
|
|
"(%w+).m.wikipedia.org/wiki/(.+)",
|
2016-07-13 14:33:52 +02:00
|
|
|
"(%w+).wikipedia.org/wiki/(.+)"
|
|
|
|
}
|
|
|
|
wikipedia.inline_triggers = {
|
|
|
|
"^wiki(%w+) (.+)",
|
|
|
|
"^wiki (.+)"
|
2016-06-21 23:23:11 +02:00
|
|
|
}
|
2016-06-11 14:46:41 +02:00
|
|
|
wikipedia.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[wiki* _<Begriff>_: Gibt Wikipedia-Artikel aus
|
2016-06-15 01:16:27 +02:00
|
|
|
Alias: ]]..config.cmd_pat..[[wikipedia]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-06-21 23:23:11 +02:00
|
|
|
local decodetext
|
|
|
|
do
|
|
|
|
local char, gsub, tonumber = string.char, string.gsub, tonumber
|
|
|
|
local function _(hex) return char(tonumber(hex, 16)) end
|
|
|
|
|
|
|
|
function decodetext(s)
|
|
|
|
s = gsub(s, '%%(%x%x)', _)
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local server = {
|
|
|
|
-- http://meta.wikimedia.org/wiki/List_of_Wikipedias
|
|
|
|
wiki_server = "https://%s.wikipedia.org",
|
|
|
|
wiki_path = "/w/api.php",
|
|
|
|
wiki_load_params = {
|
|
|
|
action = "query",
|
|
|
|
prop = "extracts",
|
|
|
|
format = "json",
|
|
|
|
exchars = 350,
|
|
|
|
exsectionformat = "plain",
|
|
|
|
explaintext = "",
|
|
|
|
redirects = ""
|
|
|
|
},
|
|
|
|
wiki_search_params = {
|
|
|
|
action = "query",
|
|
|
|
list = "search",
|
|
|
|
srlimit = 20,
|
|
|
|
format = "json",
|
|
|
|
},
|
|
|
|
default_lang = "de",
|
|
|
|
}
|
|
|
|
|
|
|
|
function wikipedia:getWikiServer(lang)
|
|
|
|
return string.format(server.wiki_server, lang or server.default_lang)
|
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
-- return decoded JSON table from Wikipedia
|
|
|
|
--]]
|
|
|
|
function wikipedia:loadPage(text, lang, intro, plain, is_search)
|
|
|
|
local request, sink = {}, {}
|
|
|
|
local query = ""
|
|
|
|
local parsed
|
|
|
|
|
|
|
|
if is_search then
|
|
|
|
for k,v in pairs(server.wiki_search_params) do
|
|
|
|
query = query .. k .. '=' .. v .. '&'
|
|
|
|
end
|
|
|
|
parsed = URL.parse(wikipedia:getWikiServer(lang))
|
|
|
|
parsed.path = server.wiki_path
|
|
|
|
parsed.query = query .. "srsearch=" .. URL.escape(text)
|
|
|
|
else
|
|
|
|
server.wiki_load_params.explaintext = plain and "" or nil
|
|
|
|
for k,v in pairs(server.wiki_load_params) do
|
|
|
|
query = query .. k .. '=' .. v .. '&'
|
|
|
|
end
|
|
|
|
parsed = URL.parse(wikipedia:getWikiServer(lang))
|
|
|
|
parsed.path = server.wiki_path
|
|
|
|
parsed.query = query .. "titles=" .. URL.escape(text)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- HTTP request
|
|
|
|
request['url'] = URL.build(parsed)
|
|
|
|
request['method'] = 'GET'
|
|
|
|
request['sink'] = ltn12.sink.table(sink)
|
|
|
|
|
|
|
|
local httpRequest = parsed.scheme == 'http' and http.request or https.request
|
|
|
|
local code, headers, status = socket.skip(1, httpRequest(request))
|
|
|
|
|
|
|
|
if not headers or not sink then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
local content = table.concat(sink)
|
|
|
|
if content ~= "" then
|
2016-07-31 21:29:44 +02:00
|
|
|
local ok, result = pcall(json.decode, content)
|
2016-06-21 23:23:11 +02:00
|
|
|
if ok and result then
|
|
|
|
return result
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2016-05-13 22:09:40 +02:00
|
|
|
end
|
|
|
|
|
2016-06-21 23:23:11 +02:00
|
|
|
-- extract intro passage in wiki page
|
2016-08-07 00:36:43 +02:00
|
|
|
function wikipedia:wikintro(text, lang, is_inline)
|
2016-06-21 23:23:11 +02:00
|
|
|
local text = decodetext(text)
|
|
|
|
local result = self:loadPage(text, lang, true, true)
|
|
|
|
|
|
|
|
if result and result.query then
|
|
|
|
|
|
|
|
local query = result.query
|
|
|
|
if query and query.normalized then
|
|
|
|
text = query.normalized[1].to or text
|
|
|
|
end
|
|
|
|
|
|
|
|
local page = query.pages[next(query.pages)]
|
|
|
|
|
|
|
|
if page and page.extract then
|
|
|
|
local lang = lang or "de"
|
|
|
|
local title = page.title
|
|
|
|
local title_enc = URL.escape(title)
|
2016-08-07 00:36:43 +02:00
|
|
|
if is_inline then
|
2016-08-07 00:53:21 +02:00
|
|
|
local result = '<b>'..title..'</b>:\n'..page.extract
|
2016-08-07 00:36:43 +02:00
|
|
|
local result = result:gsub('\n', '\\n')
|
|
|
|
local result = result:gsub('"', '\\"')
|
|
|
|
return title, result, '{"inline_keyboard":[[{"text":"Wikipedia aufrufen","url":"https://'..lang..'.wikipedia.org/wiki/'..title_enc..'"}]]}'
|
|
|
|
else
|
2016-08-07 00:53:21 +02:00
|
|
|
return '*'..title..'*:\n'..utilities.md_escape(page.extract), '{"inline_keyboard":[[{"text":"Artikel aufrufen","url":"https://'..lang..'.wikipedia.org/wiki/'..title_enc..'"}]]}'
|
2016-08-07 00:36:43 +02:00
|
|
|
end
|
2016-06-21 23:23:11 +02:00
|
|
|
else
|
2016-08-07 00:36:43 +02:00
|
|
|
if is_inline then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
local text = text.." nicht gefunden"
|
|
|
|
return text
|
|
|
|
end
|
2016-06-21 23:23:11 +02:00
|
|
|
end
|
|
|
|
else
|
2016-08-07 00:36:43 +02:00
|
|
|
if is_inline then
|
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return "Ein Fehler ist aufgetreten."
|
|
|
|
end
|
2016-06-21 23:23:11 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- search for term in wiki
|
|
|
|
function wikipedia:wikisearch(text, lang)
|
|
|
|
local result = wiki:loadPage(text, lang, true, true, true)
|
|
|
|
|
|
|
|
if result and result.query then
|
|
|
|
local titles = ""
|
|
|
|
for i,item in pairs(result.query.search) do
|
|
|
|
titles = titles .. "\n" .. item["title"]
|
|
|
|
end
|
|
|
|
titles = titles ~= "" and titles or "Keine Ergebnisse gefunden"
|
|
|
|
return titles
|
|
|
|
else
|
|
|
|
return "Ein Fehler ist aufgetreten."
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2016-02-20 11:07:20 +01:00
|
|
|
|
2016-07-13 14:33:52 +02:00
|
|
|
function wikipedia:snip_snippet(snippet)
|
|
|
|
local snippet = snippet:gsub("<span class%=\"searchmatch\">", "")
|
|
|
|
local snippet = snippet:gsub("</span>", "")
|
|
|
|
return snippet
|
|
|
|
end
|
|
|
|
|
|
|
|
function wikipedia:inline_callback(inline_query, config, matches)
|
|
|
|
if matches[2] then
|
|
|
|
lang = matches[1]
|
|
|
|
query = matches[2]
|
|
|
|
else
|
|
|
|
lang = 'de'
|
|
|
|
query = matches[1]
|
|
|
|
end
|
2016-08-07 00:36:43 +02:00
|
|
|
|
|
|
|
local search_url = 'https://'..lang..'.wikipedia.org/w/api.php?action=query&list=search&srsearch='..URL.escape(query)..'&format=json&prop=extracts&srprop=snippet&&srlimit=5'
|
|
|
|
local res, code = https.request(search_url)
|
2016-08-24 15:38:29 +02:00
|
|
|
if code ~= 200 then abort_inline_query(inline_query) return end
|
2016-07-31 21:29:44 +02:00
|
|
|
local data = json.decode(res).query
|
2016-07-13 14:33:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
local results = '['
|
2016-08-07 21:20:48 +02:00
|
|
|
local id = 700
|
2016-07-13 14:33:52 +02:00
|
|
|
for num in pairs(data.search) do
|
2016-08-07 00:36:43 +02:00
|
|
|
local title, result, keyboard = wikipedia:wikintro(data.search[num].title, lang, true)
|
2016-08-24 15:38:29 +02:00
|
|
|
if not title or not result or not keyboard then abort_inline_query(inline_query) return end
|
2016-08-07 21:20:48 +02:00
|
|
|
results = results..'{"type":"article","id":"'..id..'","title":"'..title..'","description":"'..wikipedia:snip_snippet(data.search[num].snippet)..'","url":"https://'..lang..'.wikipedia.org/wiki/'..URL.escape(title)..'","hide_url":true,"thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/wiki/logo.jpg","thumb_width":95,"thumb_height":86,"reply_markup":'..keyboard..',"input_message_content":{"message_text":"'..result..'","parse_mode":"HTML"}}'
|
|
|
|
id = id+1
|
2016-07-13 14:33:52 +02:00
|
|
|
if num < #data.search then
|
|
|
|
results = results..','
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local results = results..']'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_inline_query(inline_query, results, 3600)
|
2016-07-13 14:33:52 +02:00
|
|
|
end
|
|
|
|
|
2016-06-21 23:23:11 +02:00
|
|
|
function wikipedia:action(msg, config, matches)
|
|
|
|
local search, term, lang
|
|
|
|
if matches[1] == "search" then
|
|
|
|
search = true
|
|
|
|
term = matches[2]
|
|
|
|
lang = nil
|
|
|
|
elseif matches[2] == "search" then
|
|
|
|
search = true
|
|
|
|
term = matches[3]
|
|
|
|
lang = matches[1]
|
|
|
|
else
|
|
|
|
term = matches[2]
|
|
|
|
lang = matches[1]
|
|
|
|
end
|
|
|
|
if not term then
|
|
|
|
term = lang
|
|
|
|
lang = nil
|
|
|
|
end
|
|
|
|
if term == "" then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, wikipedia.doc, true)
|
2016-06-21 23:23:11 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local result
|
|
|
|
if search then
|
|
|
|
result = wikipedia:wikisearch(term, lang)
|
|
|
|
else
|
2016-07-09 14:19:42 +02:00
|
|
|
result, keyboard = wikipedia:wikintro(term, lang)
|
2016-06-21 23:23:11 +02:00
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, result, true, keyboard)
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return wikipedia
|