Last active
December 16, 2015 11:49
-
-
Save novel/5429964 to your computer and use it in GitHub Desktop.
This file contains 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 | |
import sys | |
import urllib | |
import json | |
URL = "http://beta-api.formspring.me/answered/list/%s" | |
def formspring_fetch(account, iteration=1, before=None): | |
url = URL % account | |
if before: | |
url += "?before=%s" % before | |
response = urllib.urlopen(url) | |
response_data = response.read() | |
json_data = json.loads(response_data) | |
if 0 == len(json_data['response']): | |
print "Done!" | |
return | |
output_filename = "%.3d.json" % iteration | |
with open(output_filename, 'w') as output_fd: | |
print "Writing data to", output_filename | |
output_fd.write(response_data) | |
before = json_data['response'][-1]['id'] | |
iteration += 1 | |
formspring_fetch(account, iteration, before) | |
if "__main__" == __name__: | |
if 2 != len(sys.argv): | |
print >> sys.stderr, "Usage: %s account" % sys.argv[0] | |
sys.exit(1) | |
account = sys.argv[1] | |
formspring_fetch(account) |
This file contains 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 | |
import sys | |
import json | |
import locale | |
system_encoding = locale.getpreferredencoding() | |
def load_data(files): | |
responses = [] | |
for file in files: | |
with open(file) as fd: | |
responses += json.loads(fd.read())['response'] | |
return responses | |
def data_to_html(data): | |
print "<html><body><div>" | |
for response in data: | |
print ("<p><i>%(question)s @ %(time)s</i></p><p>%(answer)s</p>" % | |
response).encode(system_encoding) | |
print "</div></body></html>" | |
if "__main__" == __name__: | |
if 2 > len(sys.argv): | |
print >> sys.stderr, "Usage: %s file1 ... fileN" % sys.argv[0] | |
sys.exit(1) | |
files = sys.argv[1:] | |
data = load_data(files) | |
data_to_html(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment