- gSearch: 403

- Wikipedia: Benutze altes Wiki-Plugin, fix #4
This commit is contained in:
Andreas Bielawski 2016-06-21 23:23:11 +02:00
parent 891a8f94c2
commit 526702a2aa
2 changed files with 161 additions and 82 deletions

View File

@ -25,8 +25,7 @@ function gSearch:googlethat(query, config)
-- Do the request -- Do the request
local res, code = HTTPS.request(api..parameters) local res, code = HTTPS.request(api..parameters)
if code == 403 then if code == 403 then
utilities.send_reply(self, msg, config.errors.quotaexceeded) return '403'
return
end end
if code ~= 200 then if code ~= 200 then
utilities.send_reply(self, msg, config.errors.connection) utilities.send_reply(self, msg, config.errors.connection)
@ -67,7 +66,11 @@ function gSearch:action(msg, config)
end end
end end
local results, stats = gSearch:googlethat(input, config) local results, stats = gSearch:googlethat(input, onfig)
if results == '403' then
utilities.send_reply(self, msg, config.errors.quotaexceeded)
return
end
utilities.send_message(self, msg.chat.id, gSearch:stringlinks(results, stats), true, nil, true) utilities.send_message(self, msg.chat.id, gSearch:stringlinks(results, stats), true, nil, true)
end end

View File

@ -1,112 +1,188 @@
local wikipedia = {} local wikipedia = {}
local HTTPS = require('ssl.https') local https = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('dkjson') local JSON = require('dkjson')
local socket = require('socket')
local utilities = require('otouto.utilities') local utilities = require('otouto.utilities')
wikipedia.command = 'wiki <Begriff>' wikipedia.command = 'wiki <Begriff>'
function wikipedia:init(config) function wikipedia:init(config)
wikipedia.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('wikipedia', true):t('wiki', true).table wikipedia.triggers = {
"^/[Ww]iki(%w+) (search) (.+)$",
"^/[Ww]iki (search) ?(.*)$",
"^/[Ww]iki(%w+) (.+)$",
"^/[Ww]iki ?(.*)$",
"(%w+).wikipedia.org/wiki/(.+)"
}
wikipedia.doc = [[* wikipedia.doc = [[*
]]..config.cmd_pat..[[wiki* _<Begriff>_: Gibt Wikipedia-Artikel aus ]]..config.cmd_pat..[[wiki* _<Begriff>_: Gibt Wikipedia-Artikel aus
Alias: ]]..config.cmd_pat..[[wikipedia]] Alias: ]]..config.cmd_pat..[[wikipedia]]
end end
local get_title = function(search) local decodetext
for _,v in ipairs(search) do do
if not v.snippet:match('steht für') then local char, gsub, tonumber = string.char, string.gsub, tonumber
return v.title local function _(hex) return char(tonumber(hex, 16)) end
function decodetext(s)
s = gsub(s, '%%(%x%x)', _)
return s
end end
end end
return false
end
function wikipedia:action(msg, config) 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",
}
-- Get the query. If it's not in the message, check the replied-to message. function wikipedia:getWikiServer(lang)
-- If those don't exist, send the help text. return string.format(server.wiki_server, lang or server.default_lang)
local input = utilities.input(msg.text) end
if not input then
if msg.reply_to_message and msg.reply_to_message.text then --[[
input = msg.reply_to_message.text -- 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 else
utilities.send_message(self, msg.chat.id, wikipedia.doc, true, msg.message_id, true) server.wiki_load_params.explaintext = plain and "" or nil
return for k,v in pairs(server.wiki_load_params) do
query = query .. k .. '=' .. v .. '&'
end end
parsed = URL.parse(wikipedia:getWikiServer(lang))
parsed.path = server.wiki_path
parsed.query = query .. "titles=" .. URL.escape(text)
end end
-- This kinda sucks, but whatever. -- HTTP request
input = input:gsub('#', ' sharp') request['url'] = URL.build(parsed)
request['method'] = 'GET'
request['sink'] = ltn12.sink.table(sink)
-- Disclaimer: These variables will be reused. local httpRequest = parsed.scheme == 'http' and http.request or https.request
local jstr, res, jdat local code, headers, status = socket.skip(1, httpRequest(request))
-- All pretty standard from here. if not headers or not sink then
local search_url = 'https://de.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch=' return nil
jstr, res = HTTPS.request(search_url .. URL.escape(input))
if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end end
jdat = JSON.decode(jstr) local content = table.concat(sink)
if jdat.query.searchinfo.totalhits == 0 then if content ~= "" then
utilities.send_reply(self, msg, config.errors.results) local ok, result = pcall(JSON.decode, content)
return if ok and result then
end return result
local title = get_title(jdat.query.search)
if not title then
utilities.send_reply(self, msg, config.errors.results)
return
end
local res_url = 'https://de.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
jstr, res = HTTPS.request(res_url .. URL.escape(title))
if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end
local _
local text = JSON.decode(jstr).query.pages
_, text = next(text)
if not text then
utilities.send_reply(self, msg, config.errors.results)
return
else else
text = text.extract return nil
end end
-- Remove needless bits from the article, take only the first paragraph.
text = text:gsub('</?.->', '')
local l = text:find('\n')
if l then
text = text:sub(1, l-1)
end
-- 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://de.wikipedia.org/wiki/' .. URL.escape(title)
title = title:gsub('%(.+%)', '')
local output
if string.match(text:sub(1, title:len()), title) then
output = '*' .. title .. '*' .. text:sub(title:len()+1)
else else
output = '*' .. title:gsub('%(.+%)', '') .. '*\n' .. text:gsub('%[.+%]','') return nil
end
end end
output = output .. '\n[Wikipedia - '..title..'](' .. url:gsub('%)', '\\)') .. ')'
utilities.send_message(self, msg.chat.id, output, true, nil, true) -- extract intro passage in wiki page
function wikipedia:wikintro(text, lang)
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)
return '*'..title.."*:\n"..page.extract.."\n[Auf Wikipedia ansehen](https://"..lang..".wikipedia.org/wiki/"..title_enc..")"
else
local text = text.." nicht gefunden"
return text
end
else
return "Ein Fehler ist aufgetreten."
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
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
utilities.send_reply(msg, self, wikipedia.doc)
return
end
local result
if search then
result = wikipedia:wikisearch(term, lang)
else
result = wikipedia:wikintro(term, lang)
end
utilities.send_reply(self, msg, result, true)
end end
return wikipedia return wikipedia