2008-02-23 07:17:06 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
dict.py - Phenny Dictionary Module
|
2009-06-07 05:08:49 -04:00
|
|
|
Copyright 2008-9, Sean B. Palmer, inamidst.com
|
2008-02-23 07:17:06 -05:00
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re, urllib
|
|
|
|
import web
|
|
|
|
from tools import deprecated
|
|
|
|
|
|
|
|
r_li = re.compile(r'(?ims)<li>.*?</li>')
|
|
|
|
r_tag = re.compile(r'<[^>]+>')
|
|
|
|
r_parens = re.compile(r'(?<=\()(?:[^()]+|\([^)]+\))*(?=\))')
|
|
|
|
r_word = re.compile(r'^[A-Za-z0-9\' -]+$')
|
|
|
|
|
|
|
|
uri = 'http://encarta.msn.com/dictionary_/%s.html'
|
|
|
|
r_info = re.compile(
|
|
|
|
r'(?:ResultBody"><br /><br />(.*?) )|(?:<b>(.*?)</b>)'
|
|
|
|
)
|
|
|
|
|
|
|
|
def dict(phenny, input):
|
2011-04-19 00:50:58 -04:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to define.")
|
2008-02-23 07:17:06 -05:00
|
|
|
word = input.group(2)
|
|
|
|
word = urllib.quote(word.encode('utf-8'))
|
|
|
|
|
|
|
|
def trim(thing):
|
|
|
|
if thing.endswith(' '):
|
|
|
|
thing = thing[:-6]
|
|
|
|
return thing.strip(' :.')
|
|
|
|
|
|
|
|
bytes = web.get(uri % word)
|
|
|
|
results = {}
|
|
|
|
wordkind = None
|
|
|
|
for kind, sense in r_info.findall(bytes):
|
|
|
|
kind, sense = trim(kind), trim(sense)
|
|
|
|
if kind: wordkind = kind
|
|
|
|
elif sense:
|
|
|
|
results.setdefault(wordkind, []).append(sense)
|
|
|
|
result = input.group(2).encode('utf-8') + ' - '
|
|
|
|
for key in sorted(results.keys()):
|
|
|
|
if results[key]:
|
2008-05-15 05:44:48 -04:00
|
|
|
result += (key or '') + ' 1. ' + results[key][0]
|
2008-02-23 07:17:06 -05:00
|
|
|
if len(results[key]) > 1:
|
|
|
|
result += ', 2. ' + results[key][1]
|
|
|
|
result += '; '
|
2008-04-26 10:54:35 -04:00
|
|
|
result = result.rstrip('; ')
|
|
|
|
if result.endswith('-') and (len(result) < 30):
|
|
|
|
phenny.reply('Sorry, no definition found.')
|
|
|
|
else: phenny.say(result)
|
2008-02-23 07:17:06 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|