chiark / gitweb /
Found in /home/matthew/programming/irc/bot - live version, all files as found
[irc.git] / commands.py.rej
1 ***************
2 *** 133,138 ****
3               bot.automsg(public,nick,str(gsearch))
4           else: # we haven't found anything.
5              bot.automsg(public,nick,"No pages found.")
6       except IOError: # if the connection times out. This blocks. :(
7          bot.automsg(public,nick,"The web's broken. Waah!")
8   
9 --- 133,165 ----
10               bot.automsg(public,nick,str(gsearch))
11           else: # we haven't found anything.
12              bot.automsg(public,nick,"No pages found.")
13 +     except IOError: # if the connection times out. This blocks. :(
14 +        bot.automsg(public,nick,"The web's broken. Waah!")
15
16 + # Look up the definition of something using google
17 + def defineq(bot, cmd, nick, conn, public):
18 +     cmdrest = string.join(cmd.split()[1:])
19 +     targ = ("http://www.google.com/search?q=define%%3A%s&ie=utf-8&oe=utf-8"
20 +             % urllib.quote_plus(cmdrest))
21 +     try:
22 +         # Just slurp everything into a string
23 +         defnpage = urllib.urlopen(targ).read()
24 +         # For definitions we really do have to parse the HTML, sadly.
25 +         # This is of course going to be a bit fragile. We first look for
26 +         # 'Definitions of %s on the Web' -- if this isn't present we
27 +         # assume we have the 'no definitions found page'.
28 +         # The first defn starts after the following <p> tag.
29 +         # Following that we assume that each definition is all the non-markup
30 +         # before a <br> tag. Currently we just dump out the first definition.
31 +         match = re.search(r"Definitions of <b>.*?</b> on the Web.*?<p>\s*([^>]*)<br>",defnpage,re.MULTILINE)
32 +         if match == None:
33 +            bot.automsg(public,nick,"Some things defy definition.")
34 +         else:
35 +            # We assume google has truncated the definition for us so this
36 +            # won't flood the channel with text...
37 +            defn = " ".join(match.group(1).split("\n"));
38 +            bot.automsg(public,nick,defn)
39
40       except IOError: # if the connection times out. This blocks. :(
41          bot.automsg(public,nick,"The web's broken. Waah!")
42