Skip to content

Commit

Permalink
Merge pull request #1 from OrangeCardinal/foia
Browse files Browse the repository at this point in the history
Adding code for Freedom of Information Act
  • Loading branch information
OrangeCardinal committed Apr 23, 2019
2 parents a732728 + be64895 commit f566d52
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 37 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ Attempt at building the most complete collection of government data related func


## How to Use
1. Set the environment variable
1. Create a api key @ api.data.gov ()

2.
2. Set the environment variable API_DATA_GOV_KEY
Various government services require this key, as such it must be set for all sources to work.

3. Set the environment variable GOVERNMENT_DOWNLOAD_DIRECTORY
Various calls will download data as requested and will be written into the directory specified.

## Contributors
Who|Github Contact|What|
---|---|---|
Expand Down
9 changes: 0 additions & 9 deletions base/api.py

This file was deleted.

23 changes: 23 additions & 0 deletions foia_samples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from government.usa.foia.foia import FreedomOfInformationAct

foia = FreedomOfInformationAct()

#
dataset = foia.download_data_set(2016)


exit()

# Display All the Federal Agencies that are available via this API currently
for ac in foia.agency_components():
print(ac)


# This sample shows how to get all the various agency components, and then how to get each agencies
# request form
foia = FreedomOfInformationAct()
for ac in foia.agency_components():
ac_request_form = foia.request_form(ac.id)



File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions government/base/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import certifi
import urllib3

class API(object):

def get_response(self, url):
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())
response = http.request('GET', url)
return response



def get_file(self, url, path):

http = urllib3.PoolManager()
r = http.request('GET', url, preload_content=False)

with open(path, 'wb') as out:
while True:
data = r.read(1024)
if not data:
break
out.write(data)

r.release_conn()
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from base.api import API
from international.imf.imf_data_structure import IMF_DataStructure
from international.imf.imf_data_set import IMF_DataSet
from government.base import API
from government.international.imf.imf_data_structure import IMF_DataStructure
from government.international.imf.imf_data_set import IMF_DataSet
import json

class IMF(API):
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file added government/usa/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from base.api import API
from government.base import API
import json

class BureauOfLaborStatistics(API):
Expand Down
Empty file added government/usa/foia/__init__.py
Empty file.
25 changes: 25 additions & 0 deletions government/usa/foia/agency_component.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class AgencyComponent(object):
def __init__(self, data):
self.data = data
self.id = self.data['id']
for name, val in self.data['attributes'].items():
setattr(self, name, val)



def __str__(self):
readable_desc = "<AgencyComponent>\n"
readable_desc += "{0}\n{1}\n".format(self.title,self.id)
readable_desc += "Email: {0}\n".format(self.email)
readable_desc += "Telephone: {0}\n".format(self.telephone)

readable_desc += "Update Schedule\n"
readable_desc += "Scheduled Transition Date: {0}\n".format(self.scheduled_transition_date)
readable_desc += "Scheduled Publication: {0}\n".format(self.scheduled_publication)
readable_desc += "Scheduled Moderation State: {0}\n".format(self.scheduled_moderation_state)

readable_desc += "Reading Rooms\n"
for room in self.reading_rooms:
readable_desc += "{0}\n".format(room['uri'])

return readable_desc
60 changes: 60 additions & 0 deletions government/usa/foia/foia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
import os
from government.base.api import API
from government.usa.foia.agency_component import AgencyComponent

class FreedomOfInformationAct(API):
"""
Wrapper for the API Endpoints
References
https://www.foia.gov/developer/agency-api/
"""

def agency_components(self):
"""
Gets a list of Agency Components
:return:
"""

api_key = os.environ['API_DATA_GOV_KEY']


# Make the API Call parse the json data received
url = "https://api.foia.gov/api/agency_components?api_key={0}".format(api_key)
response = self.get_response(url)
json_response = json.loads(response.data)

# Format the data as warranted
processed_data = []
for agency_component in json_response['data']:
processed_data.append(AgencyComponent(agency_component))

return processed_data

def download_data_set(self, year):
"""
Downloads a compressed archive of xml data into the requested for a given year if available
:param year: Year to get data set for
:return: Path to Zip File | None
"""
zipname = "{0}-FOIASetFull.zip".format(year)
filename = "{0}\{1}".format(os.environ['GOVERNMENT_DATA_DOWNLOAD_DIRECTORY'],zipname)
url = "https://www.foia.gov/{0}".format(zipname)
self.get_file(url, filename)
print(filename)
return filename



def request_form(self, agency_id):
api_key = os.environ['API_DATA_GOV_KEY']
url = "https://api.foia.gov/api/agency_components/{0}/request_form?api_key={1}".format(agency_id, api_key)
print(url)
response = self.get_response(url)
json_response = json.loads(response.data)
print(json_response)



File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions samples/foia_samples.py

This file was deleted.

2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from international.imf.imf import IMF
from government.international.imf.imf import IMF

#bls = BureauOfLaborStatistics()
#popular_series = bls.popular_series()
Expand Down
18 changes: 0 additions & 18 deletions usa/foia.py

This file was deleted.

0 comments on commit f566d52

Please sign in to comment.