Skip to content

Instantly share code, notes, and snippets.

@bellydrum
Last active October 25, 2019 18:58
Show Gist options
  • Save bellydrum/cc078e20bb9fe67e681c08a1115406ed to your computer and use it in GitHub Desktop.
Save bellydrum/cc078e20bb9fe67e681c08a1115406ed to your computer and use it in GitHub Desktop.
Replace all of your reddit comments with a customized text.
"""""""""""""""
ABOUT THIS SCRIPT
- here's an easy way to gain access to your reddit account from the python shell using the praw library.
-praw is a python library for accessing reddit.
- https://praw.readthedocs.io/
- written by /u/b3llydrum
- https://github.com/bellydrum
"""""""""""""""
"""""""""""""""
HOW TO USE THIS SCRIPT
1. create a reddit app from your reddit user account (see help below).
- Here's a direct link to where you can do this: https://www.reddit.com/prefs/apps
2. once created, view your app credentials and assign them to the global variables below.
3. read the praw documentation to learn what you can do with this power.
- https://praw.readthedocs.io/en/latest/code_overview/praw_models.html
HELP FOR CREATING THE REDDIT APP
when creating an app, specify that it's a script:
[ ] web app
[ ] installed app
[x] script
and make sure the redirect uri is http://localhost:8080
"""""""""""""""
import praw
# replace these empty strings with the proper values.
CLIENT_ID = '' # given when app is created
CLIENT_SECRET = '' # given when app is created
PASSWORD = '' # your reddit account password
USER_AGENT = '' # name you gave to your app (eg. 'comment_nuker')
USERNAME = '' # your reddit account username
comment_counter = 0
# create a single piece of text to replace each of your reddit comments with.
REPLACEMENT_TEXT = """
This comment text has been replaced using [this](https://gist.github.com/bellydrum/cc078e20bb9fe67e681c08a1115406ed) script.
"""
COMMENT_REPLACED_MSG = 'Replaced comment text.'
COMMENT_ALREADY_REPLACED_MSG = 'This comment text has already been replaced.'
REPLACING_COMPLETE_MSG = f'All comments have been replaced! Here is their new text:\n{REPLACEMENT_TEXT}\n'
# create the reddit instance using the above credentials.
reddit = praw.Reddit(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
password=PASSWORD,
user_agent=USER_AGENT,
username=USERNAME
)
### WARNING --
### this is the part that actually replaces your comment text.
### there will be double- and triple-checking to ensure all comments are covered.
### depending on your comment history, this might take a while. grab a coffee!
def replace_comment_text(comment, replacement_text, counter=comment_counter):
comment.edit(replacement_text)
print('\nSorting comments by new...\n')
for comment in reddit.redditor(USERNAME).comments.new(limit=None):
if comment.body.strip() != REPLACEMENT_TEXT.strip():
replace_comment_text(comment, REPLACEMENT_TEXT)
comment_counter += 1
print('Replaced comment text, making a total of {} comments replaced.'.format(comment_counter))
else:
print(COMMENT_ALREADY_REPLACED_MSG)
for time_filter in ['all', 'year', 'month', 'week']:
print(f'\nNow sorting comments by top/{time_filter}...\n')
for comment in reddit.redditor(USERNAME).comments.top(time_filter=time_filter):
if comment.body.strip() != REPLACEMENT_TEXT.strip():
replace_comment_text(comment, REPLACEMENT_TEXT)
comment_counter += 1
print('Replaced comment text, making a total of {} comments replaced.'.format(comment_counter))
else:
print(COMMENT_ALREADY_REPLACED_MSG)
print(REPLACING_COMPLETE_MSG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment