Skip to content

Instantly share code, notes, and snippets.

@awhit012
Last active February 11, 2016 23:54
Show Gist options
  • Save awhit012/82120ea9c1457fa589f6 to your computer and use it in GitHub Desktop.
Save awhit012/82120ea9c1457fa589f6 to your computer and use it in GitHub Desktop.
# This is a script to delete every comment and post on a reddit account.
# You may have to run it several times, depending on how many comments and posts you have on your user, due to
# the batching process of reddit's API.
import praw, csv, pprint
USERNAME = ""
PASSWORD = ""
class Bot:
def __init__(self):
print("Initializing Bot")
self.logged_in = False
self.r = praw.Reddit(user_agent = "script to delete my comments")
self.user = self.r.get_redditor(USERNAME)
self.log_in()
self.run()
def run(self):
try:
self.get_posts()
self.get_comments()
except praw.errors.RateLimitExceeded as error:
print('\tSleeping for %d seconds' % error.sleep_time)
time.sleep(error.sleep_time)
except requests.exceptions.ConnectionError as error:
raise error
except KeyboardInterrupt:
raise
except:
raise
def log_in(self):
disable_warning=True
while not self.logged_in:
try:
self.r.login(USERNAME, PASSWORD)
self.logged_in = True
print("logging in...")
except requests.exceptions.ConnectionError:
tag = 'No web connection.'
def get_posts(self):
print("getting posts...")
posts = self.user.get_submitted()
karma_by_subreddit = {}
for post in posts:
content = post.title
print "Saving post: " + content
self.save_things("post", content)
print "Deleting post: " + content
post.delete()
pprint.pprint(karma_by_subreddit)
def get_comments(self):
print("getting comments...")
comments = self.user.get_comments(limit=None)
karma_by_subreddit = {}
for comment in comments:
print "Saving comment: "
print comment
self.save_things("comment", comment)
print "Deleting post: "
print comment
comment.delete()
pprint.pprint(karma_by_subreddit)
# optional parse first line of each comment and post to a CSV file for postarity
def save_things(self, type, thing):
myfile = open(type + '.csv', 'a')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow([thing])
if __name__ == '__main__':
a_bot = Bot()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment