Skip to content

Instantly share code, notes, and snippets.

@andsve
Created August 23, 2010 20:48
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/546307 to your computer and use it in GitHub Desktop.
Save andsve/546307 to your computer and use it in GitHub Desktop.
module("modules/define/define")
function parse_message(bot, msg)
local i,j,sender,chan,q = string.find(msg, ":(.-)!.- PRIVMSG (.-) :" .. tostring(bot.config.triggerprefix) .. "define (.+)")
if not (i == nil) then
if not (string.sub(chan, 1, 1) == "#") then
chan = sender
end
local http = require("socket.http")
local ltn12 = require("ltn12")
q = string.gsub(q, " ", "+")
local request_body = "q=define%3A" .. tostring(q)
local b, c, h = http.request("http://www.google.com/search?" .. request_body)
function get_html_translation_table()
local table = {}
table['"'] = """
table["'"] = "'"
table["&"] = "&"
table["<"] = "&lt;"
table[">"] = "&gt;"
table[" "] = "&nbsp;"
table["¡"] = "&iexcl;"
table["¢"] = "&cent;"
table["£"] = "&pound;"
table["¤"] = "&curren;"
table["¥"] = "&yen;"
table["¦"] = "&brvbar;"
table["§"] = "&sect;"
table["¨"] = "&uml;"
table["©"] = "&copy;"
table["ª"] = "&ordf;"
table["«"] = "&laquo;"
table["¬"] = "&not;"
table["­"] = "&shy;"
table["®"] = "&reg;"
table["¯"] = "&macr;"
table["°"] = "&deg;"
table["±"] = "&plusmn;"
table["²"] = "&sup2;"
table["³"] = "&sup3;"
table["´"] = "&acute;"
table["µ"] = "&micro;"
table["¶"] = "&para;"
table["·"] = "&middot;"
table["¸"] = "&cedil;"
table["¹"] = "&sup1;"
table["º"] = "&ordm;"
table["»"] = "&raquo;"
table["¼"] = "&frac14;"
table["½"] = "&frac12;"
table["¾"] = "&frac34;"
table["¿"] = "&iquest;"
table["×"] = "&times;"
table["÷"] = "&divide;"
table["?"] = "&#%d+;"
return table
end
local a,c,def = string.find(b, '.-Definitions of.-<ul type="disc" class=std><li>(.-)<.+')
if not (a == nil) then
local t = get_html_translation_table()
for k,v in pairs(t) do
def = string.gsub(def, v, k)
end
bot:say(chan, tostring(def))
else
bot:say(chan, "Definition not found.")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment