Last active
July 2, 2016 18:26
-
-
Save nabeelio/194e4b9cecc85a685b57ae9d6b454641 to your computer and use it in GitHub Desktop.
quick script for a friend to write out a reddit thread
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
# do a "pip install praw" on the command line | |
import sys | |
import praw | |
import textwrap | |
def print_thread(comment, indent=0): | |
""" | |
write out this comment, and then check if there are replies | |
and if there are replies, we call this recursively, adding another | |
indent for each level | |
""" | |
if isinstance(comment, praw.objects.MoreComments): | |
return | |
# one space * number of indents, add a - | |
indents = (' ' * indent * 2) | |
wrapper = textwrap.TextWrapper(width=100, | |
initial_indent=indents + '- ', | |
subsequent_indent=indents + ' ') | |
for line in wrapper.wrap(comment.body): | |
print(line) | |
# check if there are replies. yay, recursion! | |
for sub_comment in comment.replies: | |
print_thread(sub_comment, indent + 1) | |
def main(): | |
r = praw.Reddit('Comment Scraper 1.0 by u/_Daimon_ see ' | |
'https://praw.readthedocs.org/en/latest/' | |
'pages/comment_parsing.html') | |
# get from the command line the thread ID. otherwise, | |
# just do some default thread id for giggles | |
if len(sys.argv) > 1: | |
sub_id = sys.argv[1] | |
else: | |
sub_id ='4qwvvw' | |
# read the submission | |
submission = r.get_submission(submission_id=sub_id) | |
# write out the title and body | |
print(submission.title) | |
print('----') | |
print(submission.selftext) | |
print('------------------------------') | |
# write out all the comments. win! | |
comments = praw.helpers.flatten_tree(submission.comments) | |
for comment in comments: | |
if isinstance(comment, praw.objects.MoreComments): | |
continue | |
print('------------------') | |
print_thread(comment) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment