PageRenderTime 2ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/calc.py

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