Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DC helper script... #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openstates/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .lxmlize import LXMLMixin
from .dir import mkdir_p
import re


Expand Down
11 changes: 11 additions & 0 deletions openstates/utils/dir.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import errno
import os

def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
50 changes: 50 additions & 0 deletions scripts/dc/dump_bill_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'''Dump JSON for a DC bill

USAGE:

$ python dump_bill_json.py PR21-0316

'''
import json
import pprint
import sys

import requests

from openstates.utils import mkdir_p

def main():
headers = {"Content-Type":"application/json"}
bill_url = "http://lims.dccouncil.us/_layouts/15/uploader/AdminProxy.aspx/GetPublicData"
try:
bill_id = sys.argv[1]
except IndexError:
msg = "\nERROR: You must supply a bill id! Example:\n\n\tpython {} PR21-0316\n".format(__file__)
sys.exit(msg)
bill_params = { "legislationId" : bill_id }
bill_info = requests.post(bill_url, headers=headers, data=json.dumps(bill_params))
bill_info = decode_json(bill_info.json()["d"])["data"]
output_dir = 'data/dc/bills_raw/'
outfile = "".join([output_dir, "{}.json".format(bill_id)])
mkdir_p(output_dir)
with open(outfile, 'w') as out:
json.dump(bill_info, out, sort_keys=True, indent=4, separators=(',', ': '))
pprint.pprint(bill_info)
print("\nRaw bill data saved: {}\n".format(outfile))

def decode_json(stringy_json):
#the "json" they send is recursively string-encoded.
if type(stringy_json) == dict:
for key in stringy_json:
stringy_json[key] = decode_json(stringy_json[key])
elif type(stringy_json) == list:
for i in range(len(stringy_json)):
stringy_json[i] = decode_json(stringy_json[i])
elif type(stringy_json) in (str,unicode):
if len(stringy_json) > 0 and stringy_json[0] in ["[","{",u"[",u"{"]:
return decode_json(json.loads(stringy_json))
return stringy_json


if __name__ == '__main__':
main()