|
""" |
|
Cannabis Licenses | Get Missouri Licenses |
|
Copyright (c) 2023 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 4/26/2023 |
|
Updated: 8/13/2023 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Collect Missouri cannabis license data. |
|
|
|
Requirements: |
|
|
|
The script leverages Google Maps to attempt to geocode license |
|
addresses. Ensure that you have a `.env` file with a valid |
|
Google Maps API specified as `GOOGLE_MAPS_API_KEY`. |
|
|
|
Command-line Usage: |
|
|
|
python get_licenses_mo.py --data_dir <DATA_DIR> --env <ENV_FILE> |
|
|
|
Data Source: |
|
|
|
- Missouri Medical Cannabis Licenses |
|
URL: <https://health.mo.gov/safety/medical-marijuana/licensed-facilities.php> |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import os |
|
import re |
|
from typing import Optional |
|
|
|
|
|
from bs4 import BeautifulSoup |
|
from cannlytics.data.gis import geocode_addresses |
|
from dotenv import dotenv_values |
|
import numpy as np |
|
import pandas as pd |
|
import requests |
|
import zipcodes |
|
|
|
|
|
|
|
DATA_DIR = '../data/mo' |
|
ENV_FILE = '../../../.env' |
|
|
|
|
|
STATE = 'MO' |
|
MISSOURI = { |
|
'licensing_authority_id': 'MDHSS', |
|
'licensing_authority': 'Missouri Department of Health and Senior Services', |
|
'licenses_url': 'https://health.mo.gov/safety/cannabis/licensed-facilities.php', |
|
'columns': { |
|
'Medical': 'medical', |
|
'Comprehensive': 'adult_use', |
|
'Approved to Operate': 'license_status', |
|
'License \nNumber': 'license_number', |
|
'Entity Name': 'business_legal_name', |
|
'City': 'premise_city', |
|
'State': 'premise_state', |
|
'Postal Code': 'premise_zip_code', |
|
' Contact \nInformation 1': 'first_name', |
|
'Contact \nInformation 1': 'first_name', |
|
'Contact \nInformation 2': 'last_name', |
|
'Contact \nPhone': 'business_phone' |
|
}, |
|
'drop': ['first_name', 'last_name'], |
|
} |
|
|
|
|
|
def format_phone_number(x): |
|
"""Format phone numbers as ###-###-####.""" |
|
digits = re.sub(r'\D', '', x) |
|
return '{}-{}-{}'.format(digits[:3], digits[3:6], digits[6:]) |
|
|
|
|
|
def get_gis_data(df: pd.DataFrame, api_key: str) -> pd.DataFrame: |
|
"""Get GIS data.""" |
|
drop_cols = ['state', 'state_name', 'address', 'formatted_address'] |
|
rename_cols = { |
|
'county': 'premise_county', |
|
'latitude': 'premise_latitude', |
|
'longitude': 'premise_longitude' |
|
} |
|
df = geocode_addresses(df, api_key=api_key, address_field='address') |
|
get_city = lambda x: x.split(', ')[1].split(',')[0] if STATE in str(x) else x |
|
df['premise_city'] = df['formatted_address'].apply(get_city) |
|
df.drop(columns=drop_cols, inplace=True) |
|
return df.rename(columns=rename_cols) |
|
|
|
|
|
def get_licenses_mo( |
|
data_dir: Optional[str] = None, |
|
env_file: Optional[str] = '.env', |
|
): |
|
"""Get Missouri cannabis license data.""" |
|
|
|
|
|
config = dotenv_values(env_file) |
|
google_maps_api_key = config.get('GOOGLE_MAPS_API_KEY') |
|
if google_maps_api_key is None: |
|
print('Proceeding without `GOOGLE_MAPS_API_KEY`.') |
|
|
|
|
|
download_dir = os.path.join(data_dir, '.datasets') |
|
if not os.path.exists(download_dir): |
|
os.makedirs(download_dir) |
|
|
|
|
|
url = MISSOURI['licenses_url'] |
|
response = requests.get(url) |
|
soup = BeautifulSoup(response.content, 'html.parser') |
|
|
|
|
|
links = soup.find_all('a') |
|
xlsx_links = [x for x in links if x.get('href').endswith('.xlsx')] |
|
|
|
|
|
datafiles = [] |
|
base = 'https://health.mo.gov/safety/cannabis/xls/' |
|
for link in xlsx_links: |
|
file_url = base + link.get('href').split('/')[-1] |
|
file_name = os.path.join(download_dir, os.path.basename(file_url)) |
|
with open(file_name, 'wb') as file: |
|
response = requests.get(file_url) |
|
file.write(response.content) |
|
datafiles.append(file_name) |
|
|
|
|
|
licenses = [] |
|
for datafile in datafiles: |
|
|
|
|
|
license_type = datafile.split('\\')[-1].replace('.xlsx', '') \ |
|
.replace('licensed-', '') \ |
|
.replace('-facilities', '') |
|
|
|
|
|
data = pd.read_excel(datafile, skiprows=1) |
|
|
|
|
|
data.rename(columns=MISSOURI['columns'], inplace=True) |
|
|
|
|
|
data['license_status'] = data['license_status'].notna() \ |
|
.map({True: 'Active', False: 'Inactive'}) |
|
|
|
|
|
try: |
|
data['medical'] = data['medical'].notna().map({True: True, False: False}) |
|
data['adult_use'] = data['adult_use'].notna().map({True: True, False: False}) |
|
conditions = [ |
|
(data['medical'] & data['adult_use']), |
|
(data['medical']), |
|
(data['adult_use']) |
|
] |
|
choices = [ |
|
'medical and adult-use', |
|
'medical', |
|
'adult-use' |
|
] |
|
data['license_designation'] = np.select(conditions, choices, default=None) |
|
except KeyError: |
|
data['license_designation'] = 'adult-use' |
|
|
|
|
|
data['business_owner_name'] = data['first_name'].str.cat( |
|
data['last_name'], |
|
sep=' ', |
|
) |
|
|
|
|
|
data['business_phone'] = data['business_phone'].apply(str).apply(format_phone_number) |
|
|
|
|
|
unnamed = [x for x in data.columns if re.match('^Unnamed', x)] |
|
to_drop = MISSOURI['drop'] + unnamed |
|
data.drop(to_drop, axis=1, inplace=True) |
|
|
|
|
|
data['address'] = data['business_legal_name'] + ', ' + data['premise_city'] + ', ' + data['premise_state'] + ' ' + data['premise_zip_code'].astype(str) |
|
data = get_gis_data(data, google_maps_api_key) |
|
|
|
|
|
get_county = lambda x: zipcodes.matching(x)[0]['county'] |
|
data['county'] = data['premise_zip_code'].astype(str).apply(get_county) |
|
|
|
|
|
data = data.assign( |
|
id=data['license_number'].astype(str), |
|
business_dba_name=data['business_legal_name'], |
|
licensing_authority_id=MISSOURI['licensing_authority_id'], |
|
licensing_authority=MISSOURI['licensing_authority'], |
|
premise_state=STATE, |
|
license_status_date=None, |
|
license_type=license_type, |
|
license_term=None, |
|
issue_date=None, |
|
expiration_date=None, |
|
business_structure=None, |
|
activity=None, |
|
parcel_number=None, |
|
business_image_url=None, |
|
) |
|
|
|
|
|
data['data_refreshed_date'] = datetime.now().isoformat() |
|
|
|
|
|
data.sort_index(axis=1, inplace=True) |
|
|
|
|
|
if data_dir is not None: |
|
if not os.path.exists(data_dir): |
|
os.makedirs(data_dir) |
|
date = datetime.now().isoformat()[:10] |
|
outfile = f'{data_dir}/{license_type}-{STATE.lower()}-{date}.csv' |
|
data.to_csv(outfile, index=False) |
|
|
|
|
|
licenses.append(data) |
|
|
|
|
|
licenses = pd.concat(licenses) |
|
if data_dir is not None: |
|
date = datetime.now().isoformat()[:10] |
|
licenses.to_csv(f'{data_dir}/licenses-{STATE.lower()}-{date}.csv', index=False) |
|
licenses.to_csv(f'{data_dir}/licenses-{STATE.lower()}-latest.csv', index=False) |
|
|
|
|
|
return licenses |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
import argparse |
|
try: |
|
arg_parser = argparse.ArgumentParser() |
|
arg_parser.add_argument('--d', dest='data_dir', type=str) |
|
arg_parser.add_argument('--data_dir', dest='data_dir', type=str) |
|
arg_parser.add_argument('--env', dest='env_file', type=str) |
|
args = arg_parser.parse_args() |
|
except SystemExit: |
|
args = {'data_dir': DATA_DIR, 'env_file': ENV_FILE} |
|
|
|
|
|
data_dir = args.get('d', args.get('data_dir')) |
|
env_file = args.get('env_file') |
|
data = get_licenses_mo(data_dir, env_file=env_file) |
|
|