Lots of fixes, changes, and new goodies.

This commit is contained in:
Sean B. Palmer
2008-02-23 12:16:43 +00:00
parent 7931fab145
commit 2fb0058943
13 changed files with 94 additions and 40 deletions

View File

@@ -187,7 +187,7 @@ TimeZones.update(TZ1)
@deprecated
def f_time(self, origin, match, args):
""".t [ <timezone> ] - Returns the current time"""
tz = match.group(1) or 'GMT'
tz = match.group(2) or 'GMT'
# Personal time zones, because they're rad
if hasattr(self.config, 'timezones'):
@@ -196,7 +196,7 @@ def f_time(self, origin, match, args):
if People.has_key(tz):
tz = People[tz]
elif (not match.group(1)) and People.has_key(origin.nick):
elif (not match.group(2)) and People.has_key(origin.nick):
tz = People[origin.nick]
TZ = tz.upper()

View File

@@ -76,7 +76,7 @@ def etymology(word):
def f_etymology(self, origin, match, args):
word = match.group(2)
try: result = etymology(word)
try: result = etymology(word.encode('utf-8'))
except IOError:
msg = "Can't connect to etymonline.com (%s)" % (etyuri % word)
self.msg(origin.sender, msg)

View File

@@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import re, urllib
import re, urllib, urlparse
from htmlentitydefs import name2codepoint
import web
from tools import deprecated
@@ -66,7 +66,7 @@ def f_title(self, origin, match, args):
status = str(info[1])
info = info[0]
if status.startswith('3'):
uri = info['Location']
uri = urlparse.urljoin(uri, info['Location'])
else: break
redirects += 1

View File

@@ -13,8 +13,10 @@ def f_reload(phenny, input):
"""Reloads a module, for use by admins only."""
if not input.admin: return
name = match.group(2)
module = getattr(__import__('modules.' + name), name)
name = input.group(2)
try: module = getattr(__import__('modules.' + name), name)
except ImportError:
module = getattr(__import__('opt.' + name), name)
reload(module)
if hasattr(module, '__file__'):
@@ -23,8 +25,8 @@ def f_reload(phenny, input):
modified = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(mtime))
else: modified = 'unknown'
self.register(vars(module))
self.bind_commands()
phenny.register(vars(module))
phenny.bind_commands()
phenny.reply('%r (version: %s)' % (module, modified))
f_reload.name = 'reload'

View File

@@ -30,10 +30,14 @@ def search(query, n=1):
def result(query):
results = search(query)
return results['results'][0]['url']
if results['results']:
return results['results'][0]['url']
return None
def count(query):
results = search(query)
if not results['results']:
return '0'
return results['estimatedCount']
def formatnumber(n):
@@ -44,8 +48,11 @@ def formatnumber(n):
return ''.join(parts)
def g(phenny, input):
uri = result(input.group(2))
phenny.reply(uri)
query = input.group(2)
uri = result(query)
if uri:
phenny.reply(uri)
else: phenny.reply("No results found for '%s'." % query)
g.commands = ['g']
g.priority = 'high'
@@ -60,7 +67,7 @@ r_query = re.compile(
r'\+?"[^"\\]*(?:\\.[^"\\]*)*"|\[[^]\\]*(?:\\.[^]\\]*)*\]|\S+'
)
def compare(phenny, input):
def gcs(phenny, input):
queries = r_query.findall(input.group(2))
if len(queries) > 6:
return phenny.reply('Sorry, can only compare up to six things.')
@@ -76,7 +83,7 @@ def compare(phenny, input):
results = [(term, n) for (n, term) in reversed(sorted(results))]
reply = ', '.join('%s (%s)' % (t, formatnumber(n)) for (t, n) in results)
phenny.say(reply)
compare.commands = ['gco', 'comp']
gcs.commands = ['gcs']
if __name__ == '__main__':
print __doc__.strip()

View File

@@ -59,6 +59,10 @@ def f_remind(phenny, input):
# @@ Multiple comma-separated tellees? Cf. Terje, #swhack, 2006-04-15
verb, tellee, msg = input.groups()
verb = verb.encode('utf-8')
tellee = tellee.encode('utf-8')
msg = msg.encode('utf-8')
tellee_original = tellee.rstrip(',:;')
tellee = tellee.lower()

View File

@@ -67,21 +67,24 @@ def translate(phrase, lang, target='en'):
return None
def tr(phenny, input):
"""Translates a phrase, with an optional language hint."""
lang, phrase = input.groups()
phrase = phrase.encode('utf-8')
if (len(phrase) > 350) and (not phenny.admin(input.nick)):
return phenny.reply('Phrase must be under 350 characters.')
language = guess_language(phrase)
if language is None:
return phenny.reply('Unable to guess the language, sorry.')
else: language = lang.encode('utf-8')
if language != 'en':
translation = translate(phrase, language)
if translation is not None:
return phenny.reply(u'"%s" (%s)' % (translation, language))
translation = translation.decode('utf-8').encode('utf-8')
return phenny.reply('"%s" (%s)' % (translation, language))
error = "I think it's %s, but I can't translate that language."
error = "I think it's %s, which I can't translate."
return phenny.reply(error % language.title())
# Otherwise, it's English, so mangle it for fun
@@ -93,10 +96,11 @@ def tr(phenny, input):
return phenny.reply(u'"%s" (en-unmangled)' % phrase)
return phenny.reply("I think it's English already.")
# @@ or 'Why but that be English, sire.'
tr.doc = ('phenny: "<phrase>"? or phenny: <lang> "<phrase>"?',
'Translate <phrase>, optionally forcing the <lang> interpretation.')
tr.rule = ('$nick', ur'(?:([a-z]{2}) +)?["“](.+?)["”]\? *$')
tr.example = '$nickname: "mon chien"? or $nickname: fr "mon chien"?'
tr.priority = 'low'
# @@ mangle
if __name__ == '__main__':
print __doc__.strip()

View File

@@ -26,7 +26,7 @@ def json(text):
raise ValueError('Input must be serialised JSON.')
def location(name):
name = urllib.quote(name)
name = urllib.quote(name.encode('utf-8'))
uri = 'http://ws.geonames.org/searchJSON?q=%s&maxRows=1' % name
for i in xrange(10):
u = urllib.urlopen(uri)

View File

@@ -123,6 +123,8 @@ def wikipedia(term, last=False):
def wik(phenny, input):
origterm = input.groups()[1]
origterm = origterm.encode('utf-8')
term = urllib.unquote(origterm)
if not term:
return phenny.say(origin.sender, 'Maybe you meant ".wik Zen"?')