/modules/calc.py
https://github.com/adiq/stah · Python · 72 lines · 54 code · 13 blank · 5 comment · 10 complexity · a91ccf92347efbc82e0c9b4f6d1cb1ee MD5 · raw file
- import re
- import json
- import hashlib
- from util.hook import *
- from util import web
- uri = 'http://api.duckduckgo.com/?q=%s&format=json'
- @hook(cmds=['c', 'calc', 'calculate'], ex='calc 5 + 3', args=True)
- def calc(code, input):
- try:
- data = web.json(uri % web.quote(input.group(2).replace('^', '**')))
- if data['AnswerType'] != 'calc':
- return code.reply('Failed to calculate')
- answer = re.sub(r'\<.*?\>', '', data['Answer']).strip().split('}')[1]
- return code.say(answer)
- except:
- return code.reply('Failed to calculate!')
- @hook(cmds=['py', 'python'], ex='py print(int(1.0) + int(3))', args=True)
- def py(code, input):
- """python <commands> -- Execute Python inside of a sandbox"""
- query = input.group(2).encode('utf-8')
- uri = 'http://tumbolia.appspot.com/py/'
- try:
- answer = web.get(uri + web.quote(query)).read()
- if answer:
- answer = answer.replace('\n', ' ').replace(
- '\t', ' ').replace('\r', '')
- return code.reply(answer)
- else:
- return code.reply('Sorry, no {b}%s{b}')
- except:
- return code.reply('{red}The server did not return an answer.')
- @hook(cmds=['wa'], ex='wa 1 mile in feet', args=True)
- def wa(code, input):
- """Wolphram Alpha search"""
- query = input.group(2)
- uri = 'http://tumbolia.appspot.com/wa/'
- answer = web.get(uri + web.quote(query)).read()
- if answer and 'json stringified precioussss' not in answer:
- answer = answer.split(';')
- if len(answer) > 3:
- answer = answer[1]
- answer = '{purple}{b}WolphramAlpha: {c}{b}' + answer
- while ' ' in answer:
- answer = answer.replace(' ', ' ')
- return code.say(web.htmlescape(answer))
- else:
- return code.reply('{red}Sorry, no result.')
- @hook(cmds=['md5', 'hash'], priority='low', args=True)
- def md5(code, input):
- """md5 <string> -- Create a md5 hash of the input string"""
- return code.say(hashlib.md5(input.group(2)).hexdigest())
- @hook(cmds=['sha256'], priority='low', args=True)
- def sha256(code, input):
- """sha256 <string> -- Create a sha256 hash of the input string"""
- return code.say(hashlib.sha256(input.group(2)).hexdigest())
- @hook(cmds=['sha512'], priority='low', args=True)
- def sha512(code, input):
- """sha512 <string> -- Create a sha512 hash of the input string"""
- return code.say(hashlib.sha512(input.group(2)).hexdigest())