PageRenderTime 59ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/calc.py

https://github.com/adiq/stah
Python | 72 lines | 69 code | 3 blank | 0 comment | 2 complexity | a91ccf92347efbc82e0c9b4f6d1cb1ee MD5 | raw file
  1. import re
  2. import json
  3. import hashlib
  4. from util.hook import *
  5. from util import web
  6. uri = 'http://api.duckduckgo.com/?q=%s&format=json'
  7. @hook(cmds=['c', 'calc', 'calculate'], ex='calc 5 + 3', args=True)
  8. def calc(code, input):
  9. try:
  10. data = web.json(uri % web.quote(input.group(2).replace('^', '**')))
  11. if data['AnswerType'] != 'calc':
  12. return code.reply('Failed to calculate')
  13. answer = re.sub(r'\<.*?\>', '', data['Answer']).strip().split('}')[1]
  14. return code.say(answer)
  15. except:
  16. return code.reply('Failed to calculate!')
  17. @hook(cmds=['py', 'python'], ex='py print(int(1.0) + int(3))', args=True)
  18. def py(code, input):
  19. """python <commands> -- Execute Python inside of a sandbox"""
  20. query = input.group(2).encode('utf-8')
  21. uri = 'http://tumbolia.appspot.com/py/'
  22. try:
  23. answer = web.get(uri + web.quote(query)).read()
  24. if answer:
  25. answer = answer.replace('\n', ' ').replace(
  26. '\t', ' ').replace('\r', '')
  27. return code.reply(answer)
  28. else:
  29. return code.reply('Sorry, no {b}%s{b}')
  30. except:
  31. return code.reply('{red}The server did not return an answer.')
  32. @hook(cmds=['wa'], ex='wa 1 mile in feet', args=True)
  33. def wa(code, input):
  34. """Wolphram Alpha search"""
  35. query = input.group(2)
  36. uri = 'http://tumbolia.appspot.com/wa/'
  37. answer = web.get(uri + web.quote(query)).read()
  38. if answer and 'json stringified precioussss' not in answer:
  39. answer = answer.split(';')
  40. if len(answer) > 3:
  41. answer = answer[1]
  42. answer = '{purple}{b}WolphramAlpha: {c}{b}' + answer
  43. while ' ' in answer:
  44. answer = answer.replace(' ', ' ')
  45. return code.say(web.htmlescape(answer))
  46. else:
  47. return code.reply('{red}Sorry, no result.')
  48. @hook(cmds=['md5', 'hash'], priority='low', args=True)
  49. def md5(code, input):
  50. """md5 <string> -- Create a md5 hash of the input string"""
  51. return code.say(hashlib.md5(input.group(2)).hexdigest())
  52. @hook(cmds=['sha256'], priority='low', args=True)
  53. def sha256(code, input):
  54. """sha256 <string> -- Create a sha256 hash of the input string"""
  55. return code.say(hashlib.sha256(input.group(2)).hexdigest())
  56. @hook(cmds=['sha512'], priority='low', args=True)
  57. def sha512(code, input):
  58. """sha512 <string> -- Create a sha512 hash of the input string"""
  59. return code.say(hashlib.sha512(input.group(2)).hexdigest())