Oblique stuff and fixes.

master
Sean B. Palmer 2008-10-29 16:03:12 +00:00
parent b91de3886e
commit fe16d21212
5 changed files with 52 additions and 28 deletions

8
irc.py
View File

@ -52,10 +52,14 @@ class Bot(asynchat.async_chat):
def write(self, args, text=None):
# This is a safe version of __write
def safe(input):
input = input.replace('\n', '')
input = input.replace('\r', '')
return input.encode('utf-8')
try:
args = [arg.encode('utf-8') for arg in args]
args = [safe(arg) for arg in args]
if text is not None:
text = text.encode('utf-8')
text = safe(text)
self.__write(args, text)
except Exception, e: pass

View File

@ -96,8 +96,8 @@ TZ1 = {
}
TZ2 = {
'ACDT': -10.5,
'ACST': -9.5,
'ACDT': 10.5,
'ACST': 9.5,
'ADT': 3,
'AEDT': 11, # hmm
'AEST': 10, # hmm
@ -251,7 +251,7 @@ def yi(phenny, input):
raels = quadraels * 4
extraraels, remainder = divide(remainder, 432000)
if extraraels == 4:
return phenny.say('Yes!')
return phenny.say('Yes! PARTAI!')
else: phenny.say('Not yet...')
yi.commands = ['yi']
yi.priority = 'low'

View File

@ -71,7 +71,8 @@ def u(phenny, input):
if not arg:
return phenny.reply('You gave me zero length input.')
elif not arg.strip(' '):
return phenny.reply('%s SPACES' % len(arg))
if len(arg) > 1: return phenny.reply('%s SPACEs (U+0020)' % len(arg))
return phenny.reply('1 SPACE (U+0020)')
# @@ space
if set(arg.upper()) - set(

View File

@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import re, urllib, httplib, urlparse, time
import re, urllib, urllib2, httplib, urlparse, time
from htmlentitydefs import name2codepoint
import web
from tools import deprecated
@ -84,7 +84,11 @@ def f_title(self, origin, match, args):
try:
redirects = 0
while True:
info = web.head(uri)
req = urllib2.Request(uri, headers={'Accept':'text/html'})
u = urllib2.urlopen(req)
info = u.info()
u.close()
# info = web.head(uri)
if not isinstance(info, list):
status = '200'
@ -108,7 +112,7 @@ def f_title(self, origin, match, args):
self.msg(origin.sender, origin.nick + ": Document isn't HTML")
return
u = urllib.urlopen(uri)
u = urllib2.urlopen(req)
bytes = u.read(32768)
u.close()
@ -142,11 +146,15 @@ def f_title(self, origin, match, args):
title = r_entity.sub(e, title)
if title:
try: title.decode('iso-8859-1')
except: pass
else: title = title.decode('iso-8859-1').encode('utf-8')
try: title.decode('utf-8')
except:
try: title = title.decode('iso-8859-1').encode('utf-8')
except: title = title.decode('cp1252').encode('utf-8')
else: pass
else: title = '[The title is empty.]'
title = title.replace('\n', '')
title = title.replace('\r', '')
self.msg(origin.sender, origin.nick + ': ' + title)
else: self.msg(origin.sender, origin.nick + ': No title found')
f_title.commands = ['title']

View File

@ -7,18 +7,22 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import urllib
import re, urllib
import web
definitions = 'http://code.google.com/p/phenny-ws/wiki/ServiceDefinitions'
r_item = re.compile(r'(?i)<li>(.*?)</li>')
r_tag = re.compile(r'<[^>]+>')
def mappings(uri):
result = {}
bytes = web.get(uri)
for line in bytes.splitlines():
if not line.startswith('<li>'): continue
line = line.strip()
if not line.endswith('</li>'): continue
for item in r_item.findall(bytes):
item = r_tag.sub('', item).strip(' \t\r\n')
if not ' ' in item: continue
command, template = line[4:-5].split(' ', 1)
command, template = item.split(' ', 1)
if not template.startswith('http://'): continue
result[command] = template
return result
@ -26,32 +30,39 @@ def mappings(uri):
def o(phenny, input):
"""Call a webservice."""
text = input.group(2)
if (not o.services) or (text == 'refresh'):
if hasattr(phenny.config, 'services'):
services = phenny.config.services
else: services = 'http://swhack.jottit.com/services'
else: services = definitions
if (not o.services) or (text == 'refresh'):
old = o.services
o.services = mappings(services)
if text == 'refresh':
return phenny.reply('Okay, found %s services.' % len(o.services))
msg = 'Okay, found %s services.' % len(o.services)
added = set(o.services) - set(old)
if added:
msg += ' Added: ' + ', '.join(sorted(added)[:5])
if len(added) > 5: msg += ', &c.'
return phenny.reply(msg)
if not text:
return phenny.reply('Try %s for details.' % services)
if ' ' in text:
command, args = text.split(' ', 1)
else: command, args = text, ''
command = command.lower()
args = urllib.quote(args)
if o.services.has_key(command):
template = o.services[command]
template = template.replace('${args}', args)
template = template.replace('${nick}', input.nick)
uri = template.replace('${sender}', input.sender)
template = template.replace('${args}', urllib.quote(args.encode('utf-8')))
template = template.replace('${nick}', urllib.quote(input.nick))
uri = template.replace('${sender}', urllib.quote(input.sender))
bytes = web.get(uri)
lines = bytes.splitlines()
if lines:
phenny.say(lines[0])
phenny.say(lines[0][:350])
else: phenny.reply('Sorry, the service is broken.')
else: phenny.reply('Sorry, no such service. See %s' % services)
o.commands = ['o']