Skip to content

Instantly share code, notes, and snippets.

@dmueller39
Created February 7, 2017 11:15
Show Gist options
  • Save dmueller39/d227ca8d42458a8fc102676098424d70 to your computer and use it in GitHub Desktop.
Save dmueller39/d227ca8d42458a8fc102676098424d70 to your computer and use it in GitHub Desktop.
A simple reddit json requester written in python
import urllib2
import json
import time
def get(o, p, d=None):
keys = p.split('.')
rv = o
for key in keys:
if isinstance(rv, dict) and key in rv:
rv = rv[key]
else:
return d
return rv
def delayed_urlopen(url):
# throttle connections by 2 secs, otherwise you might get a 429
time.sleep(2)
hdr = { 'User-Agent' : 'subreddit recommender by /u/_saltymule_' }
req = urllib2.Request(url, headers=hdr)
return urllib2.urlopen(req)
def get_hot(subreddit):
url = "https://www.reddit.com/r/"+subreddit+"/hot.json"
response = delayed_urlopen(url)
data = json.load(response)
return [post["data"] for post in get(data, "data.children", [])]
def realize_commentlist(comment):
topitem = get(comment, "data", None)
if topitem is None:
return []
result = [comment["data"]]
for reply in get(comment, "data.replies.data.children", []):
result.extend(realize_commentlist(reply))
return result
def get_postcomments(permalink):
url = "https://www.reddit.com"+permalink+".json"
response = delayed_urlopen(url)
data = json.load(response)
result = []
# I feel like there is a way to do this more pythonically
for listing in data:
for child in get(listing, "data.children", []):
if child["kind"] == "t1":
result.extend(realize_commentlist(child))
return result
def get_usercomments(username):
url = "https://www.reddit.com/user/"+username+"/comments/.json?count=100"
response = delayed_urlopen(url)
data = json.load(response)
return [comment["data"] for comment in get(data, "data.children", [])]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment