summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean B. Palmer <sbp@aldebaran.local>2011-06-17 16:49:37 +0100
committerSean B. Palmer <sbp@aldebaran.local>2011-06-17 16:49:37 +0100
commit12c8cd07f52883299ed628752b580462c31ce9f1 (patch)
treec79b62a988eb18221149fb47f873cadd1979c0fd
parent78ec2730460e8271e3a9d96056799785e6866f83 (diff)
downloadbot-12c8cd07f52883299ed628752b580462c31ce9f1.tar.gz
bot-12c8cd07f52883299ed628752b580462c31ce9f1.tar.bz2
bot-12c8cd07f52883299ed628752b580462c31ce9f1.zip
Search shim, and an encoding fix.
-rwxr-xr-xmodules/calc.py2
-rwxr-xr-xmodules/ping.py2
-rwxr-xr-xmodules/search.py15
-rwxr-xr-x[-rw-r--r--]modules/wikipedia.py0
4 files changed, 17 insertions, 2 deletions
diff --git a/modules/calc.py b/modules/calc.py
index 88ac814..9d632ca 100755
--- a/modules/calc.py
+++ b/modules/calc.py
@@ -90,7 +90,7 @@ c.commands = ['c']
c.example = '.c 5 + 3'
def py(phenny, input):
- query = input.group(2)
+ query = input.group(2).encode('utf-8')
uri = 'http://tumbolia.appspot.com/py/'
answer = web.get(uri + web.urllib.quote(query))
if answer:
diff --git a/modules/ping.py b/modules/ping.py
index 97e41e1..23219ac 100755
--- a/modules/ping.py
+++ b/modules/ping.py
@@ -11,7 +11,7 @@ def hello(phenny, input):
greeting = random.choice(('Hi', 'Hey', 'Hello'))
punctuation = random.choice(('', '!'))
phenny.say(greeting + ' ' + input.nick + punctuation)
-hello.rule = r'(?i)(hi|hello|hey) $nickname\b'
+hello.rule = r'(?i)(hi|hello|hey) $nickname[ \t]*$'
def interjection(phenny, input):
phenny.say(input.nick + '!')
diff --git a/modules/search.py b/modules/search.py
index 1067531..f99baf9 100755
--- a/modules/search.py
+++ b/modules/search.py
@@ -10,17 +10,31 @@ http://inamidst.com/phenny/
import re
import web
+class Grab(web.urllib.URLopener):
+ def __init__(self, *args):
+ self.version = 'Mozilla/5.0 (Phenny)'
+ web.urllib.URLopener.__init__(self, *args)
+ self.addheader('Referer', 'https://github.com/sbp/phenny')
+ def http_error_default(self, url, fp, errcode, errmsg, headers):
+ return web.urllib.addinfourl(fp, [headers, errcode], "http:" + url)
+
def search(query):
"""Search using AjaxSearch, and return its JSON."""
uri = 'http://ajax.googleapis.com/ajax/services/search/web'
args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8'))
+ handler = web.urllib._urlopener
+ web.urllib._urlopener = Grab()
bytes = web.get(uri + args)
+ web.urllib._urlopener = handler
return web.json(bytes)
def result(query):
results = search(query)
try: return results['responseData']['results'][0]['unescapedUrl']
except IndexError: return None
+ except TypeError:
+ print results
+ return False
def count(query):
results = search(query)
@@ -48,6 +62,7 @@ def g(phenny, input):
if not hasattr(phenny.bot, 'last_seen_uri'):
phenny.bot.last_seen_uri = {}
phenny.bot.last_seen_uri[input.sender] = uri
+ elif uri is False: phenny.reply("Problem getting data from Google.")
else: phenny.reply("No results found for '%s'." % query)
g.commands = ['g']
g.priority = 'high'
diff --git a/modules/wikipedia.py b/modules/wikipedia.py
index b476ba3..b476ba3 100644..100755
--- a/modules/wikipedia.py
+++ b/modules/wikipedia.py