Last active
June 17, 2017 04:32
-
-
Save looselycoupled/7eec8a9ce57f35ae3e42 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 | |
""" | |
Model answer for REST Workshop | |
NOTE: In order to use this program, be sure to create a sub-directory | |
called 'releases' | |
""" | |
########################################################################## | |
## Imports | |
########################################################################## | |
import os | |
import json | |
import requests | |
########################################################################## | |
## Module Variables/Constants | |
########################################################################## | |
DOJ_RELEASES_URL = 'http://www.justice.gov/api/v1/press_releases.json?pagesize=5' | |
########################################################################## | |
## Functions | |
########################################################################## | |
def fetch_press_releases(): | |
""" | |
Performs a GET on the DOJ web service and return the array found in the | |
'results' attribute of the JSON response | |
""" | |
# execute a GET request and store the results | |
response = requests.get(DOJ_RELEASES_URL) | |
# decode as json and store the results | |
data = response.json() | |
# return the 'results' array of press releases | |
return data['results'] | |
def main(): | |
""" | |
Main execution function to perform required actions | |
""" | |
# fetch array of press releases | |
press_releases = fetch_press_releases() | |
# iterate press releases | |
for release in press_releases: | |
path = './releases/%s.json' % release['uuid'] | |
content = json.dumps(release) | |
f = open(path, 'wb') | |
f.write(content.encode('utf-8')) | |
f.close() | |
########################################################################## | |
## Execution | |
########################################################################## | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment