Advertisement
MolSno

calc.py

Nov 30th, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # coding=utf-8
  3. """
  4. calc.py - Phenny Calculator Module
  5. Copyright 2008, Sean B. Palmer, inamidst.com
  6. Licensed under the Eiffel Forum License 2.
  7.  
  8. http://inamidst.com/phenny/
  9. """
  10.  
  11. import re
  12. import web
  13.  
  14. r_result = re.compile(r'(?i)<A NAME=results>(.*?)</A>')
  15. r_tag = re.compile(r'<\S+.*?>')
  16.  
  17. subs = [
  18.    (' in ', ' -> '),
  19.    (' over ', ' / '),
  20.    (u'£', 'GBP '),
  21.    (u'€', 'EUR '),
  22.    ('\$', 'USD '),
  23.    (r'\bKB\b', 'kilobytes'),
  24.    (r'\bMB\b', 'megabytes'),
  25.    (r'\bGB\b', 'kilobytes'),
  26.    ('kbps', '(kilobits / second)'),
  27.    ('mbps', '(megabits / second)')
  28. ]
  29.  
  30. def calc(phenny, input):
  31.    """Use the Frink online calculator."""
  32.    q = input.group(2)
  33.    if not q:
  34.       return phenny.say('0?')
  35.  
  36.    query = q[:]
  37.    for a, b in subs:
  38.       query = re.sub(a, b, query)
  39.    query = query.rstrip(' \t')
  40.  
  41.    precision = 5
  42.    if query[-3:] in ('GBP', 'USD', 'EUR', 'NOK'):
  43.       precision = 2
  44.    query = web.urllib.quote(query.encode('utf-8'))
  45.  
  46.    uri = 'http://futureboy.us/fsp/frink.fsp?fromVal='
  47.    bytes = web.get(uri + query)
  48.    m = r_result.search(bytes)
  49.    if m:
  50.       result = m.group(1)
  51.       result = r_tag.sub('', result) # strip span.warning tags
  52.       result = result.replace('&gt;', '>')
  53.       result = result.replace('(undefined symbol)', '(?) ')
  54.  
  55.       if '.' in result:
  56.          try: result = str(round(float(result), precision))
  57.          except ValueError: pass
  58.  
  59.       if not result.strip():
  60.          result = '?'
  61.       elif ' in ' in q:
  62.          result += ' ' + q.split(' in ', 1)[1]
  63.  
  64.       phenny.say(q + ' = ' + result[:350])
  65.    else: phenny.reply("Sorry, can't calculate that.")
  66.    phenny.say('Note that .calc is deprecated, consider using .c')
  67. calc.commands = ['calc']
  68. calc.example = '.calc 5 + 3'
  69.  
  70. def c(phenny, input):
  71.    """Google calculator."""
  72.    if not input.group(2):
  73.       return phenny.reply("Nothing to calculate.")
  74.    q = input.group(2).encode('utf-8')
  75.    q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
  76.    q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
  77.    uri = 'http://www.google.com/ig/calculator?q='
  78.    bytes = web.get(uri + web.urllib.quote(q))
  79.    parts = bytes.split('",')
  80.    answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
  81.    if answer:
  82.       answer = answer.decode('unicode-escape')
  83.       answer = ''.join(chr(ord(c)) for c in answer)
  84.       answer = answer.decode('utf-8')
  85.       answer = answer.replace(u'\xc2\xa0', ',')
  86.       answer = answer.replace('<sup>', '^(')
  87.       answer = answer.replace('</sup>', ')')
  88.       answer = web.decode(answer)
  89.       phenny.say(answer)
  90.    else: phenny.say('Sorry, no result.')
  91. c.commands = ['c']
  92. c.example = '.c 5 + 3'
  93.  
  94. def py(phenny, input):
  95.    query = input.group(2).encode('utf-8')
  96.    uri = 'http://tumbolia.appspot.com/py/'
  97.    answer = web.get(uri + web.urllib.quote(query))
  98.    if answer:
  99.       phenny.say(answer)
  100.    else: phenny.reply('Sorry, no result.')
  101. py.commands = ['py']
  102.  
  103. def wa(phenny, input):
  104.    if not input.group(2):
  105.       return phenny.reply("No search term.")
  106.    query = input.group(2).encode('utf-8')
  107.    uri = 'http://tumbolia.appspot.com/wa/'
  108.    answer = web.get(uri + web.urllib.quote(query.replace('+', '%2B')))
  109.    if answer:
  110.       phenny.say(answer)
  111.    else: phenny.reply('Sorry, no result.')
  112. wa.commands = ['wa']
  113.  
  114. if __name__ == '__main__':
  115.    print __doc__.strip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement