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 bindings = require('bindings')
|
|
|
|
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-04-11 06:04:47 +02:00
|
|
|
function wikipedia:action(msg)
|
2015-08-18 11:55:25 +02:00
|
|
|
|
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-04-11 06:04:47 +02:00
|
|
|
bindings.sendMessage(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
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local gurl = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q=site:wikipedia.org%20'
|
|
|
|
local wurl = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
|
|
|
|
|
|
|
|
local jstr, res = HTTPS.request(gurl .. URL.escape(input))
|
2015-08-18 11:55:25 +02:00
|
|
|
if res ~= 200 then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local jdat = JSON.decode(jstr)
|
2016-01-08 14:44:37 +01:00
|
|
|
if not jdat.responseData then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.config.errors.connection)
|
2016-01-08 14:44:37 +01:00
|
|
|
return
|
|
|
|
end
|
2015-11-26 11:34:16 +01:00
|
|
|
if not jdat.responseData.results[1] then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(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-02-26 05:14:25 +01:00
|
|
|
local url = jdat.responseData.results[1].unescapedUrl
|
2015-11-25 03:22:04 +01:00
|
|
|
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '')
|
|
|
|
|
2016-02-26 17:46:14 +01:00
|
|
|
-- 'https://en.wikipedia.org/wiki/':len() == 30
|
2016-02-26 05:14:25 +01:00
|
|
|
jstr, res = HTTPS.request(wurl .. url:sub(31))
|
2015-08-18 11:55:25 +02:00
|
|
|
if res ~= 200 then
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(self, msg, self.config.error.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-04-11 06:04:47 +02:00
|
|
|
bindings.sendReply(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-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-01-08 14:44:37 +01:00
|
|
|
title = title:gsub('%(.+%)', '')
|
2016-02-26 05:50:51 +01:00
|
|
|
local esctitle = title:gsub("[%^$()%%.%[%]*+%-?]","%%%1")
|
|
|
|
local output = text:gsub('%[.+%]',''):gsub(esctitle, '*%1*') .. '\n'
|
2016-01-08 14:44:37 +01:00
|
|
|
if url:find('%(') then
|
|
|
|
output = output .. url:gsub('_', '\\_')
|
|
|
|
else
|
|
|
|
output = output .. '[Read more.](' .. url .. ')'
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
bindings.sendMessage(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
|