Created
December 30, 2011 09:16
-
-
Save peterjmag/1538914 to your computer and use it in GitHub Desktop.
A terribly inefficient script to determine the depth of a comment in a Reddit thread
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
reddit-comment-depth.py | |
Created by peterjmag on 2011-12-30. | |
""" | |
import urllib2 | |
import simplejson as json | |
import time | |
def get_comment(url): | |
api_url = url + '.json' | |
raw = urllib2.urlopen(api_url).read() | |
json_object = json.loads(raw) | |
comment = json_object[1]['data']['children'][0]['data'] | |
return comment | |
def main(): | |
url = raw_input('Comment Permalink URL: ') | |
if url[-1] is '/': | |
url = url[:-1] | |
base_url = url.replace(url.split('/')[-1], '') | |
comment = get_comment(url) | |
print comment['id'], ':', comment['body'] | |
d = 1 | |
while 't1_' in comment['parent_id']: | |
parent_url = base_url + comment['parent_id'].replace('t1_', '') | |
time.sleep(2) # Ghetto rate throttle per Reddit's API rules | |
comment = get_comment(parent_url) | |
print comment['id'], ':', comment['body'] | |
d += 1 | |
if d is 1: | |
print d, 'comment deep' | |
else: | |
print d, 'comments deep' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment