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)
|
|
|
|
wikipedia.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('wikipedia', true):t('wiki', true):t('w', true).table
|
2016-07-25 11:03:35 +02:00
|
|
|
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:26:44 +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:26:44 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
2015-08-18 11:55:25 +02:00
|
|
|
if not input then
|
2016-08-14 04:26:44 +02:00
|
|
|
utilities.send_reply(self, msg, wikipedia.doc, true)
|
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local jstr, code = HTTPS.request(wikipedia.search_url .. URL.escape(input))
|
|
|
|
if code ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local data = JSON.decode(jstr)
|
|
|
|
if data.query.searchinfo.totalhits == 0 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2016-01-08 14:44:37 +01:00
|
|
|
return
|
|
|
|
end
|
2016-05-13 22:09:40 +02:00
|
|
|
|
2016-08-14 04:26:44 +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
|
2016-05-13 22:09:40 +02:00
|
|
|
if not title then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2015-11-26 11:34:16 +01:00
|
|
|
return
|
|
|
|
end
|
2016-02-26 17:46:14 +01:00
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local res_jstr, res_code = HTTPS.request(wikipedia.res_url .. URL.escape(title))
|
|
|
|
if res_code ~= 200 then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
local _, text = next(JSON.decode(res_jstr).query.pages)
|
2015-08-23 08:46:34 +02:00
|
|
|
if not text then
|
2016-05-27 02:26:30 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.results)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
|
|
|
|
2016-08-14 04:26:44 +02:00
|
|
|
text = text.extract
|
|
|
|
-- Remove crap and take only the first paragraph.
|
|
|
|
text = text:gsub('</?.->', ''):gsub('%[.+%]', '')
|
2016-02-20 11:57:57 +01:00
|
|
|
local l = text:find('\n')
|
2015-08-23 08:46:34 +02:00
|
|
|
if l then
|
2016-02-23 22:32:34 +01:00
|
|
|
text = text:sub(1, l-1)
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
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
|
2016-01-08 14:44:37 +01:00
|
|
|
else
|
2016-08-14 04:26:44 +02:00
|
|
|
body = '<b>' .. title .. '</b>\n' .. text
|
2016-01-08 14:44:37 +01:00
|
|
|
end
|
2016-08-14 04:26:44 +02:00
|
|
|
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
|