Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
import urllib2
import json
last_timestamp_utc = 0
reddit_api = 'https://www.reddit.com/r/opendirectories/new.json?sort=new'
page_request = urllib2.urlopen(reddit_api)
page_content = json.loads(page_request.read())
posts = page_content['data']['children']
# API results are in order of newest to oldest, but we want to
# itterate over them from oldest to newest, so we reverse the
# order of the list here
posts.reverse()
# Check each post in the list of posts the API gave us
for i in posts:
# Skip posts from before our last post timestamp
if i['data']['created_utc'] <= last_timestamp_utc:
#print "Skipped: Presumably old post: " + str(i['data']['created_utc'])
continue
# Skip posts that are marked NSFW
if i['data']['over_18']:
#print "Skipped: Post is NSFW"
continue
# Skip if the post is a meta post
if i['data']['is_self']:
#print "Skipped: Post is metapost (self)"
continue
post_url = i['data']['url']
#print 'Valid post url: ' + post_url
print post_url
last_timestamp_utc = i['data']['created_utc']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment