Created
August 27, 2010 01:54
-
-
Save andsve/552625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module("modules/wp/wp") | |
function parse_message(bot, msg) | |
local i,j,sender,chan,q = string.find(msg, ":(.-)!.- PRIVMSG (.-) :" .. bot.config.triggerprefix .. "wp (.+)") | |
if not (i == nil) then | |
if not (string.sub(chan, 1, 1) == "#") then | |
chan = sender | |
end | |
q = q:gsub(" ", "+") | |
local http = require("socket.http") | |
local request_body = "action=query&prop=revisions&rvprop=content&format=yaml&redirects&titles=" .. q | |
local b, c, h = http.request("http://en.wikipedia.org/w/api.php?" .. request_body) | |
if not (b == nil) then | |
-- try to find beginning of page | |
b = b:gsub("{%b{}}", "") | |
local i,j,summary = b:find("'%*': |%-%s*(.+)") | |
if i then | |
-- make it pretty | |
summary = summary:gsub("%[%[([^%]^|]+)|([^%]]+)%]%]", "%2") -- [[a|b]] -> b | |
summary = summary:gsub("%[%[([^%]]+)%]%]", "%1") -- [[a]] -> a | |
summary = summary:gsub("'+([^']+)'+", "%1") -- ''a'' -> a | |
summary = summary:gsub("%b<>", "") -- <...> -> | |
summary = summary:sub(1,500) | |
bot:say(chan, summary) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment