# This seems to work as of 4 June 2023.
# You need python 3.7++ and the praw library
# and you'll also need to have a client id and secret for reddit which should be straightforward to make

import json
import praw

def comment_to_dict(comment):
   # Store a few important details of the comment, I don't care about the other details that reddit returns
   return {"link_title" : comment.link_title,
       "link_id" : comment.id,
       "subreddit_id" : comment.subreddit_id,
       "subreddit" : comment.subreddit.display_name,
       "comment_id" : comment.id,
       "parent_id" : comment.parent_id,
       "score" : comment.score,
       "body" : comment.body,
       "created" : comment.created_utc}

def comment_to_json(comment):
   # dump to a json, ensure_ascii keeps greek characters
   return json.dumps(comment_to_dict(comment), indent=4, ensure_ascii=False)

myreddit = praw.Reddit(client_id='YOURCLIENTID', \
                    client_secret='YOURCLIENTSECRET', \
                    user_agent='YOURUSERAGENTNAME', \
                    username='YOURREDDITUSERNAME', \
                    password='YOURREDDITPASSWORD')

myreddit.config.store_json_result = True
myreddit.validate_on_submit = True


user = myreddit.redditor('YOURREDDITUSERNAME')


# we'll replace all comments with this text.
# we'll insert the original comment's id in the text to make matching it with the 
# 'database' we've stored locally more easy
replace_with = """
#!> {}   
## This comment has been edited in protest to reddit's decision to bully 3rd party apps into closure.       
If you want to do the same, you can find instructions here:   
http://notepad.link/share/rAk4RNJlb3vmhROVfGPV
"""


with open("./my_comments_database.json", "w") as targetfile:
   count = 0
   for comment in user.comments.new(limit=None):

       # if we've already edited this comment, move to the next one
       if "!#>" in comment.body:
           continue

       #dump original content in local file:
       print(comment_to_json(comment), file=targetfile)

       # edit the comment in reddit
       comment.edit(replace_with.format(comment.id))

       # remove this to let the script run for ALL of your comments instead of the last 5
       count +=1
       if count >=5:
           break

Leave a comment




Comments

GrandmaRosie'

Just checkers

GrandmaRosie'

Just checkers