This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathutils.py
128 lines (94 loc) · 3.72 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import requests
import zipfile
import certifi
import urllib3
from urllib3.exceptions import MaxRetryError
from tqdm import tqdm
try:
from config import settings
except ImportError:
# error triggered on setup.
# settings should exist after.
pass
CVIOLET = '\33[35m'
CEND = '\33[0m'
CURL = '\33[4m'
def get_api_url(resource=None):
root_url = "%s/%s/" % (settings.API_URL, settings.API_VERSION)
if not resource:
return root_url
return root_url + resource + '/'
def get_jurisdictions():
url = get_api_url() + 'jurisdictions'
response = requests.get(url)
return response.json()['results']
def print_info(instruction):
"""
Colorize print output for instructions
"""
print(CVIOLET + instruction + CEND)
def get_cases_from_bulk(jurisdiction="Illinois", data_format="json"):
body_format = "xml" if data_format == "xml" else "text"
bulk_url = settings.API_BULK_URL + "/?body_format=%s&filter_type=jurisdiction" % body_format
bulk_api_results = requests.get(bulk_url)
found = False
for jur in bulk_api_results.json()['results']:
if jurisdiction in jur['file_name']:
found = True
break
if not found:
raise Exception("Jurisdiction not found. Please check spelling.")
filename = os.path.join(settings.DATA_DIR, jur['file_name'])
http = urllib3.PoolManager(
cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where())
headers = {'AUTHORIZATION': 'Token {}'.format(settings.API_KEY)}
try:
resp = http.request("GET", jur["download_url"],
preload_content=False,
headers=headers)
except MaxRetryError as err:
print("Writing of file was interrupted.\n\n%s" % err)
return
if resp.status != 200:
raise Exception("Something went wrong.\n\n%s" % resp.data)
print_info("downloading %s into ../data dir" % jur['file_name'])
with open(filename, 'wb') as f:
for chunk in tqdm(resp.stream(1024)):
f.write(chunk)
print_info("extracting %s into ../data dir" % jur['file_name'])
with zipfile.ZipFile(filename, 'r') as zip_ref:
zip_ref.extractall(settings.DATA_DIR)
print_info("Done.")
decompressed_dir = filename.split('.zip')[0]
return os.path.join(decompressed_dir + '/data/data.jsonl.xz')
def get_and_extract_from_bulk(jurisdiction="Illinois", data_format="json"):
dir_exists = False
data_format = "xml" if data_format == "xml" else "text" # xml or json
for filename in os.listdir(settings.DATA_DIR):
if jurisdiction in filename and "-" + data_format in filename:
if os.path.exists(os.path.join(settings.DATA_DIR, filename + '/data/data.jsonl.xz')):
dir_exists = True
break
if dir_exists:
dir_path = os.path.join(settings.DATA_DIR, filename)
else:
print_info("Getting compressed file for %s from /bulk endpoint.\nThis might take a while." % jurisdiction)
dir_path = get_cases_from_bulk(jurisdiction=jurisdiction, data_format=data_format)
compressed_file = os.path.join(settings.DATA_DIR, dir_path)
return compressed_file
def get_cases_from_api(**kwargs):
"""
Get back json of the first 100 cases unless a cursor argument is provided
"""
api_url = get_api_url(resource='cases')
filters = "?format=json&"
for key, val in kwargs.items():
filters += "%s=%s&" % (key, val)
api_url += filters
headers = {'AUTHORIZATION': 'Token {}'.format(settings.API_KEY)}
response = requests.get(api_url, headers=headers)
if response.status_code != 200:
raise Exception("Something went wrong.\n\n%s" % response.reason)
return response.json()