Skip to content

Instantly share code, notes, and snippets.

@andsve
Created August 27, 2010 01:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andsve/552625 to your computer and use it in GitHub Desktop.
Save andsve/552625 to your computer and use it in GitHub Desktop.
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