Created
October 24, 2011 17:10
-
-
Save faried/1309536 to your computer and use it in GitHub Desktop.
dump reddit links
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 | |
"""Dump reddit links. | |
Run me as something like | |
./dumpredditlinks http://www.reddit.com/user/self/submitted/.json | |
./dumpredditlinks http://www.reddit.com/r/programming | |
./dumpredditlinks \ | |
http://www.reddit.com/r/compsci \ | |
http://www.reddit.com/r/apple | |
""" | |
from json import loads | |
from urllib2 import urlopen | |
import sys | |
from time import sleep | |
def dumplinks(jsonurl): | |
"""Extract links from a reddit page.""" | |
tofetch = jsonurl | |
after = None | |
while True: | |
parsed = loads(urlopen(tofetch).read()) | |
for link in parsed['data']['children']: | |
print '%s %s' % (link['data']['url'], link['data']['title']) | |
after = parsed['data']['after'] | |
if after: | |
tofetch = '%s?after=%s' % (jsonurl, after) | |
sleep(2) | |
else: | |
break | |
def main(args): | |
"""Main branching logic.""" | |
if not len(args): | |
sys.stderr.write('''\ | |
usage: %s reddit-url/.json [reddit-url/.json ...] | |
''' % sys.argv[0].split('/')[-1]) | |
sys.exit(1) | |
for url in args: | |
if url.rfind('/.json') != -1: | |
dumplinks(url) | |
elif url.rfind('/') == len(url) - 1: | |
dumplinks(url + '.json') | |
else: | |
dumplinks(url + '/.json') | |
sleep(2) | |
if __name__ == '__main__': | |
main(sys.argv[1:]) | |
# eof |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment