Skip to content

Instantly share code, notes, and snippets.

@offby1
Created November 18, 2017 22:59
Show Gist options
  • Save offby1/a17527271312e451979bfd45d9ed3c19 to your computer and use it in GitHub Desktop.
Save offby1/a17527271312e451979bfd45d9ed3c19 to your computer and use it in GitHub Desktop.
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