-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponder
executable file
·63 lines (52 loc) · 2.06 KB
/
responder
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
#!/usr/bin/env python
# Copyright (c) 2013 Louis Taylor <[email protected]>
# This work is free. You can redistribute it and/or modify it under the
# terms of the Do What The Fuck You Want To Public License, Version 2,
# as published by Sam Hocevar. See the COPYING file for more details.
import praw
import json
import re
import time
from collections import OrderedDict
from os.path import abspath
class Responder(object):
def __init__(self, configFilename = 'config.json'):
self.log('Loading config from', abspath(configFilename))
config = json.load(open(configFilename),
object_pairs_hook=OrderedDict)
self.username = config['username']
self.password = config['password']
self.actions = OrderedDict()
for r, message in config['responses'].iteritems():
self.actions[re.compile(r, re.IGNORECASE)] = message
self.log('Loaded', len(self.actions), 'actions')
self._r = praw.Reddit('Auto responder bot by /u/kragniz')
self._r.login(self.username, self.password)
self.messages = []
def has_messages(self):
self.messages = [m for m in self._r.get_unread()]
return len(self.messages) > 0
def respond(self, message):
for r, messageText in self.actions.iteritems():
if r.match(message.subject):
self.log('New message from:', message.author)
self.log('Subject:', message.subject)
message.reply(messageText)
message.mark_as_read()
break
def respond_to_messages(self):
for m in self.messages:
if not m.was_comment:
self.respond(m)
def log(self, *args):
print '\033[94m' + time.asctime() + '\033[0m',
print ' '.join([str(s) for s in args]) + '\033[0m'
if __name__ == '__main__':
r = Responder()
while True:
try:
time.sleep(10)
if r.has_messages():
r.respond_to_messages()
except Exception, e:
print r.log('Error raised:', e)