Updated the json methods, didn\'t realise it has a load/dump for fh like objects

nopraw
David Trail 13 years ago
parent 4336be4429
commit 6e594edca3

@ -1,17 +1,13 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
import sys import sys
from json import loads, dumps import json # from json import loads, dumps
from urllib2 import urlopen, HTTPError from urllib2 import urlopen, HTTPError
from time import sleep from time import sleep
user = None f = open('user.json', 'r')
if len(sys.argv) == 2: user = json.load(f)['user']
user = sys.argv[1] f.close()
else:
f = open('user.json', 'r')
user = loads(f.read())['user']
f.close()
sub_section = 'comments' sub_section = 'comments'
after = '' after = ''
@ -20,19 +16,19 @@ init_url = 'http://www.reddit.com/user/{user}/comments/.json?after=%s'.format(us
next_url = init_url % after next_url = init_url % after
try: try:
http = urlopen(next_url).read() http = urlopen(next_url)
except HTTPError: except HTTPError:
raise HTTPError("You seem to have given an invalid user") raise HTTPError("You seem to have given an invalid user")
try: try:
json = loads(http) reddit = json.load(http)
except ValueError: except ValueError:
raise ValueError("Failed to decode json.") raise ValueError("Failed to decode json.")
datum = [] datum = []
while True: while True:
after = json['data']['after'] after = reddit['data']['after']
children = json['data']['children'] children = reddit['data']['children']
# This bit fills datum with the id (for removal) and the date (for saving recent posts) # This bit fills datum with the id (for removal) and the date (for saving recent posts)
for child in children: for child in children:
@ -44,10 +40,10 @@ while True:
break break
next_url = init_url % after next_url = init_url % after
http = urlopen(next_url).read() http = urlopen(next_url)
json = loads(http) reddit = json.load(http)
sleep(1) # don't want to hammer reddit to hard sleep(1) # don't want to hammer reddit to hard
f = open('data.json', 'w') f = open('data.json', 'w')
f.write(dumps(datum)) json.dump(datum, f)
f.close() f.close()

Loading…
Cancel
Save