File size: 6,312 Bytes
124701c 1352c88 124701c 1352c88 124701c 1352c88 124701c 1352c88 124701c 1352c88 124701c 1352c88 124701c 1352c88 124701c |
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
"""
Cannabis Licenses | Get Maine Licenses
Copyright (c) 2022 Cannlytics
Authors:
Keegan Skeate <https://github.com/keeganskeate>
Candace O'Sullivan-Sutherland <https://github.com/candy-o>
Created: 9/29/2022
Updated: 10/7/2022
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>
Description:
Collect Maine cannabis license data.
Data Source:
- Maine Office of Cannabis Policy
URL: <https://www.maine.gov/dafs/ocp/open-data/adult-use>
"""
# Standard imports.
from datetime import datetime
import os
from typing import Optional
# External imports.
from bs4 import BeautifulSoup
from cannlytics.data.gis import geocode_addresses
from dotenv import dotenv_values
import pandas as pd
import requests
# Specify where your data lives.
DATA_DIR = '../data/me'
ENV_FILE = '../.env'
# Specify state-specific constants.
STATE = 'ME'
MAINE = {
'licensing_authority_id': 'MEOCP',
'licensing_authority': 'Maine Office of Cannabis Policy',
'licenses': {
'url': 'https://www.maine.gov/dafs/ocp/open-data/adult-use',
'key': 'Adult_Use_Establishments_And_Contacts',
'columns': {
'LICENSE': 'license_number',
'LICENSE_CATEGORY': 'license_type',
'LICENSE_TYPE': 'license_designation',
'LICENSE_NAME': 'business_legal_name',
'DBA': 'business_dba_name',
'LICENSE_STATUS': 'license_status',
'LICENSE_CITY': 'premise_city',
'WEBSITE': 'business_website',
'CONTACT_NAME': 'business_owner_name',
'CONTACT_TYPE': 'contact_type',
'CONTACT_CITY': 'contact_city',
'CONTACT_DESCRIPTION': 'contact_description',
},
}
}
def get_licenses_me(
data_dir: Optional[str] = None,
env_file: Optional[str] = '.env',
):
"""Get Maine cannabis license data."""
# Create the necessary directories.
file_dir = f'{data_dir}/.datasets'
if not os.path.exists(data_dir): os.makedirs(data_dir)
if not os.path.exists(file_dir): os.makedirs(file_dir)
# Get the download link.
licenses_url = None
licenses_key = MAINE['licenses']['key']
url = MAINE['licenses']['url']
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
links = soup.find_all('a')
for link in links:
try:
href = link['href']
except KeyError:
continue
if licenses_key in href:
licenses_url = href
break
# Download the licenses workbook.
filename = licenses_url.split('/')[-1].split('?')[0]
licenses_source_file = os.path.join(file_dir, filename)
response = requests.get(licenses_url)
with open(licenses_source_file, 'wb') as doc:
doc.write(response.content)
# Extract the data from the license workbook.
licenses = pd.read_excel(licenses_source_file)
licenses.rename(columns=MAINE['licenses']['columns'], inplace=True)
licenses = licenses.assign(
licensing_authority_id=MAINE['licensing_authority_id'],
licensing_authority=MAINE['licensing_authority'],
license_designation='Adult-Use',
premise_state=STATE,
license_status_date=None,
license_term=None,
issue_date=None,
expiration_date=None,
business_structure=None,
business_email=None,
business_phone=None,
activity=None,
parcel_number=None,
premise_street_address=None,
id=licenses['license_number'],
business_image_url=None,
)
# Remove duplicates.
licenses.drop_duplicates(subset='license_number', inplace=True)
# Replace null DBA with legal name.
criterion = licenses['business_dba_name'].isnull()
licenses.loc[criterion,'business_dba_name'] = licenses['business_legal_name']
# Convert certain columns from upper case title case.
cols = ['business_legal_name', 'business_dba_name', 'business_owner_name']
for col in cols:
licenses[col] = licenses[col].apply(
lambda x: x.title().strip() if isinstance(x, str) else x
)
# Get the refreshed date.
date = licenses_source_file.split('\\')[-1].split('.')[0].replace(licenses_key, '')
date = date.replace('%20', '')
date = '-'.join([date[:2], date[2:4], date[4:]])
licenses['data_refreshed_date'] = pd.to_datetime(date).isoformat()
# Geocode licenses to get `premise_latitude` and `premise_longitude`.
config = dotenv_values(env_file)
api_key = config['GOOGLE_MAPS_API_KEY']
cols = ['premise_city', 'premise_state']
licenses['address'] = licenses[cols].apply(
lambda row: ', '.join(row.values.astype(str)),
axis=1,
)
licenses = geocode_addresses(licenses, address_field='address', api_key=api_key)
drop_cols = ['state', 'state_name', 'address', 'formatted_address',
'contact_type', 'contact_city', 'contact_description']
gis_cols = {
'county': 'premise_county',
'latitude': 'premise_latitude',
'longitude': 'premise_longitude',
}
licenses['premise_zip_code'] = licenses['formatted_address'].apply(
lambda x: x.split(', ')[2].split(',')[0].split(' ')[-1] if STATE in str(x) else x
)
licenses.drop(columns=drop_cols, inplace=True)
licenses.rename(columns=gis_cols, inplace=True)
# Save and return the data.
if data_dir is not None:
timestamp = datetime.now().isoformat()[:19].replace(':', '-')
licenses.to_csv(f'{data_dir}/licenses-{STATE.lower()}-{timestamp}.csv', index=False)
return licenses
# === Test ===
if __name__ == '__main__':
# Support command line usage.
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 = {'d': DATA_DIR, 'env_file': ENV_FILE}
# Get licenses, saving them to the specified directory.
data_dir = args.get('d', args.get('data_dir'))
env_file = args.get('env_file')
data = get_licenses_me(data_dir, env_file=env_file)
|