Last active
August 29, 2015 14:16
-
-
Save Echocage/aa41f5073d061966a712 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 praw | |
def _get_question_comment(comment): | |
if comment.is_root: | |
return | |
parent_comment = r.get_info(thing_id=comment.parent_id) | |
if parent_comment.is_root: | |
return parent_comment, comment | |
def _format_interaction(interaction): | |
question, answer = interaction | |
return '{} | {}'.format(question.body, answer.body).replace('\n', ' ').replace('\r', ' ') | |
def get_interactions(parent_submission): | |
""" | |
Searches for questions answered by OP in parent_submission | |
""" | |
op = parent_submission.author | |
is_child_of_submission = lambda comment: comment.link_id == parent_submission.fullname | |
submission_comments = filter(is_child_of_submission, op.get_comments(limit=100)) | |
conversations = filter(None, map(_get_question_comment, submission_comments)) | |
yield from conversations | |
def create_table(conversations): | |
""" | |
:returns: Table of question and answer pairs in reddit's markup language | |
""" | |
table = 'Question | Answer\n---|---\n' | |
conversations = map(_format_interaction, conversations) | |
return table + '\n'.join(conversations) | |
r = praw.Reddit('Collects and displays AMA results' | |
'by /u/echocage') | |
if __name__ == '__main__': | |
sub = r.get_subreddit('IAMA') | |
for submission in sub.get_hot(): | |
interactions = get_interactions(submission) | |
print(create_table(interactions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment