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-04-11 06:04:47 +02:00
|
|
|
local utilities = require('utilities')
|
|
|
|
|
|
|
|
wikipedia.command = 'wikipedia <query>'
|
|
|
|
wikipedia.doc = [[```
|
2016-01-08 14:44:37 +01:00
|
|
|
/wikipedia <query>
|
|
|
|
Returns an article from Wikipedia.
|
|
|
|
Aliases: /w, /wiki
|
|
|
|
```]]
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function wikipedia:init()
|
|
|
|
wikipedia.triggers = utilities.triggers(self.info.username):t('wikipedia', true):t('wiki', true):t('w', true).table
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
local get_title = function(search)
|
2016-05-29 19:08:39 +02:00
|
|
|
for _,v in ipairs(search) do
|
2016-05-13 22:09:40 +02:00
|
|
|
if not v.snippet:match('may refer to:') then
|
|
|
|
return v.title
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function wikipedia:action(msg)
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
-- Get the query. If it's not in the message, check the replied-to message.
|
|
|
|
-- If those don't exist, send the help text.
|
2016-04-08 23:12:02 +02:00
|
|
|
local input = utilities.input(msg.text)
|
2015-08-18 11:55:25 +02:00
|
|
|
if not input then
|
2015-11-25 03:22:04 +01:00
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
else
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, wikipedia.doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
-- This kinda sucks, but whatever.
|
|
|
|
input = input:gsub('#', ' sharp')
|
|
|
|
|
|
|
|
-- Disclaimer: These variables will be reused.
|
|
|
|
local jstr, res, jdat
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
-- All pretty standard from here.
|
|
|
|
local search_url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch='
|
|
|
|
|
|
|
|
jstr, res = HTTPS.request(search_url .. URL.escape(input))
|
2015-08-18 11:55:25 +02:00
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
jdat = JSON.decode(jstr)
|
|
|
|
if jdat.query.searchinfo.totalhits == 0 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.results)
|
2016-01-08 14:44:37 +01:00
|
|
|
return
|
|
|
|
end
|
2016-05-13 22:09:40 +02:00
|
|
|
|
|
|
|
local title = get_title(jdat.query.search)
|
|
|
|
if not title then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.results)
|
2015-11-26 11:34:16 +01:00
|
|
|
return
|
|
|
|
end
|
2016-02-26 17:46:14 +01:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
local res_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
jstr, res = HTTPS.request(res_url .. URL.escape(title))
|
2015-08-18 11:55:25 +02:00
|
|
|
if res ~= 200 then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
local _
|
2015-11-25 03:22:04 +01:00
|
|
|
local text = JSON.decode(jstr).query.pages
|
2016-04-11 06:04:47 +02:00
|
|
|
_, text = next(text)
|
2015-08-23 08:46:34 +02:00
|
|
|
if not text then
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_reply(self, msg, self.config.errors.results)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2016-04-11 06:04:47 +02:00
|
|
|
else
|
|
|
|
text = text.extract
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
-- Remove needless bits from the article, take only the first paragraph.
|
2016-02-20 11:57:57 +01:00
|
|
|
text = text:gsub('</?.->', '')
|
|
|
|
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
|
2015-08-18 11:55:25 +02:00
|
|
|
|
2016-05-13 22:09:40 +02:00
|
|
|
-- This block can be annoying to read.
|
|
|
|
-- We use the initial title to make the url for later use. Then we remove
|
|
|
|
-- the extra bits that won't be in the article. We determine whether the
|
|
|
|
-- first part of the text is the title, and if so, we embolden that.
|
|
|
|
-- Otherwise, we prepend the text with a bold title. Then we append a "Read
|
|
|
|
-- More" link.
|
|
|
|
local url = 'https://en.wikipedia.org/wiki/' .. URL.escape(title)
|
2016-01-08 14:44:37 +01:00
|
|
|
title = title:gsub('%(.+%)', '')
|
2016-05-13 22:09:40 +02:00
|
|
|
local output
|
|
|
|
if string.match(text:sub(1, title:len()), title) then
|
|
|
|
output = '*' .. title .. '*' .. text:sub(title:len()+1)
|
2016-01-08 14:44:37 +01:00
|
|
|
else
|
2016-05-13 22:09:40 +02:00
|
|
|
output = '*' .. title:gsub('%(.+%)', '') .. '*\n' .. text:gsub('%[.+%]','')
|
2016-01-08 14:44:37 +01:00
|
|
|
end
|
2016-05-13 22:09:40 +02:00
|
|
|
output = output .. '\n[Read more.](' .. url:gsub('%)', '\\)') .. ')'
|
2016-01-08 14:44:37 +01:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, true, nil, true)
|
2016-02-20 11:07:20 +01:00
|
|
|
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return wikipedia
|