2016-04-11 06:04:47 +02:00
|
|
|
local wikipedia = {}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
wikipedia.command = 'wikipedia <query>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function wikipedia:init(config)
|
2016-08-14 04:46:18 +02:00
|
|
|
wikipedia.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('wikipedia', true):t('wiki', true):t('w', true).table
|
|
|
|
wikipedia.doc = config.cmd_pat .. [[wikipedia <query>
|
2016-01-08 14:44:37 +01:00
|
|
|
Returns an article from Wikipedia.
|
2016-07-25 11:03:35 +02:00
|
|
|
Aliases: ]] .. config.cmd_pat .. 'w, ' .. config.cmd_pat .. 'wiki'
|
2016-08-14 04:46:18 +02:00
|
|
|
wikipedia.search_url = 'https://' .. config.lang .. '.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch='
|
|
|
|
wikipedia.res_url = 'https://' .. config.lang .. '.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
|
|
|
|
wikipedia.art_url = 'https://' .. config.lang .. '.wikipedia.org/wiki/'
|
2016-05-13 22:09:40 +02:00
|
|
|
end
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function wikipedia:action(msg, config)
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
|
|
|
utilities.send_reply(self, msg, wikipedia.doc, true)
|
|
|
|
return
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local jstr, code = HTTPS.request(wikipedia.search_url .. URL.escape(input))
|
|
|
|
if code ~= 200 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local data = JSON.decode(jstr)
|
|
|
|
if data.query.searchinfo.totalhits == 0 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
2016-05-13 22:09:40 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local title
|
|
|
|
for _, v in ipairs(data.query.search) do
|
|
|
|
if not v.snippet:match('may refer to:') then
|
|
|
|
title = v.title
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not title then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
2016-02-26 17:46:14 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local res_jstr, res_code = HTTPS.request(wikipedia.res_url .. URL.escape(title))
|
|
|
|
if res_code ~= 200 then
|
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
|
|
|
return
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local _, text = next(JSON.decode(res_jstr).query.pages)
|
|
|
|
if not text then
|
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
|
|
|
return
|
|
|
|
end
|
2015-08-23 08:46:34 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
text = text.extract
|
|
|
|
-- Remove crap and take only the first paragraph.
|
|
|
|
text = text:gsub('</?.->', ''):gsub('%[.+%]', '')
|
|
|
|
local l = text:find('\n')
|
|
|
|
if l then
|
|
|
|
text = text:sub(1, l-1)
|
|
|
|
end
|
|
|
|
local url = wikipedia.art_url .. URL.escape(title)
|
|
|
|
title = utilities.html_escape(title)
|
|
|
|
-- If the beginning of the article is the title, embolden that.
|
|
|
|
-- Otherwise, we'll add a title in bold.
|
|
|
|
local short_title = title:gsub('%(.+%)', '')
|
|
|
|
local combined_text, count = text:gsub('^'..short_title, '<b>'..short_title..'</b>')
|
|
|
|
local body
|
|
|
|
if count == 1 then
|
|
|
|
body = combined_text
|
|
|
|
else
|
|
|
|
body = '<b>' .. title .. '</b>\n' .. text
|
|
|
|
end
|
|
|
|
local output = string.format(
|
|
|
|
'%s\n<a href="%s">Read more.</a>',
|
|
|
|
body,
|
|
|
|
utilities.html_escape(url)
|
|
|
|
)
|
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, 'html')
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return wikipedia
|