Skip to content

Commit

Permalink
[calc, etymology, ip] PEP8 per #125 and unicode per #240
Browse files Browse the repository at this point in the history
  • Loading branch information
embolalia committed Apr 24, 2013
1 parent df17688 commit 94820b4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
7 changes: 3 additions & 4 deletions willie/modules/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
import HTMLParser


def calculate(trigger):
q = trigger.encode('utf-8')
def calculate(q):
q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
uri = 'http://www.google.com/ig/calculator?q='
Expand Down Expand Up @@ -47,7 +46,7 @@ def c(willie, trigger):

def py(willie, trigger):
"""Evaluate a Python expression."""
query = trigger.group(2).encode('utf-8')
query = trigger.group(2)
uri = 'http://tumbolia.appspot.com/py/'
answer = web.get(uri + web.quote(query))
if answer:
Expand All @@ -62,7 +61,7 @@ def wa(willie, trigger):
"""Wolfram Alpha calculator"""
if not trigger.group(2):
return willie.reply("No search term.")
query = trigger.group(2).encode('utf-8')
query = trigger.group(2)
uri = 'http://tumbolia.appspot.com/wa/'
try:
answer = web.get(uri + web.quote(query.replace('+', '%2B')), 45)
Expand Down
24 changes: 8 additions & 16 deletions willie/modules/etymology.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
t_sentence = r'^.*?(?<!%s)(?:\.(?= [A-Z0-9]|\Z)|\Z)'
r_sentence = re.compile(t_sentence % ')(?<!'.join(abbrs))


def unescape(s):
s = s.replace('&gt;', '>')
s = s.replace('&lt;', '<')
s = s.replace('&amp;', '&')
return s


def text(html):
html = r_tag.sub('', html)
html = r_whitespace.sub(' ', html)
return unescape(html).strip()


def etymology(word):
# @@ <nsh> sbp, would it be possible to have a flag for .ety to get 2nd/etc
# entries? - http://swhack.com/logs/2006-07-19#T15-05-29
Expand All @@ -56,11 +59,6 @@ def etymology(word):
return None
sentence = m.group(0)

try:
sentence = unicode(sentence, 'iso-8859-1')
sentence = sentence.encode('utf-8')
except: pass

maxlength = 275
if len(sentence) > maxlength:
sentence = sentence[:maxlength]
Expand All @@ -71,17 +69,17 @@ def etymology(word):
sentence = '"' + sentence.replace('"', "'") + '"'
return sentence + ' - ' + (etyuri % word)


def f_etymology(willie, trigger):
"""Look up the etymology of a word"""
word = trigger.group(2)
if not word or not re.match(r"([A-Za-z0-9' .-]+)$", word):
return

try: result = etymology(word.encode('utf-8'))
try:
result = etymology(word)
except IOError:
msg = "Can't connect to etymonline.com (%s)" % (etyuri % word)
willie.msg(trigger.sender, msg)
return
return willie.NOLIMIT
except AttributeError:
result = None

Expand All @@ -91,12 +89,6 @@ def f_etymology(willie, trigger):
uri = etysearch % word
msg = 'Can\'t find the etymology for "%s". Try %s' % (word, uri)
willie.msg(trigger.sender, msg)
# @@ Cf. http://swhack.com/logs/2006-01-04#T01-50-22
return willie.NOLIMIT
f_etymology.commands = ['ety']
f_etymology.example = '.ety word'
f_etymology.thread = True
f_etymology.priority = 'high'

if __name__=="__main__":
import sys
print etymology(sys.argv[1])
73 changes: 37 additions & 36 deletions willie/modules/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,45 @@
import re
import willie.web as web


def ip(willie, trigger):
"""IP Lookup tool"""
if not trigger.group(2):
return willie.reply("No search term.")
query = trigger.group(2).encode('utf-8')
uri = 'http://www.rscript.org/lookup.php?type=ipdns&ip='
answer = web.get(uri + web.quote(query.replace('+', '%2B')))
if answer:
invalid = re.search("(?:INVALID: )([\S ]*)", answer)
if invalid:
response = "[IP/Host Lookup] "+invalid.group(1)
else:
#parse stuffs.
host = re.search("(?:Hostname:[ ]?)([\S ]*)", answer)
isp = re.search("(?:ISP:[ ]?)([\S ]*)", answer)
org = re.search("(?:Organization:[ ]?)([\S ]*)(?:Services:)", answer)
typ = re.search("(?:Type:[ ]?)([\S ]*)", answer)
assign = re.search("(?:Assignment:[ ]?)([\S ]*)", answer)
city = re.search("(?:City:[ ]?)([\S ]*)", answer)
state = re.search("(?:State/Region:[ ]?)([\S ]*)", answer)
country = re.search("(?:Country:[ ]?)([\S ]*)(?: )", answer)
"""IP Lookup tool"""
if not trigger.group(2):
return willie.reply("No search term.")
query = trigger.group(2)
uri = 'http://www.rscript.org/lookup.php?type=ipdns&ip='
answer = web.get(uri + web.quote(query.replace('+', '%2B')))
if answer:
invalid = re.search("(?:INVALID: )([\S ]*)", answer)
if invalid:
response = "[IP/Host Lookup] " + invalid.group(1)
else:
#parse stuffs.
host = re.search("(?:Hostname:[ ]?)([\S ]*)", answer)
isp = re.search("(?:ISP:[ ]?)([\S ]*)", answer)
org = re.search("(?:Organization:[ ]?)([\S ]*)(?:Services:)", answer)
typ = re.search("(?:Type:[ ]?)([\S ]*)", answer)
assign = re.search("(?:Assignment:[ ]?)([\S ]*)", answer)
city = re.search("(?:City:[ ]?)([\S ]*)", answer)
state = re.search("(?:State/Region:[ ]?)([\S ]*)", answer)
country = re.search("(?:Country:[ ]?)([\S ]*)(?: )", answer)

if not host or not isp or not org or not typ or not assign or not city or not state or not country:
response = "[IP/Host Lookup] Something went wrong, please try again."
else:
response = "[IP/Host Lookup] Hostname: "+host.group(1)
response += " | ISP: "+isp.group(1)
response += " | Organization: "+org.group(1)
response += " | Type: "+typ.group(1)
response += " | Assignment: "+assign.group(1)
response += " | Location: "+city.group(1)
response += ", "+state.group(1)
response += ", "+country.group(1)+"."
willie.say(response)
else:
willie.reply('Sorry, no result.')
ip.commands = ['iplookup','ip']
if not host or not isp or not org or not typ or not assign or not city or not state or not country:
response = "[IP/Host Lookup] Something went wrong, please try again."
else:
response = "[IP/Host Lookup] Hostname: " + host.group(1)
response += " | ISP: " + isp.group(1)
response += " | Organization: " + org.group(1)
response += " | Type: " + typ.group(1)
response += " | Assignment: " + assign.group(1)
response += " | Location: " + city.group(1)
response += ", " + state.group(1)
response += ", " + country.group(1) + "."
willie.say(response)
else:
willie.reply('Sorry, no result.')
ip.commands = ['iplookup', 'ip']
ip.example = '.iplookup 8.8.8.8'

if __name__ == '__main__':
print __doc__.strip()
print __doc__.strip()

0 comments on commit 94820b4

Please sign in to comment.