master
commit
4e25fa7bf9
4
bot.py
4
bot.py
|
@ -94,8 +94,8 @@ class Phenny(irc.Bot):
|
||||||
|
|
||||||
def sub(pattern, self=self):
|
def sub(pattern, self=self):
|
||||||
# These replacements have significant order
|
# These replacements have significant order
|
||||||
pattern = pattern.replace('$nickname', self.nick)
|
pattern = pattern.replace('$nickname', re.escape(self.nick))
|
||||||
return pattern.replace('$nick', r'%s[,:] +' % self.nick)
|
return pattern.replace('$nick', r'%s[,:] +' % re.escape(self.nick))
|
||||||
|
|
||||||
for name, func in self.variables.iteritems():
|
for name, func in self.variables.iteritems():
|
||||||
# print name, func
|
# print name, func
|
||||||
|
|
|
@ -69,6 +69,8 @@ calc.example = '.calc 5 + 3'
|
||||||
|
|
||||||
def c(phenny, input):
|
def c(phenny, input):
|
||||||
"""Google calculator."""
|
"""Google calculator."""
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("Nothing to calculate.")
|
||||||
q = input.group(2).encode('utf-8')
|
q = input.group(2).encode('utf-8')
|
||||||
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
|
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
|
||||||
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
|
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
|
||||||
|
@ -90,7 +92,7 @@ c.commands = ['c']
|
||||||
c.example = '.c 5 + 3'
|
c.example = '.c 5 + 3'
|
||||||
|
|
||||||
def py(phenny, input):
|
def py(phenny, input):
|
||||||
query = input.group(2)
|
query = input.group(2).encode('utf-8')
|
||||||
uri = 'http://tumbolia.appspot.com/py/'
|
uri = 'http://tumbolia.appspot.com/py/'
|
||||||
answer = web.get(uri + web.urllib.quote(query))
|
answer = web.get(uri + web.urllib.quote(query))
|
||||||
if answer:
|
if answer:
|
||||||
|
@ -99,6 +101,8 @@ def py(phenny, input):
|
||||||
py.commands = ['py']
|
py.commands = ['py']
|
||||||
|
|
||||||
def wa(phenny, input):
|
def wa(phenny, input):
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("No search term.")
|
||||||
query = input.group(2).encode('utf-8')
|
query = input.group(2).encode('utf-8')
|
||||||
uri = 'http://tumbolia.appspot.com/wa/'
|
uri = 'http://tumbolia.appspot.com/wa/'
|
||||||
answer = web.get(uri + web.urllib.quote(query))
|
answer = web.get(uri + web.urllib.quote(query))
|
||||||
|
|
|
@ -22,6 +22,8 @@ r_info = re.compile(
|
||||||
)
|
)
|
||||||
|
|
||||||
def dict(phenny, input):
|
def dict(phenny, input):
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("Nothing to define.")
|
||||||
word = input.group(2)
|
word = input.group(2)
|
||||||
word = urllib.quote(word.encode('utf-8'))
|
word = urllib.quote(word.encode('utf-8'))
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ def etymology(word):
|
||||||
raise ValueError("Word too long: %s[...]" % word[:10])
|
raise ValueError("Word too long: %s[...]" % word[:10])
|
||||||
word = {'axe': 'ax/axe'}.get(word, word)
|
word = {'axe': 'ax/axe'}.get(word, word)
|
||||||
|
|
||||||
bytes = web.get(etyuri % word)
|
bytes = web.get(etyuri % web.urllib.quote(word))
|
||||||
definitions = r_definition.findall(bytes)
|
definitions = r_definition.findall(bytes)
|
||||||
|
|
||||||
if not definitions:
|
if not definitions:
|
||||||
|
@ -77,7 +77,7 @@ def etymology(word):
|
||||||
def f_etymology(self, origin, match, args):
|
def f_etymology(self, origin, match, args):
|
||||||
word = match.group(2)
|
word = match.group(2)
|
||||||
|
|
||||||
try: result = etymology(word.encode('utf-8'))
|
try: result = etymology(word.encode('iso-8859-1'))
|
||||||
except IOError:
|
except IOError:
|
||||||
msg = "Can't connect to etymonline.com (%s)" % (etyuri % word)
|
msg = "Can't connect to etymonline.com (%s)" % (etyuri % word)
|
||||||
self.msg(origin.sender, msg)
|
self.msg(origin.sender, msg)
|
||||||
|
@ -92,7 +92,7 @@ def f_etymology(self, origin, match, args):
|
||||||
msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri)
|
msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri)
|
||||||
self.msg(origin.sender, msg)
|
self.msg(origin.sender, msg)
|
||||||
# @@ Cf. http://swhack.com/logs/2006-01-04#T01-50-22
|
# @@ Cf. http://swhack.com/logs/2006-01-04#T01-50-22
|
||||||
f_etymology.rule = (['ety'], r"([A-Za-z0-9' .-]+)$")
|
f_etymology.rule = (['ety'], r"(.+?)$")
|
||||||
f_etymology.thread = True
|
f_etymology.thread = True
|
||||||
f_etymology.priority = 'high'
|
f_etymology.priority = 'high'
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ def hello(phenny, input):
|
||||||
greeting = random.choice(('Hi', 'Hey', 'Hello'))
|
greeting = random.choice(('Hi', 'Hey', 'Hello'))
|
||||||
punctuation = random.choice(('', '!'))
|
punctuation = random.choice(('', '!'))
|
||||||
phenny.say(greeting + ' ' + input.nick + punctuation)
|
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):
|
def interjection(phenny, input):
|
||||||
phenny.say(input.nick + '!')
|
phenny.say(input.nick + '!')
|
||||||
|
|
|
@ -10,17 +10,31 @@ http://inamidst.com/phenny/
|
||||||
import re
|
import re
|
||||||
import web
|
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):
|
def search(query):
|
||||||
"""Search using AjaxSearch, and return its JSON."""
|
"""Search using AjaxSearch, and return its JSON."""
|
||||||
uri = 'http://ajax.googleapis.com/ajax/services/search/web'
|
uri = 'http://ajax.googleapis.com/ajax/services/search/web'
|
||||||
args = '?v=1.0&safe=off&q=' + web.urllib.quote(query.encode('utf-8'))
|
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)
|
bytes = web.get(uri + args)
|
||||||
|
web.urllib._urlopener = handler
|
||||||
return web.json(bytes)
|
return web.json(bytes)
|
||||||
|
|
||||||
def result(query):
|
def result(query):
|
||||||
results = search(query)
|
results = search(query)
|
||||||
try: return results['responseData']['results'][0]['unescapedUrl']
|
try: return results['responseData']['results'][0]['unescapedUrl']
|
||||||
except IndexError: return None
|
except IndexError: return None
|
||||||
|
except TypeError:
|
||||||
|
print results
|
||||||
|
return False
|
||||||
|
|
||||||
def count(query):
|
def count(query):
|
||||||
results = search(query)
|
results = search(query)
|
||||||
|
@ -48,6 +62,7 @@ def g(phenny, input):
|
||||||
if not hasattr(phenny.bot, 'last_seen_uri'):
|
if not hasattr(phenny.bot, 'last_seen_uri'):
|
||||||
phenny.bot.last_seen_uri = {}
|
phenny.bot.last_seen_uri = {}
|
||||||
phenny.bot.last_seen_uri[input.sender] = 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)
|
else: phenny.reply("No results found for '%s'." % query)
|
||||||
g.commands = ['g']
|
g.commands = ['g']
|
||||||
g.priority = 'high'
|
g.priority = 'high'
|
||||||
|
@ -69,6 +84,8 @@ r_query = re.compile(
|
||||||
)
|
)
|
||||||
|
|
||||||
def gcs(phenny, input):
|
def gcs(phenny, input):
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("Nothing to compare.")
|
||||||
queries = r_query.findall(input.group(2))
|
queries = r_query.findall(input.group(2))
|
||||||
if len(queries) > 6:
|
if len(queries) > 6:
|
||||||
return phenny.reply('Sorry, can only compare up to six things.')
|
return phenny.reply('Sorry, can only compare up to six things.')
|
||||||
|
|
|
@ -11,6 +11,8 @@ import web
|
||||||
|
|
||||||
def val(phenny, input):
|
def val(phenny, input):
|
||||||
"""Check a webpage using the W3C Markup Validator."""
|
"""Check a webpage using the W3C Markup Validator."""
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("Nothing to validate.")
|
||||||
uri = input.group(2)
|
uri = input.group(2)
|
||||||
if not uri.startswith('http://'):
|
if not uri.startswith('http://'):
|
||||||
uri = 'http://' + uri
|
uri = 'http://' + uri
|
||||||
|
|
|
@ -10,9 +10,9 @@ http://inamidst.com/phenny/
|
||||||
import re, urllib
|
import re, urllib
|
||||||
import web
|
import web
|
||||||
|
|
||||||
wikiuri = 'http://en.wikipedia.org/wiki/%s'
|
wikiuri = 'http://%s.wikipedia.org/wiki/%s'
|
||||||
wikisearch = 'http://en.wikipedia.org/wiki/Special:Search?' \
|
# wikisearch = 'http://%s.wikipedia.org/wiki/Special:Search?' \
|
||||||
+ 'search=%s&fulltext=Search'
|
# + 'search=%s&fulltext=Search'
|
||||||
|
|
||||||
r_tr = re.compile(r'(?ims)<tr[^>]*>.*?</tr>')
|
r_tr = re.compile(r'(?ims)<tr[^>]*>.*?</tr>')
|
||||||
r_paragraph = re.compile(r'(?ims)<p[^>]*>.*?</p>|<li(?!n)[^>]*>.*?</li>')
|
r_paragraph = re.compile(r'(?ims)<p[^>]*>.*?</p>|<li(?!n)[^>]*>.*?</li>')
|
||||||
|
@ -59,30 +59,30 @@ def search(term):
|
||||||
return uri[len('http://en.wikipedia.org/wiki/'):]
|
return uri[len('http://en.wikipedia.org/wiki/'):]
|
||||||
else: return term
|
else: return term
|
||||||
|
|
||||||
def wikipedia(term, last=False):
|
def wikipedia(term, language='en', last=False):
|
||||||
global wikiuri
|
global wikiuri
|
||||||
if not '%' in term:
|
if not '%' in term:
|
||||||
if isinstance(term, unicode):
|
if isinstance(term, unicode):
|
||||||
t = term.encode('utf-8')
|
t = term.encode('utf-8')
|
||||||
else: t = term
|
else: t = term
|
||||||
q = urllib.quote(t)
|
q = urllib.quote(t)
|
||||||
u = wikiuri % q
|
u = wikiuri % (language, q)
|
||||||
bytes = web.get(u)
|
bytes = web.get(u)
|
||||||
else: bytes = web.get(wikiuri % term)
|
else: bytes = web.get(wikiuri % (language, term))
|
||||||
bytes = r_tr.sub('', bytes)
|
bytes = r_tr.sub('', bytes)
|
||||||
|
|
||||||
if not last:
|
if not last:
|
||||||
r = r_redirect.search(bytes[:4096])
|
r = r_redirect.search(bytes[:4096])
|
||||||
if r:
|
if r:
|
||||||
term = urllib.unquote(r.group(1))
|
term = urllib.unquote(r.group(1))
|
||||||
return wikipedia(term, last=True)
|
return wikipedia(term, language=language, last=True)
|
||||||
|
|
||||||
paragraphs = r_paragraph.findall(bytes)
|
paragraphs = r_paragraph.findall(bytes)
|
||||||
|
|
||||||
if not paragraphs:
|
if not paragraphs:
|
||||||
if not last:
|
if not last:
|
||||||
term = search(term)
|
term = search(term)
|
||||||
return wikipedia(term, last=True)
|
return wikipedia(term, language=language, last=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Pre-process
|
# Pre-process
|
||||||
|
@ -115,7 +115,7 @@ def wikipedia(term, last=False):
|
||||||
if not m:
|
if not m:
|
||||||
if not last:
|
if not last:
|
||||||
term = search(term)
|
term = search(term)
|
||||||
return wikipedia(term, last=True)
|
return wikipedia(term, language=language, last=True)
|
||||||
return None
|
return None
|
||||||
sentence = m.group(0)
|
sentence = m.group(0)
|
||||||
|
|
||||||
|
@ -127,17 +127,18 @@ def wikipedia(term, last=False):
|
||||||
sentence = ' '.join(words) + ' [...]'
|
sentence = ' '.join(words) + ' [...]'
|
||||||
|
|
||||||
if (('using the Article Wizard if you wish' in sentence)
|
if (('using the Article Wizard if you wish' in sentence)
|
||||||
or ('or add a request for it' in sentence)):
|
or ('or add a request for it' in sentence)
|
||||||
|
or ('in existing articles' in sentence)):
|
||||||
if not last:
|
if not last:
|
||||||
term = search(term)
|
term = search(term)
|
||||||
return wikipedia(term, last=True)
|
return wikipedia(term, language=language, last=True)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
sentence = '"' + sentence.replace('"', "'") + '"'
|
sentence = '"' + sentence.replace('"', "'") + '"'
|
||||||
sentence = sentence.decode('utf-8').encode('utf-8')
|
sentence = sentence.decode('utf-8').encode('utf-8')
|
||||||
wikiuri = wikiuri.decode('utf-8').encode('utf-8')
|
wikiuri = wikiuri.decode('utf-8').encode('utf-8')
|
||||||
term = term.decode('utf-8').encode('utf-8')
|
term = term.decode('utf-8').encode('utf-8')
|
||||||
return sentence + ' - ' + (wikiuri % term)
|
return sentence + ' - ' + (wikiuri % (language, term))
|
||||||
|
|
||||||
def wik(phenny, input):
|
def wik(phenny, input):
|
||||||
origterm = input.groups()[1]
|
origterm = input.groups()[1]
|
||||||
|
@ -146,12 +147,19 @@ def wik(phenny, input):
|
||||||
origterm = origterm.encode('utf-8')
|
origterm = origterm.encode('utf-8')
|
||||||
|
|
||||||
term = urllib.unquote(origterm)
|
term = urllib.unquote(origterm)
|
||||||
|
language = 'en'
|
||||||
|
if term.startswith(':') and (' ' in term):
|
||||||
|
a, b = term.split(' ', 1)
|
||||||
|
a = a.lstrip(':')
|
||||||
|
if a.isalpha():
|
||||||
|
language, term = a, b
|
||||||
term = term[0].upper() + term[1:]
|
term = term[0].upper() + term[1:]
|
||||||
term = term.replace(' ', '_')
|
term = term.replace(' ', '_')
|
||||||
|
|
||||||
try: result = wikipedia(term)
|
try: result = wikipedia(term, language)
|
||||||
except IOError:
|
except IOError:
|
||||||
error = "Can't connect to en.wikipedia.org (%s)" % (wikiuri % term)
|
args = (language, wikiuri % (language, term))
|
||||||
|
error = "Can't connect to %s.wikipedia.org (%s)" % args
|
||||||
return phenny.say(error)
|
return phenny.say(error)
|
||||||
|
|
||||||
if result is not None:
|
if result is not None:
|
||||||
|
|
|
@ -72,6 +72,8 @@ def format(word, definitions, number=2):
|
||||||
return result.strip(' .,')
|
return result.strip(' .,')
|
||||||
|
|
||||||
def w(phenny, input):
|
def w(phenny, input):
|
||||||
|
if not input.group(2):
|
||||||
|
return phenny.reply("Nothing to define.")
|
||||||
word = input.group(2)
|
word = input.group(2)
|
||||||
etymology, definitions = wiktionary(word)
|
etymology, definitions = wiktionary(word)
|
||||||
if not definitions:
|
if not definitions:
|
||||||
|
|
Loading…
Reference in New Issue