-
Notifications
You must be signed in to change notification settings - Fork 7
/
AL.py
516 lines (416 loc) · 18.2 KB
/
AL.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""
Al - for Alan Turing
If someone says the bot's name in the channel followed by a ':',
e.g.
<sam> al: hello!
the al will reply:
<logbot> sam: I am AL
Run this script with four arguments:
e.g.
<server/ip>: 'irc.freenode.net'
<port>: 6667
<channel>: main
<logfile>: log/channel.log
$ python AL.py irc.freenode.net 6667 main log/channel.log
"""
# twisted imports
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log
# system imports
import time
import sys
from scrapers.cafescraper import scrapeCafe
from apis.weatherman import currentWeather
from apis.wolfram import wolfram
from apis.urbandic import urbanDict
from apis.lastfm import getCurrentSong
from apis.rottentomatoes import rottentomatoes
from apis.reddit import getSubReddit, getQuote
from random import randint
import ConfigParser
import json
import traceback
class MessageLogger:
"""
An independent logger class (because separation of application
and protocol logic is a good thing).
"""
def __init__(self, file):
self.file = file
def log(self, message):
"""Write a message to the file."""
timestamp = time.strftime("[%H:%M:%S]", time.localtime(time.time()))
self.file.write('%s %s\n' % (timestamp, message))
self.file.flush()
def close(self):
self.file.close()
class LogBot(irc.IRCClient):
"""A logging IRC bot."""
# the nickname might have problems with uniquness when connecting to freenode.net
nickname = "AL"
stored_messages = {}
user_info = {}
def __init__(self):
self.stored_messages = self.getMessages()
self.user_info = self.getUserInfo()
def getMessages(self):
""" Get my persisted messages from the message.json file"""
with open('files/messages.json', 'r') as f:
try:
messages = json.loads(f.read())
f.close()
return messages
except:
f.close()
return {}
def saveMessages(self):
""" Presist my stored messages by writing to a file"""
with open('files/messages.json', 'w') as f:
f.write(json.dumps(self.stored_messages))
f.close()
def getUserInfo(self):
""" Get information about my users """
with open('files/user_info.json', 'r') as f:
try:
messages = json.loads(f.read())
f.close()
return messages
except:
f.close()
return {}
def logError(self, channel):
""" Log an error to STDOUT, the logs, and chat """
print traceback.format_exc()
self.logger.log("Traceback Error:\n%s" % traceback.format_exc())
self.msg(channel, 'There was an Error in your request, check the logs')
def saveUserInfo(self):
""" Save my user data """
with open('files/user_info.json', 'w') as f:
f.write(json.dumps(self.user_info))
f.close()
def connectionMade(self):
irc.IRCClient.connectionMade(self)
self.logger = MessageLogger(open(self.factory.filename, "a"))
self.logger.log("[connected at %s]" %
time.asctime(time.localtime(time.time())))
self.join(self.factory.channel)
def connectionLost(self, reason):
irc.IRCClient.connectionLost(self, reason)
self.logger.log("[disconnected at %s]" %
time.asctime(time.localtime(time.time())))
self.logger.close()
# callbacks for events
def signedOn(self):
"""Called when bot has succesfully signed on to server."""
self.join(self.factory.channel)
def joined(self, channel):
"""This will get called when the bot joins the channel."""
self.logger.log("[I have joined %s]" % channel)
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
parts = msg.split()
# Check to see if they're sending me a private message
if channel == self.nickname:
parts.insert(0, self.nickname+':')
channel = user
# if someone is trying to give points
if parts[0][-2:] == '++':
awardee = parts[0][:-2]
# create a new user if AL doesn't know who they are
if awardee not in self.user_info:
self.user_info[awardee] = {
'email': '',
'phone': '',
'points': 0
}
self.user_info[awardee]['points'] += 1
total_points = self.user_info[awardee]['points']
self.saveUserInfo()
if total_points == 1:
self.msg(channel, '{0} has {1} point'. format(awardee, total_points))
else:
self.msg(channel, '{0} has {1} points'. format(awardee, total_points))
#==========================================================================================
# ---------- MESSAGES DIRECTED AT ME
#==========================================================================================
if parts[0] == self.nickname + ':':
if parts[1] == 'help':
"""Tell them the commands I have available"""
try:
help_msg = 'I currently support the following commands:\
\ncafe\
\nquote\
\nweather [<city> <state> | <zip>]\
\ntell <user> <message> (When they join the channel)\
\ndefine <something>\
\nshow users\
\nremember <name> <email> <phone number> (email, phone optional)\
\nupdate <user> <new email>\
\nsong <lastfm user>\
\nmovie <movie name>\
\nreddit <subreddit> <# of article optional>\
\nor just ask me a question'
self.msg(user, help_msg)
except Exception as e:
self.logError(channel)
elif parts[1] == 'cafe':
try:
menu = scrapeCafe()
# make the menu all nice for chat purposes
for k, v in menu['stations'].items():
if v:
station = '{:.<{station_width}}'.format(k.encode('utf-8'), station_width=menu['station_max_width'] + 4)
item = '{:.>{item_width}}'.format(v['item'].encode('utf-8'), item_width=menu['item_max_width'])
self.msg(channel, '%s%s %s' % (station, item, v['price'].encode('utf-8')))
except Exception as e:
self.logError(channel)
elif parts[1] == 'hi':
self.msg(channel, 'Hello, I am AL')
elif parts[1] == 'quote':
try:
randomQuote = getQuote()
self.msg(channel, randomQuote.encode('utf-8'))
except Exception, e:
self.logError(channel)
elif parts[1] == 'weather':
try:
# get the weather and tell the channel
if len(parts) == 3 and parts[2].isdigit() and len(parts[2]) == 5:
weather = currentWeather('', '', parts[2])
elif len(parts) >= 4:
state = parts.pop()
city = ' '.join(parts[2:])
weather = currentWeather(city, state)
else:
weather = currentWeather()
w_msg = 'The weather in {0} is {1}, {2} degrees, {3}% humdity.'.format(
weather['place'],
weather['status'],
weather['temp'],
weather['humidity']
)
self.msg(channel, w_msg)
self.logger.log(w_msg)
except Exception as e:
self.logError(channel)
elif parts[1] == 'tell':
"""Tell a user a given message when they join"""
try:
# form the message
target_user = parts[2]
tell_msg = '{0}, {1} said: {2}'.format(target_user, user, ' '.join(parts[3:]))
if target_user not in self.stored_messages:
self.stored_messages[target_user] = []
self.stored_messages[target_user].append(tell_msg)
self.saveMessages()
self.msg(channel, 'I will pass that along when {0} joins'.format(target_user))
except Exception as e:
self.logError(channel)
elif parts[1] == 'movie':
try:
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
key = config.get('rottentomatoes', 'key')
movie = ' '.join(parts[2:])
movie_response = rottentomatoes(movie, key)
if movie_response:
answer = 'Critics Score: {0}\nAudience Score: {1}\n{2}'.format(
movie_response['critics_score'],
movie_response['audience_score'],
movie_response['link'])
self.msg(channel, answer)
else:
answer = 'I can\'t find that movie'
self.msg(channel, answer)
except Exception, e:
self.logError(channel)
elif parts[1] == 'reddit':
try:
subreddit = parts[2]
try:
count = int(parts[3])
except IndexError:
count = 1
reddit_response = getSubReddit(subreddit, count)
if reddit_response:
answer = '{0}: {1} : {2}'.format(
count,
reddit_response['title'],
reddit_response['url'])
self.msg(channel, answer.encode('utf-8'))
else:
answer = 'I can\'t find that on reddit'
self.msg(channel, answer)
except Exception, e:
self.logError(channel)
elif parts[1] == 'define':
try:
question = ' '.join(parts[2:])
urban_response = urbanDict(question)
if urban_response:
answer = '{0}\nFor Example: {1}\n{2}'.format(
urban_response['definition'],
urban_response['example'],
urban_response['permalink'])
self.msg(channel, answer)
else:
answer = 'I don\'t know'
self.msg(channel, answer)
except Exception as e:
self.logError(channel)
elif ' '.join(parts[1:3]) == 'show users':
try:
self.msg(channel, ', '.join([user for user in self.user_info]).encode('utf-8'))
except Exception as e:
self.logError(channel)
elif parts[1] == 'remember':
try:
user = parts[2]
if user not in self.user_info:
self.user_info[user] = {
'email': '',
'phone': '',
'points': 0
}
# Try to set an email or phone, if they were supplied
try:
self.user_info[user]['email'] = parts[3]
try:
self.user_info[user]['phone'] = parts[4]
except IndexError:
pass
except IndexError:
pass
self.saveUserInfo()
self.msg(channel, "I'll remember that info")
else:
self.msg(channel, 'I already know that user')
except Exception as e:
self.logError(channel)
elif ' '.join(parts[1:3]) == 'update email':
try:
user = parts[3]
if user in self.user_info:
try:
self.user_info[user]['email'] = parts[4]
self.saveUserInfo()
self.msg(channel, 'Updated email for %s' % user)
except IndexError:
self.msg(channel, 'Please supply an email')
else:
self.msg(channel, "I don't know that user")
except Exception as e:
self.logError(channel)
elif parts[1] == 'song':
try:
user = parts[2]
song = getCurrentSong(user)
if song:
self.msg(channel, '{0} is listening to {1}'.format(user, song.encode('utf-8')))
except Exception as e:
self.logError(channel)
elif parts[1] in ['Will', 'will']:
try:
possible_ansers = [
'Yes.',
'No.',
'Probably.',
'There is a 50/50 chance.',
'Maybe. Impossible to know for sure',
'Probably not.'
]
self.msg(channel, possible_ansers[randint(0, 5)].encode('utf-8'))
except:
self.logError(channel)
#==========================================================================================
# ---------- IF NOT ONE OF THE SPECIAL COMMANDS ABOVE ASK WOLFRAM
#==========================================================================================
else:
try:
config = ConfigParser.RawConfigParser()
config.read('config.cfg')
key = config.get('wolfram', 'key')
question = ' '.join(parts[1:])
w = wolfram(key)
result = w.search(question)
if result:
answer = result.get('Value',
result.get('Result',
result.get('Definition',
result.get('Statement',
result.get('Current result',
None)))))
if answer:
self.msg(channel, answer.encode('utf-8'))
else:
count = 0
self.msg(channel, 'Not entirely sure, maybe this helps?:')
for k, v in result.items():
if count < 2:
self.msg(channel, v.encode('utf-8'))
else:
self.msg(user, v.encode('utf-8'))
count += 1
else:
self.msg(channel, 'I don\'t know')
except Exception as e:
self.logError(channel)
def userJoined(self, user, channel):
"""This will get called when I see a user join a channel"""
#check to see if I need to tell anyone anything
try:
if user in self.stored_messages:
for message in self.stored_messages[user]:
self.msg(channel, str(message))
#remove the messages
del self.stored_messages[user]
self.saveMessages()
except Exception as e:
self.logError(channel)
def action(self, user, channel, msg):
"""This will get called when the bot sees someone do an action."""
user = user.split('!', 1)[0]
self.logger.log("* %s %s" % (user, msg))
# irc callbacks
def irc_NICK(self, prefix, params):
"""Called when an IRC user changes their nickname."""
old_nick = prefix.split('!')[0]
new_nick = params[0]
self.logger.log("%s is now known as %s" % (old_nick, new_nick))
# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
def alterCollidedNick(self, nickname):
"""
Generate an altered version of a nickname that caused a collision in an
effort to create an unused related name for subsequent registration.
"""
return nickname + '^'
class LogBotFactory(protocol.ClientFactory):
"""A factory for LogBots.
A new protocol instance will be created each time we connect to the server.
"""
def __init__(self, channel, filename):
self.channel = channel
self.filename = filename
def buildProtocol(self, addr):
p = LogBot()
p.factory = self
return p
def clientConnectionLost(self, connector, reason):
"""If we get disconnected, reconnect to server."""
connector.connect()
def clientConnectionFailed(self, connector, reason):
print "connection failed:", reason
reactor.stop()
if __name__ == '__main__':
# initialize logging
log.startLogging(sys.stdout)
# create factory protocol and application
f = LogBotFactory(sys.argv[3], sys.argv[4])
# connect factory to this host and port
reactor.connectTCP(sys.argv[1], int(sys.argv[2]), f)
# run bot
reactor.run()