Oblique stuff and fixes.
parent
b91de3886e
commit
fe16d21212
8
irc.py
8
irc.py
|
@ -52,10 +52,14 @@ class Bot(asynchat.async_chat):
|
||||||
|
|
||||||
def write(self, args, text=None):
|
def write(self, args, text=None):
|
||||||
# This is a safe version of __write
|
# This is a safe version of __write
|
||||||
|
def safe(input):
|
||||||
|
input = input.replace('\n', '')
|
||||||
|
input = input.replace('\r', '')
|
||||||
|
return input.encode('utf-8')
|
||||||
try:
|
try:
|
||||||
args = [arg.encode('utf-8') for arg in args]
|
args = [safe(arg) for arg in args]
|
||||||
if text is not None:
|
if text is not None:
|
||||||
text = text.encode('utf-8')
|
text = safe(text)
|
||||||
self.__write(args, text)
|
self.__write(args, text)
|
||||||
except Exception, e: pass
|
except Exception, e: pass
|
||||||
|
|
||||||
|
|
|
@ -96,8 +96,8 @@ TZ1 = {
|
||||||
}
|
}
|
||||||
|
|
||||||
TZ2 = {
|
TZ2 = {
|
||||||
'ACDT': -10.5,
|
'ACDT': 10.5,
|
||||||
'ACST': -9.5,
|
'ACST': 9.5,
|
||||||
'ADT': 3,
|
'ADT': 3,
|
||||||
'AEDT': 11, # hmm
|
'AEDT': 11, # hmm
|
||||||
'AEST': 10, # hmm
|
'AEST': 10, # hmm
|
||||||
|
@ -251,7 +251,7 @@ def yi(phenny, input):
|
||||||
raels = quadraels * 4
|
raels = quadraels * 4
|
||||||
extraraels, remainder = divide(remainder, 432000)
|
extraraels, remainder = divide(remainder, 432000)
|
||||||
if extraraels == 4:
|
if extraraels == 4:
|
||||||
return phenny.say('Yes!')
|
return phenny.say('Yes! PARTAI!')
|
||||||
else: phenny.say('Not yet...')
|
else: phenny.say('Not yet...')
|
||||||
yi.commands = ['yi']
|
yi.commands = ['yi']
|
||||||
yi.priority = 'low'
|
yi.priority = 'low'
|
||||||
|
|
|
@ -71,7 +71,8 @@ def u(phenny, input):
|
||||||
if not arg:
|
if not arg:
|
||||||
return phenny.reply('You gave me zero length input.')
|
return phenny.reply('You gave me zero length input.')
|
||||||
elif not arg.strip(' '):
|
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
|
# @@ space
|
||||||
if set(arg.upper()) - set(
|
if set(arg.upper()) - set(
|
||||||
|
|
|
@ -7,7 +7,7 @@ Licensed under the Eiffel Forum License 2.
|
||||||
http://inamidst.com/phenny/
|
http://inamidst.com/phenny/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re, urllib, httplib, urlparse, time
|
import re, urllib, urllib2, httplib, urlparse, time
|
||||||
from htmlentitydefs import name2codepoint
|
from htmlentitydefs import name2codepoint
|
||||||
import web
|
import web
|
||||||
from tools import deprecated
|
from tools import deprecated
|
||||||
|
@ -84,7 +84,11 @@ def f_title(self, origin, match, args):
|
||||||
try:
|
try:
|
||||||
redirects = 0
|
redirects = 0
|
||||||
while True:
|
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):
|
if not isinstance(info, list):
|
||||||
status = '200'
|
status = '200'
|
||||||
|
@ -108,7 +112,7 @@ def f_title(self, origin, match, args):
|
||||||
self.msg(origin.sender, origin.nick + ": Document isn't HTML")
|
self.msg(origin.sender, origin.nick + ": Document isn't HTML")
|
||||||
return
|
return
|
||||||
|
|
||||||
u = urllib.urlopen(uri)
|
u = urllib2.urlopen(req)
|
||||||
bytes = u.read(32768)
|
bytes = u.read(32768)
|
||||||
u.close()
|
u.close()
|
||||||
|
|
||||||
|
@ -142,11 +146,15 @@ def f_title(self, origin, match, args):
|
||||||
title = r_entity.sub(e, title)
|
title = r_entity.sub(e, title)
|
||||||
|
|
||||||
if title:
|
if title:
|
||||||
try: title.decode('iso-8859-1')
|
try: title.decode('utf-8')
|
||||||
except: pass
|
except:
|
||||||
else: title = title.decode('iso-8859-1').encode('utf-8')
|
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.]'
|
else: title = '[The title is empty.]'
|
||||||
|
|
||||||
|
title = title.replace('\n', '')
|
||||||
|
title = title.replace('\r', '')
|
||||||
self.msg(origin.sender, origin.nick + ': ' + title)
|
self.msg(origin.sender, origin.nick + ': ' + title)
|
||||||
else: self.msg(origin.sender, origin.nick + ': No title found')
|
else: self.msg(origin.sender, origin.nick + ': No title found')
|
||||||
f_title.commands = ['title']
|
f_title.commands = ['title']
|
||||||
|
|
|
@ -7,18 +7,22 @@ Licensed under the Eiffel Forum License 2.
|
||||||
http://inamidst.com/phenny/
|
http://inamidst.com/phenny/
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import urllib
|
import re, urllib
|
||||||
import web
|
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):
|
def mappings(uri):
|
||||||
result = {}
|
result = {}
|
||||||
bytes = web.get(uri)
|
bytes = web.get(uri)
|
||||||
for line in bytes.splitlines():
|
for item in r_item.findall(bytes):
|
||||||
if not line.startswith('<li>'): continue
|
item = r_tag.sub('', item).strip(' \t\r\n')
|
||||||
line = line.strip()
|
if not ' ' in item: continue
|
||||||
if not line.endswith('</li>'): continue
|
|
||||||
|
|
||||||
command, template = line[4:-5].split(' ', 1)
|
command, template = item.split(' ', 1)
|
||||||
if not template.startswith('http://'): continue
|
if not template.startswith('http://'): continue
|
||||||
result[command] = template
|
result[command] = template
|
||||||
return result
|
return result
|
||||||
|
@ -26,32 +30,39 @@ def mappings(uri):
|
||||||
def o(phenny, input):
|
def o(phenny, input):
|
||||||
"""Call a webservice."""
|
"""Call a webservice."""
|
||||||
text = input.group(2)
|
text = input.group(2)
|
||||||
|
|
||||||
if (not o.services) or (text == 'refresh'):
|
|
||||||
if hasattr(phenny.config, 'services'):
|
if hasattr(phenny.config, 'services'):
|
||||||
services = 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)
|
o.services = mappings(services)
|
||||||
if text == 'refresh':
|
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:
|
if ' ' in text:
|
||||||
command, args = text.split(' ', 1)
|
command, args = text.split(' ', 1)
|
||||||
else: command, args = text, ''
|
else: command, args = text, ''
|
||||||
command = command.lower()
|
command = command.lower()
|
||||||
args = urllib.quote(args)
|
|
||||||
|
|
||||||
if o.services.has_key(command):
|
if o.services.has_key(command):
|
||||||
template = o.services[command]
|
template = o.services[command]
|
||||||
template = template.replace('${args}', args)
|
template = template.replace('${args}', urllib.quote(args.encode('utf-8')))
|
||||||
template = template.replace('${nick}', input.nick)
|
template = template.replace('${nick}', urllib.quote(input.nick))
|
||||||
uri = template.replace('${sender}', input.sender)
|
uri = template.replace('${sender}', urllib.quote(input.sender))
|
||||||
|
|
||||||
bytes = web.get(uri)
|
bytes = web.get(uri)
|
||||||
lines = bytes.splitlines()
|
lines = bytes.splitlines()
|
||||||
if lines:
|
if lines:
|
||||||
phenny.say(lines[0])
|
phenny.say(lines[0][:350])
|
||||||
else: phenny.reply('Sorry, the service is broken.')
|
else: phenny.reply('Sorry, the service is broken.')
|
||||||
else: phenny.reply('Sorry, no such service. See %s' % services)
|
else: phenny.reply('Sorry, no such service. See %s' % services)
|
||||||
o.commands = ['o']
|
o.commands = ['o']
|
||||||
|
|
Loading…
Reference in New Issue