Created
January 29, 2021 01:56
-
-
Save ejdoh1/c944c96ea9c1258508cd091c1c361c08 to your computer and use it in GitHub Desktop.
Write all Reddit comments for a submission to a file
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 sys | |
import argparse | |
import praw | |
from praw.models import MoreComments | |
import json | |
SUBMISSION = 'l79x17' | |
def main(arguments): | |
parser = argparse.ArgumentParser( | |
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) | |
parser.add_argument('-s', '--submission', | |
help="Submission ID", default=SUBMISSION) | |
parser.add_argument('-f', '--filename', | |
help="Filename", default=f'{SUBMISSION}.json') | |
parser.add_argument('-a', '--app', | |
help="Reddit app name", required=True) | |
parser.add_argument('-u', '--user', | |
help="Reddit username", required=True) | |
parser.add_argument('-c', '--client', | |
help="Reddit app client ID", required=True) | |
parser.add_argument('-e', '--secret', | |
help="Reddit app client ID secret", required=True) | |
args = parser.parse_args(arguments) | |
r = praw.Reddit( | |
user_agent=f'{args.app} by {args.user}', | |
client_id=args.client, | |
client_secret=args.secret, | |
) | |
s = r.submission(args.submission) | |
count = 0 | |
all_comments = [] | |
for c in s.comments: | |
if isinstance(c, MoreComments): | |
s.comments.replace_more() | |
continue | |
count += 1 | |
all_comments.append(c.body) | |
print(count) | |
with open(args.filename, 'w') as f: | |
json.dump(all_comments, f) | |
if __name__ == '__main__': | |
sys.exit(main(sys.argv[1:])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment