Skip to content

Commit

Permalink
Merge pull request #3 from listen2/master
Browse files Browse the repository at this point in the history
Change a bunch of stuff.
  • Loading branch information
xiphirx committed Aug 18, 2012
2 parents 21bf1da + 17277be commit 2301e96
Showing 1 changed file with 92 additions and 48 deletions.
140 changes: 92 additions & 48 deletions auto_mod.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,69 @@
import configparser import configparser




dryrun = True
loglevel = 3 #0 error, 1 normal activity, 2 verbose activity, 3 debug

def log(lvl, s):
if lvl <= loglevel:
print(s)

class Rule:
def __init__(self, name):
self.rname = name

def __str__(self):
return "<Rule(%s)>" % (self.rname)

def match(self, submission):
log(0, "INTERNAL ERROR: %s's rule type does not have a match() function" % (self.rname))
return False

def apply(self, submission):
if submission.subreddit.display_name not in self.reddits:
log(3, "SKIPPING %s %s" % (self.rname, submission.permalink))
return # Not all rules apply to all subreddits
if self.match(submission):
log(1, "MATCH %s: %s (%s)" % (self.rname, submission.permalink, submission.title))
self.do_actions(submission)

def do_actions(self, submission):
if dryrun:
log(1, "Dry run. Not acting.")
return
# Use "none" in the config file if you just want to log the match without acting.
if "comment" in self.actions:
log(2, "comment %s" % (submission.permalink))
modReply = submission.add_comment(self.rules[rule].comment)
modReply.distinguish()
if "remove" in self.actions:
log(1, "REMOVE %s" % (submission.permalink))
submission.remove()
if "report" in self.actions:
log(2, "REPORT %s" % (submission.permalink))
#TODO

class ImageRule(Rule):
def match(self, submission):
if submission.domain.lower() == "self." + self.rname.lower():
return False # self-posts can't be images
if self.re.match(submission.url):
return True
img = urllib.request.urlopen(submission.url)
type = img.info()['Content-Type']
if type.startswith('image/'):
return True
return False

class TitleRule(Rule):
def match(self, submission):
return self.re.match(submission.title)

class UserRule(Rule):
def match(self, submission):
return self.re.match(submission.author.name)


class ButcherBot: class ButcherBot:
class rule: class rule:
def __init__(self): def __init__(self):
Expand All @@ -16,61 +79,41 @@ def __init__(self):
self.config = configparser.SafeConfigParser() self.config = configparser.SafeConfigParser()
self.config.read("rules.ini") self.config.read("rules.ini")


# Login to moderator account self.reddits = set()
self.r = praw.Reddit(user_agent='r/Diablo Automated moderation bot') self.rules = []
print('Logging in as %s...' % (self.config.get("DEFAULT", "user"))) for s in self.config.sections():
rtype = self.config.get(s, "type")
if rtype == "image":
rule = ImageRule(s)
elif rtype == "title":
rule = TitleRule(s)
else:
rule = Rule(s) # This will probably cause a runtime error. Good.
rule.re = re.compile(self.config.get(s, "re"))
rule.comment = self.config.get(s, "comment")
rule.distinguish = self.config.get(s, "distinguish").lower() in ["true", "1", "t", "y", "yes", "on"]
rule.reddits = self.config.get(s, "reddits").split()
self.rules.append(rule)
for sr in self.config.get(s, "reddits").split():
self.reddits.add(sr)
log(3, "rules: %s" % (self.rules))
log(3, "reddits: %s" % (self.reddits))

# Log in
self.r = praw.Reddit(user_agent=self.config.get("DEFAULT", "user_agent"))
log(2, 'Logging in as %s...' % (self.config.get("DEFAULT", "user")))
self.r.login(self.config.get("DEFAULT", "user"), self.config.get("DEFAULT", "pass")) self.r.login(self.config.get("DEFAULT", "user"), self.config.get("DEFAULT", "pass"))


self.reddits = {}
for s in self.config.get("DEFAULT", "reddits").split():
self.reddits[s] = self.r.get_subreddit(s)

self.rules = {}
for s in self.config.sections():
self.rules[s] = self.rule()
self.rules[s].type = self.config.get(s, "type")
self.rules[s].re = re.compile(self.config.get(s, "re"))
self.rules[s].comment = self.config.get(s, "comment")
self.rules[s].distinguish = self.config.get(s, "distinguish").lower() in ["true", "1", "t", "y", "yes", "on"]


def save_config(self): def save_config(self):
with open('rules.ini', 'w') as fname: with open('rules.ini', 'w') as fname:
self.config.write(fname) self.config.write(fname)


def is_image(self, submission):
if self.rules["images"].re.match(submission.url):
return True
img = urllib.request.urlopen(submission.url)
type = img.info()['Content-Type']
return type.startswith('image/')

def has_acronym(self, submission):
return self.acronymRules[0].match(submission.title)

def remove_and_comment(self, submission, rule):
modReply = submission.add_comment(self.rules[rule].comment)
if self.rules[rule].distinguish:
modReply.distinguish()
submission.remove()

def apply_rules(self, rname, submission):
# TODO: flake still thinks this method is too complex
for rule in self.rules:
if self.rules[rule].type == "url":
if submission.domain.lower() == "self." + rname.lower():
continue # Don't even check self-posts
if self.is_image(submission):
print("Image detected, %s" % (submission.title))
self.remove_and_comment(submission, rule)

elif self.rules[rule].type == "title":
if self.has_acronym(submission):
print("Acronym detected, %s" % (submission.title))
self.remove_and_comment(submission, rule)


def auto_mod(self): def auto_mod(self):
# Grab the newest submissions... # main loop
for rname, sub in self.reddits.items(): for rname in self.reddits:
sub = self.r.get_subreddit(rname)
submissions = sub.get_new(limit=None, place_holder=self.config.get("DEFAULT", "last_item")) submissions = sub.get_new(limit=None, place_holder=self.config.get("DEFAULT", "last_item"))
first = True first = True
for submission in submissions: for submission in submissions:
Expand All @@ -79,10 +122,11 @@ def auto_mod(self):
first = False first = False


if submission.approved_by: if submission.approved_by:
print("Post is already approved") log(2, "Post is already approved")
continue continue


self.apply_rules(rname, submission) for rule in self.rules:
rule.apply(submission)


self.save_config() self.save_config()


Expand Down

0 comments on commit 2301e96

Please sign in to comment.