*quietly adds a file that should have been in the tree since 9839a*
[halbot.git] / web.py
blob7a8c4815158f648467de33267e70a31b81f86dd2
1 import html, perms
2 from basic_commands import reply, troggie
3 from globals import commands
4 from throttle import throttle
6 web_throttle = throttle("web")
8 def title(who, where, args, all=False):
9 "$title <url>: Asks Hal to fetch the title of a given URL. Not very useful in a channel, where it's done implicitly whenever someone says a URL."
10 if html.non_webpage(args):
11 return
12 try:
13 reply(who, where, "[%s]" % (html.get_title(args)), all)
14 except Exception, e:
15 #if not html.is_webpage(args):
16 print e
17 reply(who, where, "Error retrieving URL '%s'." % args, all)
18 title = troggie(title)
19 title = throttle("web", quiet=True)(title)
20 commands['title'] = (perms.voice, title)
22 def title_implicit(who, where, args):
23 "Implicit version of $title."
24 title(who, where, args, all=True)
25 commands['title implicit'] = (perms.voice, title_implicit)
27 def calc(who, where, args):
28 "$calc <math>: Asks Google's calculator to do some math for you."
29 try:
30 reply(who, where, "Google says: %s" % html.google_calc(args), all=True)
31 except Exception, e:
32 print e
33 reply(who, where, "Ewwww... Google barfed on me.", all=True)
34 #calc = troggie(calc)
35 calc = web_throttle(calc)
36 commands['calc'] = (perms.voice, calc)
38 def google(who, where, args):
39 "$google <search>: Asks Google for its first result for a given search."
40 try:
41 url = html.first_google(args)
42 reply(who, where, "Google says: %s [%s]." % (url, html.get_title(url)), all=True)
43 except Exception, e:
44 print e
45 reply(who, where, "Ewwww... Google barfed on me.", all=True)
46 #google = troggie(google)
47 google = web_throttle(google)
48 commands['google'] = (perms.voice, google)
50 def jig(who, where, args):
51 "$jig <search>: Ask Google for its first result for a given search, restricted to Jayisgames."
52 google(who, where, args + " site:jayisgames.com")
53 #jig = troggie(jig)
54 commands['jig'] = (perms.voice, jig)
56 def wp(who, where, args):
57 "$wp <search> or $wikipedia <search>: Asks Google for its first result for a given search, restricted to Wikipedia."
58 google(who, where, args + " site:en.wikipedia.org")
59 commands['wp'] = (perms.voice, wp)
60 commands['wikipedia'] = (perms.voice, wp)
62 def youtube(who, where, args):
63 "$youtube <search>: Asks Google for its first result for a given search, restricted to YouTube."
64 google(who, where, args + " site:youtube.com")
65 commands['youtube'] = (perms.voice, youtube)
67 def onr(who, where, args, comic=None):
68 "$onr <search>: Searches OhNoRobot.com, returns the first result."
69 try:
70 url = html.first_onr(args, comic)
71 reply(who, where, "OhNoRobot.com says: %s [%s]." % (url, html.get_title(url)), all=True)
72 except Exception, e:
73 print e
74 reply(who, where, "Oh no! No luck.", all=True)
75 onr = web_throttle(onr)
76 commands['onr'] = (perms.voice, onr)
78 def xkcd(who, where, args):
79 "$xkcd <search>: Searches XKCD comics with OhNoRobot.com."
80 onr(who, where, args, 56)
81 commands['xkcd'] = (perms.voice, xkcd)
83 def weather(who, where, args):
84 "$weather <zipcode or location>: Asks Hal for the weather in a given location. Unfortunately, he doesn't know how yet."
85 reply(who, where, "I'm sorry, troggie isn't here, and I don't know how to do that.")
86 weather = troggie(weather)
87 commands['weather'] = (perms.voice, weather)
89 def reset_throttle(who, where, args):
90 "$reset-throttle: Resets Hal's throttling mechanism for web requests."
91 reply(who, where, "Throttling reset.")
92 reset_throttle = throttle("web", seconds_between=.1)(reset_throttle)
93 commands['reset-throttle'] = (perms.ircop, reset_throttle)