Created
September 9, 2014 16:49
-
-
Save LeandroLovisolo/44a3495a90b8826f3635 to your computer and use it in GitHub Desktop.
Reddit user photos backup script
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 python2 | |
# coding: utf-8 | |
import argparse | |
import re | |
from urllib import urlretrieve | |
from urllib2 import urlopen | |
from os.path import isfile | |
def download_json_and_photos(url, url_filename, output_path): | |
p = re.compile(ur'(http://i.imgur.com/([a-zA-Z0-9]+.[a-zA-Z0-9]+))') | |
json = urlopen(url).read() | |
f = open(output_path + "/" + url_filename, "w+") | |
f.write(json) | |
f.close() | |
print "Downloading imgur photos..." | |
pics = re.findall(p, json) | |
current = 0 | |
for pic in pics: | |
current += 1 | |
print "[%d/%d]\t%s" % (current, len(pics), pic[0]) | |
filename = output_path + "/" + pic[1] | |
if not isfile(filename): urlretrieve (pic[0], filename) | |
def main(username, path): | |
print "Downloading submissions..." | |
download_json_and_photos( | |
"http://www.reddit.com/user/%s/submitted.json?limit=999999999" % username, | |
"submitted.json", | |
path) | |
print "Downloading comments..." | |
download_json_and_photos( | |
"http://www.reddit.com/user/%s/comments.json?limit=999999999" % username, | |
"comments.json", | |
path) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('username', help='reddit username') | |
parser.add_argument('path', help='output path') | |
args = parser.parse_args() | |
main(args.username, args.path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment