Created
November 18, 2017 22:59
-
-
Save offby1/a17527271312e451979bfd45d9ed3c19 to your computer and use it in GitHub Desktop.
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 requests | |
from pprint import pprint | |
postID = '7dn55x' | |
#c=requests.get('https://api.pushshift.io/reddit/search/comment/?ids=' + ','.join(requests.get('https://api.pushshift.io/reddit/submission/comment_ids/' + postID).json()['data'])).json()['data'] | |
import ast | |
c = ast.literal_eval(open ('/tmp/data').read ()) | |
class RedditComment (): | |
def __init__ (self, id_, parent_id, other_stuff): | |
self.id_ = id_ | |
self.parent_id = parent_id | |
self.other_stuff = other_stuff | |
self.children = [] | |
def __repr__ (self): | |
return '{} (child of {}): {}'.format (self.id_, self.parent_id, self.other_stuff['body']) | |
comments_by_id = {} | |
for comment_data in c: | |
comment = RedditComment ( | |
id_ = comment_data['id'], | |
parent_id = comment_data.get ('parent_id'), | |
other_stuff = comment_data | |
) | |
comments_by_id[comment.id_] = comment | |
if comment.parent_id and comment.parent_id in comments_by_id: | |
comments_by_id[comment.parent_id].children.append (comment) | |
pprint (comments_by_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment