fbi-data-api 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fbi_api/__init__.py +1 -0
- fbi_api/config.yml +64 -0
- fbi_api/core.py +107 -0
- fbi_api/utils.py +9 -0
- fbi_data_api-0.1.0.dist-info/METADATA +54 -0
- fbi_data_api-0.1.0.dist-info/RECORD +8 -0
- fbi_data_api-0.1.0.dist-info/WHEEL +4 -0
- fbi_data_api-0.1.0.dist-info/licenses/LICENSE +21 -0
fbi_api/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .core import FBI
|
fbi_api/config.yml
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
state_abbrs:
|
|
2
|
+
- AL
|
|
3
|
+
- AK
|
|
4
|
+
- AZ
|
|
5
|
+
- AR
|
|
6
|
+
- CA
|
|
7
|
+
- CO
|
|
8
|
+
- CT
|
|
9
|
+
- DE
|
|
10
|
+
- FL
|
|
11
|
+
- GA
|
|
12
|
+
- HI
|
|
13
|
+
- ID
|
|
14
|
+
- IL
|
|
15
|
+
- IN
|
|
16
|
+
- IA
|
|
17
|
+
- KS
|
|
18
|
+
- KY
|
|
19
|
+
- LA
|
|
20
|
+
- ME
|
|
21
|
+
- MD
|
|
22
|
+
- MA
|
|
23
|
+
- MI
|
|
24
|
+
- MN
|
|
25
|
+
- MS
|
|
26
|
+
- MO
|
|
27
|
+
- MT
|
|
28
|
+
- NE
|
|
29
|
+
- NV
|
|
30
|
+
- NH
|
|
31
|
+
- NJ
|
|
32
|
+
- NM
|
|
33
|
+
- NY
|
|
34
|
+
- NC
|
|
35
|
+
- ND
|
|
36
|
+
- OH
|
|
37
|
+
- OK
|
|
38
|
+
- OR
|
|
39
|
+
- PA
|
|
40
|
+
- RI
|
|
41
|
+
- SC
|
|
42
|
+
- SD
|
|
43
|
+
- TN
|
|
44
|
+
- TX
|
|
45
|
+
- UT
|
|
46
|
+
- VT
|
|
47
|
+
- VA
|
|
48
|
+
- WA
|
|
49
|
+
- WV
|
|
50
|
+
- WI
|
|
51
|
+
- WY
|
|
52
|
+
- DC
|
|
53
|
+
|
|
54
|
+
offenses:
|
|
55
|
+
Violent Crimes: V
|
|
56
|
+
Aggravated Assault: ASS
|
|
57
|
+
Burglary: BUR
|
|
58
|
+
Larceny: LAR
|
|
59
|
+
Motor Vehicle Theft: MVT
|
|
60
|
+
Homicide: HOM
|
|
61
|
+
Rape: RPE
|
|
62
|
+
Robbery: ROB
|
|
63
|
+
Arson: ARS
|
|
64
|
+
All Property Crimes: P
|
fbi_api/core.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import requests
|
|
3
|
+
import pandas as pd
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from .utils import load_yaml
|
|
6
|
+
|
|
7
|
+
class FBI:
|
|
8
|
+
base_url = "https://api.usa.gov/crime/fbi/cde"
|
|
9
|
+
config = load_yaml(Path(__file__).parent.joinpath("config.yml"))
|
|
10
|
+
|
|
11
|
+
def __init__(self, api_key: str = None):
|
|
12
|
+
'''
|
|
13
|
+
If no api_key is passed, it is invoked from environment variable "FBI_API_KEY."
|
|
14
|
+
'''
|
|
15
|
+
self.api_key = api_key
|
|
16
|
+
|
|
17
|
+
@staticmethod
|
|
18
|
+
def _get_state_abbrs() -> list:
|
|
19
|
+
return FBI.config["state_abbrs"]
|
|
20
|
+
|
|
21
|
+
@staticmethod
|
|
22
|
+
def _get_offenses() -> dict:
|
|
23
|
+
return FBI.config["offenses"]
|
|
24
|
+
|
|
25
|
+
def _get_api_key(self) -> str:
|
|
26
|
+
return os.getenv("FBI_API_KEY", default = self.api_key)
|
|
27
|
+
|
|
28
|
+
def _add_key_to_call(self, api_call: str) -> str:
|
|
29
|
+
prefix = "&" if "?" in api_call else "?"
|
|
30
|
+
|
|
31
|
+
auth_str = f"{prefix}API_KEY={self._get_api_key()}"
|
|
32
|
+
|
|
33
|
+
if not api_call.endswith(auth_str):
|
|
34
|
+
api_call = f"{api_call}{auth_str}"
|
|
35
|
+
|
|
36
|
+
return api_call
|
|
37
|
+
|
|
38
|
+
def get(self, api_call: str, timeout_limit: int = 10) -> dict:
|
|
39
|
+
api_call = self._add_key_to_call(api_call)
|
|
40
|
+
response = requests.get(api_call, timeout = timeout_limit)
|
|
41
|
+
|
|
42
|
+
return response.json() if response.status_code == 200 else None
|
|
43
|
+
|
|
44
|
+
def _oris_by_state(self, state_abbr: str) -> pd.DataFrame:
|
|
45
|
+
nested = self.get(f"{FBI.base_url}/agency/byStateAbbr/{state_abbr}")
|
|
46
|
+
|
|
47
|
+
flattened = []
|
|
48
|
+
|
|
49
|
+
for agencies in nested.values():
|
|
50
|
+
for agency in agencies:
|
|
51
|
+
flattened.append(agency)
|
|
52
|
+
|
|
53
|
+
return pd.DataFrame(flattened)
|
|
54
|
+
|
|
55
|
+
def get_metadata(self, state_abbr: str) -> pd.DataFrame:
|
|
56
|
+
'''
|
|
57
|
+
state_abbr: State abbreviation of desired state.
|
|
58
|
+
|
|
59
|
+
Extracts the metadata for a state, namely all law enforcement agencies that have provided data to the
|
|
60
|
+
Uniform Crime Reporting (UCR) program. If metadata for all states are desired, set state_abbr to "all."
|
|
61
|
+
'''
|
|
62
|
+
if state_abbr == "all":
|
|
63
|
+
results = []
|
|
64
|
+
|
|
65
|
+
for state in FBI._get_state_abbrs():
|
|
66
|
+
print(f"Extracting metadata for {state}...")
|
|
67
|
+
results.append(self._oris_by_state(state))
|
|
68
|
+
|
|
69
|
+
return pd.concat(results, ignore_index = True)
|
|
70
|
+
else:
|
|
71
|
+
return self._oris_by_state(state_abbr)
|
|
72
|
+
|
|
73
|
+
def get_crime_statistics(self, ori: str, year: int, offense: str) -> pd.DataFrame:
|
|
74
|
+
'''
|
|
75
|
+
ori: The originating agency identifier (ORI). Invoke get_metadata() to extract all ORIs in a state.
|
|
76
|
+
year: The year.
|
|
77
|
+
|
|
78
|
+
Extracts the monthly crime statistics reported by an agency.
|
|
79
|
+
'''
|
|
80
|
+
try:
|
|
81
|
+
offense_mapping = FBI._get_offenses()[offense]
|
|
82
|
+
api_call = self.get(
|
|
83
|
+
f"{FBI.base_url}/summarized/agency/{ori}/{offense_mapping}?from=01-{year}&to=12-{year}"
|
|
84
|
+
)
|
|
85
|
+
except KeyError:
|
|
86
|
+
raise KeyError(f"Valid offenses to pass are {", ".join(list(FBI._get_offenses().keys()))}.")
|
|
87
|
+
|
|
88
|
+
last_refresh_date = api_call["cde_properties"]["last_refresh_date"]["UCR"]
|
|
89
|
+
|
|
90
|
+
_, first_value = next(iter(api_call["offenses"]["actuals"].items()))
|
|
91
|
+
|
|
92
|
+
crime_statistics = pd.Series(first_value, name = "count").rename_axis("date").reset_index()
|
|
93
|
+
|
|
94
|
+
month_year_columns = ["month", "year"]
|
|
95
|
+
crime_statistics[month_year_columns] = crime_statistics["date"].str.split("-", expand = True)
|
|
96
|
+
crime_statistics.drop(columns = "date", inplace = True)
|
|
97
|
+
|
|
98
|
+
crime_statistics["ori"] = ori
|
|
99
|
+
crime_statistics["offense"] = offense
|
|
100
|
+
crime_statistics["last_refresh_date"] = last_refresh_date
|
|
101
|
+
|
|
102
|
+
for col in month_year_columns:
|
|
103
|
+
crime_statistics[col] = crime_statistics[col].astype(int)
|
|
104
|
+
|
|
105
|
+
crime_statistics.sort_values(by = month_year_columns, inplace = True)
|
|
106
|
+
|
|
107
|
+
return crime_statistics[["ori", "month", "year", "offense", "count", "last_refresh_date"]]
|
fbi_api/utils.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fbi-data-api
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Interface for extracting data assets from the FBI Crime Data API.
|
|
5
|
+
Project-URL: Homepage, https://github.com/teddythepooh/fbi_api
|
|
6
|
+
Project-URL: Issues, https://github.com/teddythepooh/fbi_api/issues
|
|
7
|
+
Author-email: "Ted Jesus C. Chua" <chuatedjesusc@gmail.com>
|
|
8
|
+
License: MIT License
|
|
9
|
+
|
|
10
|
+
Copyright (c) 2026 Ted Jesus C. Chua
|
|
11
|
+
|
|
12
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
13
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
14
|
+
in the Software without restriction, including without limitation the rights
|
|
15
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
16
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
17
|
+
furnished to do so, subject to the following conditions:
|
|
18
|
+
|
|
19
|
+
The above copyright notice and this permission notice shall be included in all
|
|
20
|
+
copies or substantial portions of the Software.
|
|
21
|
+
|
|
22
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
23
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
24
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
25
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
26
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
27
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
28
|
+
SOFTWARE.
|
|
29
|
+
License-File: LICENSE
|
|
30
|
+
Requires-Python: >=3.12
|
|
31
|
+
Requires-Dist: pandas>=3.0.1
|
|
32
|
+
Requires-Dist: python-dotenv>=1.2.2
|
|
33
|
+
Requires-Dist: pyyaml>=6.0.3
|
|
34
|
+
Requires-Dist: requests>=2.32.5
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
|
|
37
|
+
# FBI Crime Data API
|
|
38
|
+
This is a wrapper around the FBI Crime Data API, the first of its kind as of March 2026.
|
|
39
|
+
|
|
40
|
+
## Example
|
|
41
|
+
After signing up for an API key in https://api.data.gov/signup/,
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from fbi_api import FBI
|
|
45
|
+
|
|
46
|
+
# If no api_key is passed, your environment variable FBI_API_KEY is automatically invoked.
|
|
47
|
+
api = FBI(api_key = your_api_key)
|
|
48
|
+
|
|
49
|
+
metadata = api.get_metadata(state_abbr = "all")
|
|
50
|
+
|
|
51
|
+
# ORI stands for Originating Agency Identifer (ORI), uniquely identifying the law enforcement agencies that report
|
|
52
|
+
# to the FBI. The ORIs in a state can be extracted from api.get_metadata().
|
|
53
|
+
crime_statistics = api.get_crime_statistics(ori = "ILCPD0000", year = 2024, offense = "Violent Crimes")
|
|
54
|
+
```
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
fbi_api/__init__.py,sha256=IszPozU4iZtGN0h_qFTqblBcNh1uY2aeQX9Aq0KXdHs,23
|
|
2
|
+
fbi_api/config.yml,sha256=sA5X2Q3KTaATLnTAjZytFdlOxLO_hqdsWIlFiA1lSi8,630
|
|
3
|
+
fbi_api/core.py,sha256=NsMxAMw-2rh7buEW1vyY_tUWqkOm1X0IFC_z4nBmiTA,4150
|
|
4
|
+
fbi_api/utils.py,sha256=MzDeiVpGUO-jkFKZsQT0fY8-rKnFLMSoi7uV3WSC7D8,253
|
|
5
|
+
fbi_data_api-0.1.0.dist-info/METADATA,sha256=16DYz11kKT20XXlemfUlGdVTJfAQOow8CpymQd-msMw,2476
|
|
6
|
+
fbi_data_api-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
7
|
+
fbi_data_api-0.1.0.dist-info/licenses/LICENSE,sha256=BgOQYYuroNUl_qvYPRK6nS0UPI6R0l2xCs3b6r3CEKA,1095
|
|
8
|
+
fbi_data_api-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ted Jesus C. Chua
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|