Last active
January 31, 2019 22:48
-
-
Save paultopia/31846c98174a60420e726d580ad90be8 to your computer and use it in GitHub Desktop.
quick and dirty commandline tool to download a case by citation from caselaw access project (https://case.law). Requires API key registration.
This file contains hidden or 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
| import requests, json, string, sys, os | |
| endpoint = "https://api.case.law/v1/cases/" | |
| apikey = os.environ["CASELAW"] # put your case.law api key here | |
| headers = {"Authorization": "Token " + apikey} | |
| def depunctuate(st): | |
| return st.translate(str.maketrans(dict.fromkeys(string.punctuation))) | |
| def get_opinion_texts(api_response): | |
| ops = api_response["results"][0]["casebody"]["data"]["opinions"] | |
| doublespaced_ops = ["\n\n".join(x["text"].split("\n")) for x in ops] | |
| return "\n\n<hr>\n\n".join([x for x in doublespaced_ops]) | |
| def get_name(api_response): | |
| try: | |
| shortname = api_response["results"][0]["name_abbreviation"] | |
| except: | |
| shortname = "" | |
| longname = api_response["results"][0]["name_abbreviation"] | |
| if shortname: | |
| return shortname | |
| return longname | |
| def get_cites(api_response): | |
| cites = [x["cite"] for x in api_response["results"][0]["citations"]] | |
| return "\n\n".join(cites) | |
| def get_date(api_response): | |
| date = api_response["results"][0]["decision_date"] | |
| return date | |
| def get_case(cite): | |
| resp = requests.get(endpoint, headers=headers, params={"cite": cite, "full_case": "true"}).json() | |
| opinions = get_opinion_texts(resp) | |
| name = get_name(resp) | |
| cites = get_cites(resp) | |
| date = get_date(resp) | |
| text = name + "\n\n" + cites + "\n\n" + date + "\n\n" + opinions | |
| filename = depunctuate(name) + "_" + depunctuate(cites) + ".md" | |
| return (text, filename.replace(" ", "_")) | |
| if __name__ == "__main__": | |
| toget = sys.argv[1] | |
| text, filename = get_case(toget) | |
| with open(filename, "w") as f: | |
| f.write(text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment