Last active
August 29, 2015 14:27
-
-
Save 13steinj/4027d2a2283e921f4d66 to your computer and use it in GitHub Desktop.
Reddit bot for mods who want to remove all comments in removed threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import praw | |
import OAuth2Util | |
# Your username. Not the username of the bot | |
USERNAME = "" | |
SUBREDDIT = "" | |
# Post limit chooses how far to look back in /about/spam | |
# Comment limit chooses the max amount of MoreComments to | |
# load if they aren't originally loaded in the post | |
# Leave it to None if there is no issue with speed. | |
POSTLIMIT = None | |
COMMENTLIMIT = None | |
# If you are automating this task via a CRON job or Windows | |
# Task Scheduler, keep the following value True. Otherwise, | |
# set it to false for the script to automate itself. | |
CRONJOB = True | |
try: | |
import botconfig | |
except ImportError: | |
pass | |
try: | |
USERNAME = botconfig.USERNAME | |
SUBREDDIT = botconfig.SUBREDDIT | |
except AttributeError: | |
pass | |
USERNAME.replace("/u/", "") | |
USERNAME.replace("/U/", "") | |
USERNAME.replace("u/", "") | |
USERNAME.replace("U/", "") | |
SUBREDDIT.replace("/r/", "") | |
SUBREDDIT.replace("/R/", "") | |
SUBREDDIT.replace("r/", "") | |
SUBREDDIT.replace("R/", "") | |
if USERNAME == "" or SUBREDDIT == "": | |
raise NameError('You must fill out the username and subreddit fields.') | |
USERAGENT = "A script to remove all comments on removed posts on /r/{0} for /u/{1}".format(SUBREDDIT, USERNAME) | |
if USERNAME != "13steinj" | |
USERAGENT += " by /u/13steinj" | |
def main(): | |
sub = r.get_subreddit(SUBREDDIT) | |
spammed = sub.get_spam(limit=LIMIT) | |
for x in spammed: | |
if isinstance(x, praw.objects.Comment) | |
continue | |
x.replace_more_comments(limit=COMMENTLIMIT, threshold=0) | |
comments = praw.helpers.flatten_tree(x.comments) | |
for y in comments: | |
if y.distinguished != None: | |
continue | |
y.remove() | |
if __name__ == '__main__': | |
r = praw.Reddit(USERAGENT) | |
o = OAuth2Util.OAuth2Util(r) | |
if CRONJOB == True: | |
o.refresh() | |
try: | |
main() | |
except KeyboardInterrupt: | |
raise | |
elif CRONJOB == False: | |
o.refresh(force=True) | |
while CRONJOB == False: | |
try: | |
main() | |
except KeyboardInterrupt: | |
raise |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment