mod_util.py

Sat, 29 Nov 2014 10:45:51 -0500

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Sat, 29 Nov 2014 10:45:51 -0500
changeset 104
27294de7b0d3
parent 102
2bad379cd416
child 105
84bd617a2520
permissions
-rw-r--r--

- merged

73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 import math
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 import urllib2
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
3 import json
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 from modulecore import command_error
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
5 import modulecore as ModuleCore
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 ModuleData = {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 'commands':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 [
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 'name': 'convert',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 'description': 'Performs numeric conversion',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 'args': '<value> as <valuetype>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 },
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 {
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18 'name': 'ud',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 'description': 'Looks up a term in urban dictionary',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 'args': '<term...>',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 'level': 'normal',
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 },
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
23
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
24 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
25 'name': 'commands',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
26 'description': 'Lists commands available to the calling user',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
27 'args': None,
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
28 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
29 },
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
30
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
31 {
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
32 'name': 'help',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
33 'description': 'Prints help about a given command',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
34 'args': '<command>',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
35 'level': 'normal',
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
36 },
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 ]
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 }
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 def cmd_convert (bot, args, replyto, **rest):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41 value = float (args['value'])
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
42 valuetype = args['valuetype']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 if valuetype in ['radians', 'degrees']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 if valuetype == 'radians':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 radvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 degvalue = (value * 180.) / math.pi
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 radvalue = (value * math.pi) / 180.
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50 degvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 bot.privmsg (replyto, '%s radians, %s degrees (%s)' %
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 (radvalue, degvalue, degvalue % 360.))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 if valuetype in ['celsius', 'fahrenheit']:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 if valuetype == 'celsius':
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 celvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 fahrvalue = value * 1.8 + 32
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 else:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 celvalue = (value - 32) / 1.8
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 fahrvalue = value
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue))
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 return
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 'celsius, fahrenheit (temperature conversion)' % valuetype)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 def cmd_ud (bot, args, replyto, **rest):
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 try:
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
72 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 response = urllib2.urlopen (url).read()
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 data = json.loads (response)
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 if not 'list' in data \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 or len(data['list']) == 0 \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 or not 'word' in data['list'][0] \
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 or not 'definition' in data['list'][0]:
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80 command_error ("couldn't find a definition of \002%s\002" % args['term'])
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 word = data['list'][0]['word']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 up = data['list'][0]['thumbs_up']
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 down = data['list'][0]['thumbs_down']
76
a2fe9ba3041a - various fixes
Teemu Piippo <crimsondusk64@gmail.com>
parents: 73
diff changeset
86 bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
73
d67cc4fbc3f1 - modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 except Exception as e:
102
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
88 command_error ('Urban dictionary lookup failed: %s' % e)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
89
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
90 def cmd_commands (bot, replyto, ident, host, **rest):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
91 commandlist = ModuleCore.get_available_commands (ident, host)
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
92 partitioned=[]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
93
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
94 while len (commandlist) > 0:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
95 partitioned.append (commandlist[0:15])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
96 commandlist = commandlist[15:]
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
97
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
98 for part in partitioned:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
99 bot.privmsg (replyto, 'Available commands: %s' % (", ".join (part)))
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
100
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
101 def cmd_help (bot, replyto, ident, host, args, **rest):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
102 cmd = ModuleCore.get_command_by_name (args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
103
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
104 if not cmd:
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
105 command_error ('unknown command \'%s\'' % args['command'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
106
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
107 if not ModuleCore.is_available (cmd, ident, host):
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
108 command_error ('you may not use %s' % cmd['name'])
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
109
2bad379cd416 - added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents: 76
diff changeset
110 bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description']))

mercurial