Advertisement
askcompu

Untitled

Jul 15th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 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.     ('£', 'GBP '),
  21.     ('€', '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 c(phenny, input):
  31.     """Google calculator."""
  32.     if not input.group(2):
  33.         return phenny.reply("Nothing to calculate.")
  34.     q = input.group(2)
  35.     q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5
  36.     q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0
  37.     uri = 'http://www.google.com/ig/calculator?q='
  38.     bytes = web.get(uri + web.quote(q))
  39.     parts = bytes.split('",')
  40.     answer = [p for p in parts if p.startswith('rhs: "')][0][6:]
  41.     if answer:
  42.         #answer = ''.join(chr(ord(c)) for c in answer)
  43.         #answer = answer.decode('utf-8')
  44.         answer = answer.replace('\\x26#215;', '*')
  45.         answer = answer.replace('\\x3c', '<')
  46.         answer = answer.replace('\\x3e', '>')
  47.         answer = answer.replace('<sup>', '^(')
  48.         answer = answer.replace('</sup>', ')')
  49.         answer = web.decode(answer)
  50.         phenny.say(answer)
  51.     else: phenny.say('Sorry, no result.')
  52. c.commands = ['c']
  53. c.example = '.c 5 + 3'
  54.  
  55. def py(phenny, input):
  56.     query = input.group(2)
  57.     uri = 'http://tumbolia.appspot.com/py/'
  58.     answer = web.get(uri + web.quote(query))
  59.     if answer:
  60.         phenny.say(answer)
  61.     else: phenny.reply('Sorry, no result.')
  62. py.commands = ['py']
  63.  
  64. def wa(phenny, input):
  65.     if not input.group(2):
  66.         return phenny.reply("No search term.")
  67.     query = input.group(2)
  68.     uri = 'http://tumbolia.appspot.com/wa/'
  69.     answer = web.get(uri + web.quote(query.replace('+', '%2B')))
  70.     answerindex = 1
  71.     if (answer.split(";").len() < 2):
  72.         answerindex = 0
  73.     answer = answer.split(";")[answerindex]
  74.     if answer:
  75.         phenny.say(answer)
  76.     else: phenny.reply('Sorry, no result.')
  77. wa.commands = ['wa']
  78.  
  79. if __name__ == '__main__':
  80.     print(__doc__.strip())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement