Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. # This is a script to delete every comment and post on a reddit account.
  2. # You may have to run it several times, depending on how many comments and posts you have on your user, due to
  3. # the batching process of reddit's API.
  4.  
  5. import praw, csv, pprint
  6.  
  7. USERNAME = ""
  8. PASSWORD = ""
  9.  
  10. class Bot:
  11.  
  12. def __init__(self):
  13. print("Initializing Bot")
  14. self.logged_in = False
  15. self.r = praw.Reddit(user_agent = "script to delete my comments")
  16. self.user = self.r.get_redditor(USERNAME)
  17. self.log_in()
  18. self.run()
  19.  
  20. def run(self):
  21. try:
  22. self.get_posts()
  23. self.get_comments()
  24. except praw.errors.RateLimitExceeded as error:
  25. print('\tSleeping for %d seconds' % error.sleep_time)
  26. time.sleep(error.sleep_time)
  27. except requests.exceptions.ConnectionError as error:
  28. raise error
  29. except KeyboardInterrupt:
  30. raise
  31. except:
  32. raise
  33.  
  34. def log_in(self):
  35. disable_warning=True
  36. while not self.logged_in:
  37. try:
  38. self.r.login(USERNAME, PASSWORD)
  39. self.logged_in = True
  40. print("logging in...")
  41. except requests.exceptions.ConnectionError:
  42. tag = 'No web connection.'
  43.  
  44. def get_posts(self):
  45. print("getting posts...")
  46. posts = self.user.get_submitted()
  47. karma_by_subreddit = {}
  48. for post in posts:
  49. content = post.title
  50. print "Saving post: " + content
  51. self.save_things("post", content)
  52. print "Deleting post: " + content
  53. post.delete()
  54.  
  55. pprint.pprint(karma_by_subreddit)
  56.  
  57. def get_comments(self):
  58. print("getting comments...")
  59. comments = self.user.get_comments(limit=None)
  60. karma_by_subreddit = {}
  61. for comment in comments:
  62. print "Saving comment: "
  63. print comment
  64. self.save_things("comment", comment)
  65. print "Deleting post: "
  66. print comment
  67. comment.delete()
  68.  
  69. pprint.pprint(karma_by_subreddit)
  70.  
  71. # optional parse first line of each comment and post to a CSV file for postarity
  72. def save_things(self, type, thing):
  73. myfile = open(type + '.csv', 'a')
  74. wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
  75. wr.writerow([thing])
  76.  
  77. if __name__ == '__main__':
  78. a_bot = Bot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement