-
Notifications
You must be signed in to change notification settings - Fork 1
/
redditprocessor.py
87 lines (69 loc) · 2.74 KB
/
redditprocessor.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
#!/usr/bin/env python
"""
Reddit Comment Processor
Processor for Reddit comments that scans the last comments and runs it through
a chain of commands to allow each command to process the comment.
"""
import abc
import logging
import praw
from abc import ABCMeta
from itertools import product
class Command(object):
"""Abstract comment command processor
Implementors will need to override process method.
"""
__metaclass__ = ABCMeta
def __init__(self, name):
self.__logger = logging.getLogger('reddit-bot.Command')
self.name = name
def execute(self, comment):
#self.__logger.debug('Processing: %s/_/%s',
# comment.link_id, comment.id)
self.process(comment)
@abc.abstractmethod
def process(self, comment):
"""Process the comment"""
return
class RedditCommentProcessor(object):
"""Reddit comment processor
Comment processor that scans the last series of comments and passes it
to the registered commands.
Commands will need to be registered by calling register_command.
"""
def __init__(self,
reddit_creds=None,
user_agent='Snapshot Bot 0.1 by /u/tazzy531',
subreddit_filter=[],
comment_limit=50):
self.logger = logging.getLogger('reddit-bot.RedditCommentProcessor')
self.reddit = praw.Reddit(user_agent)
self.subreddit_filter = subreddit_filter
self.comment_limit = comment_limit
self.commands = []
if reddit_creds is not None:
self.logger.info('Logging into praw with user [/u/%s]',
reddit_creds['username'])
self.reddit.login(username=reddit_creds['username'],
password=reddit_creds['password'])
def register_command(self, command):
"""Register command to execute per comment"""
self.commands.append(command)
def run(self):
"""Process the latest comments for a given subreddit"""
self.logger.info('Executing Run')
for subreddit in self.subreddit_filter:
self.logger.info('Processing subreddit: %s', subreddit)
comments = self.reddit.get_comments(
subreddit,
limit=self.comment_limit)
self._process_comments(comments)
self.logger.info('Completed run')
def _process_comments(self, comments):
for comment, command in product(comments, self.commands):
try:
command.execute(comment)
except Exception, err:
self.logger.error(
'Failed processing on command [%s] with exception: %s',
command.name, repr(err), exc_info=True)