forked from mikellama/pronk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.py
192 lines (166 loc) · 6.53 KB
/
actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
#
## Copyright (C) 2018 Mike Rose
#
## This is version 0.5 of the Copyfree Open Innovation License.
##
## ## Terms and Conditions
##
## Redistributions, modified or unmodified, in whole or in part, must retain
## applicable copyright or other legal privilege notices, these conditions, and
## the following license terms and disclaimer. Subject to these conditions, the
## holder(s) of copyright or other legal privileges, author(s) or assembler(s),
## and contributors of this work hereby grant to any person who obtains a copy of
## this work in any form:
##
## 1. Permission to reproduce, modify, distribute, publish, sell, sublicense, use,
## and/or otherwise deal in the licensed material without restriction.
##
## 2. A perpetual, worldwide, non-exclusive, royalty-free, irrevocable patent
## license to reproduce, modify, distribute, publish, sell, use, and/or otherwise
## deal in the licensed material without restriction, for any and all patents:
##
## a. Held by each such holder of copyright or other legal privilege, author
## or assembler, or contributor, necessarily infringed by the contributions
## alone or by combination with the work, of that privilege holder, author or
## assembler, or contributor.
##
## b. Necessarily infringed by the work at the time that holder of copyright
## or other privilege, author or assembler, or contributor made any
## contribution to the work.
##
## NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS LICENSE
## OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE, INCLUDING BUT NOT
## LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
## AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, ASSEMBLERS, OR HOLDERS OF
## COPYRIGHT OR OTHER LEGAL PRIVILEGE BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
## LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT, OR OTHERWISE ARISING FROM, OUT
## OF, OR IN CONNECTION WITH THE WORK OR THE USE OF OR OTHER DEALINGS IN THE WORK.
#
## Import the files required for this to work.
import requests, wikipedia
import urbandictionary as ud
import mwaaa
import details
import random
from HTMLParser import HTMLParser
from imdbparser import IMDb
## Define a list of commands.
commands = ["?song", "?ask", "?wiki", "?bye", "?ud", "?/", "?imdb", "?coin", "?slap"]
commands += list(mwaaa.reply.keys())
commands += ["PRIVMSG "+details.nick, mwaaa.updateKey]
def act(c,msg,sender,mem):
'''
c is a command from commands list defined above
msg is the whole message send that contains the command trigger
sender is part of msg, the nick for the sender
mem is a list or strings containing the last few 'msg's
'''
with open('callLog', 'a') as f:
f.write(sender+": "+c+"\n"+msg+"\n\n")
r = ""
## Basic text response.
if c in mwaaa.reply:
r = mwaaa.reply[c]
## Song.
elif c == "?song":
try:
req = requests.get("http://letty.tk:8000/rds-xml.xsl")
h = HTMLParser()
r = h.unescape(req.content[29:-9])
if len(r) < 3:
r = "I don't hear anything."
except:
r = "not now " + sender
#r = "This feature is disabled :("
## get answer from yahoo answers.
elif c == "?ask":
try:
question = msg[msg.find("?ask") + 5:]
if len(question) > 1:
h = HTMLParser()
req = requests.get("http://api.haxed.net/answers/?b&q=" + question)
r = h.unescape(req.content)
if len(r) < 3:
r = "no answer"
except:
r = "error getting answer"
#r = "This feature is disabled :("
## Slap
elif c == "?slap":
audience = msg[msg.find("?slap") + 6:]
if len(audience) > 1:
r = sender + " slaps themself for the amusement of " + audience
else:
r = sender + " slaps themself."
## Coin Flip
elif c == "?coin":
r = random.sample(["heads", "tails"], 1)
r = r[0]
## Text replacement.
elif c == "?/" and msg.find(" :?/") > 1:
if msg[-1] == '/':
msg = msg[:-1]
mfull = msg[msg.find("?/")+2:]
mbad = mfull[:mfull.find("/")]
mgood = '\x02' + mfull[mfull.find("/")+1:] + '\x02'
try:
for m in reversed(mem[:-1]):
if m.find(mbad) != -1 and m.find("?/") == -1:
oldSender = m[:m.find("??")]
mes = m[len(oldSender)+2:]
if mes.startswith("\x01ACTION"): # /me
mes = mes[8:-1]
action = True
else:
action = False
preBad = mes[:mes.find(mbad)]
postBad = mes[mes.find(mbad)+len(mbad):]
fixed = preBad + mgood + postBad
if action:
fixed = "* \x02"+oldSender+"\x02 "+fixed
else:
fixed = '"'+fixed+'"'
r = "\x02"+oldSender+"\x02 meant: " +fixed
if sender != oldSender:
r = "\x02"+sender+'\x02 thinks ' + r
return r
except:
r = "well that didn't work :/"
## Urban Dictionary.
elif c == "?ud":
try:
query = msg[msg.find("?ud") + 4:].replace('"',"'")
defs = ud.define(query)
for d in defs[:3]:
r += d.definition.replace('\n', ' ').replace('\r', ' ')
except:
r = "well that didn't work :/"
## Wikipedia.
elif c == "?wiki":
try:
query = msg[msg.find("?wiki") + 6:]
r = wikipedia.summary(query, sentences=3)
except wikipedia.exceptions.DisambiguationError as e:
optionCount = min(len(e.options), 14)
for c, value in enumerate(e.options[:optionCount-1]):
r += value+", "
r+= "or " +e.options[optionCount-1]+ "?"
except wikipedia.exceptions.PageError as e2:
r = "Didn't find anything"
## IMDb.
elif c == "?imdb":
imdb = IMDb()
title = msg[msg.find("?imdb") + 6:]
try:
searchResult = imdb.search_movie(title)
searchResult.fetch()
movie = searchResult.results[0]
movie.fetch()
if len(searchResult.results) < 1:
r = "I didn't find anything"
else:
r = movie.title+" ("+str(movie.year)+") "+'-'.join(movie.genres)+" ["+str(movie.rating)+"/10] "+movie.plot
except:
r = "something went wrong :/"
return r.encode('utf-8')