Skip to content

Instantly share code, notes, and snippets.

@SuperSonicHub1
Last active June 7, 2022 00:11
Show Gist options
  • Save SuperSonicHub1/5ab2e6fd237735fbb8a75d8cc360c8ef to your computer and use it in GitHub Desktop.
Save SuperSonicHub1/5ab2e6fd237735fbb8a75d8cc360c8ef to your computer and use it in GitHub Desktop.
Bill Iterator for Senator Jabari Brisport
"""Basic script that iterates over a list of bills using the NYS Senate OpenLegislation API:
https://legislation.nysenate.gov/. Requires Requests: `pip install requests`."""
__author__ = "Kyle Williams <[email protected]>"
# https://unlicense.org/
__license__ = "Unlicense"
from csv import DictReader, DictWriter
from os import environ
from requests import Session
# Constants
short_name = "BRISPORT"
passed_senate = "PASSED_SENATE"
passed_assembly = "PASSED_ASSEMBLY"
# Requests session
session = Session()
# API key is needed for all requests
session.params.update({"key": environ.get("API_KEY")})
def get_bill(print_no: str, session_year: int = 2022) -> dict:
"""Get information on a bill from the OpenLegislation API:
https://legislation.nysenate.gov/static/docs/html/bills.html#get-a-single-bill"""
res = session.get(f"https://legislation.nysenate.gov/api/3/bills/{session_year}/{print_no}")
res.raise_for_status()
return res.json()["result"]
def create_bill_info(print_no: str) -> dict:
"""Parse dictionary from `get_bill` in order to get necessary information."""
info = {}
bill = get_bill(print_no)
info["bill"] = bill["printNo"]
info["title"] = bill["title"]
sponsor_member = bill["sponsor"]["member"]
info["sponsor"] = sponsor_member["fullName"]
past_committees = bill["pastCommittees"]["items"]
# Use a set to avoid duplicates
info["committees"] = {committee["name"] for committee in past_committees}
# Iterate through all versions of a bill to see if Jabari is a co-sponsor
info["cosponsor"] = False
amendments = bill["amendments"]["items"]
for amendment in amendments.values():
cosponsors = amendment["coSponsors"]["items"]
for cosponsor in cosponsors:
if cosponsor["shortName"] == short_name:
info["cosponsor"] = True
break
if info["cosponsor"]:
break
# Iterate through all milestones to see if the senate and
# the assembly have approved of the bill
info["passed_senate"] = False
info["passed_assembly"] = False
milestones = bill["milestones"]["items"]
for milestone in milestones:
if milestone["statusType"] == passed_senate:
info["passed_senate"] = True
elif milestone["statusType"] == passed_assembly:
info["passed_assembly"] = True
return info
# Get all bills listed in CSV file
with open("cosponsors.csv", 'r') as f:
cosponsors = DictReader(f)
bill_ids = [row["Bill"] for row in cosponsors]
# Iterate over all of them
bills_info = [create_bill_info(print_no) for print_no in bill_ids]
# Write a new CSV file
with open("bills_info.csv", 'w') as f:
writer = DictWriter(f, fieldnames=bills_info[0].keys())
writer.writeheader()
for row in bills_info:
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment