|
""" |
|
Cannabis Licenses | Get All Licenses |
|
Copyright (c) 2022-2023 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 9/29/2022 |
|
Updated: 9/20/2023 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Collect all cannabis license data from states with permitted adult-use: |
|
|
|
✓ Alaska (Requires Selenium) |
|
✓ Arizona (Requires Selenium) |
|
✓ California |
|
✓ Colorado |
|
✓ Connecticut |
|
✓ Delaware |
|
✓ Illinois (FIXME) |
|
✓ Maine |
|
✓ Maryland |
|
✓ Massachusetts |
|
✓ Michigan (Requires Selenium) |
|
✓ Minnesota |
|
✓ Missouri |
|
✓ Montana |
|
✓ Nevada |
|
✓ New Jersey |
|
✓ New York |
|
✓ New Mexico (Requires Selenium) (FIXME) |
|
✓ Oregon |
|
✓ Rhode Island |
|
✓ Vermont |
|
✓ Washington |
|
""" |
|
|
|
from datetime import datetime |
|
import importlib |
|
import os |
|
import sys |
|
|
|
|
|
from cannlytics import Cannlytics |
|
import pandas as pd |
|
|
|
|
|
|
|
ALGORITHMS = { |
|
'ak': 'get_licenses_ak', |
|
'az': 'get_licenses_az', |
|
'ca': 'get_licenses_ca', |
|
'co': 'get_licenses_co', |
|
'ct': 'get_licenses_ct', |
|
'de': 'get_licenses_de', |
|
'il': 'get_licenses_il', |
|
'ma': 'get_licenses_ma', |
|
'md': 'get_licenses_md', |
|
'me': 'get_licenses_me', |
|
'mi': 'get_licenses_mi', |
|
'mo': 'get_licenses_mo', |
|
'mt': 'get_licenses_mt', |
|
'nj': 'get_licenses_nj', |
|
'nm': 'get_licenses_nm', |
|
'nv': 'get_licenses_nv', |
|
'ny': 'get_licenses_ny', |
|
'or': 'get_licenses_or', |
|
'ri': 'get_licenses_ri', |
|
'vt': 'get_licenses_vt', |
|
'wa': 'get_licenses_wa', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
def main(data_dir, env_file): |
|
"""Collect all cannabis license data from states with permitted adult-use, |
|
dynamically importing modules and finding the entry point for each of the |
|
`ALGORITHMS`.""" |
|
|
|
|
|
licenses = pd.DataFrame() |
|
date = datetime.now().isoformat()[:10] |
|
manager = Cannlytics() |
|
|
|
|
|
sys.path.append(os.getcwd()) |
|
|
|
|
|
for state, algorithm in ALGORITHMS.items(): |
|
|
|
|
|
module = importlib.import_module(algorithm) |
|
entry_point = getattr(module, algorithm) |
|
|
|
|
|
|
|
manager.create_log(f'Getting license data for {state.upper()}.') |
|
data = entry_point(data_dir, env_file=env_file) |
|
|
|
state_data_dir = os.path.join(data_dir, state) |
|
if not os.path.exists(state_data_dir): |
|
os.makedirs(state_data_dir) |
|
|
|
state_data_file = os.path.join(state_data_dir, f'licenses-{state}-{date}.csv') |
|
data.to_csv(state_data_file, index=False) |
|
manager.create_log(f'Collected {state.upper()} licenses.') |
|
licenses = pd.concat([licenses, data]) |
|
|
|
|
|
|
|
|
|
all_data_dir = os.path.join(data_dir, 'all') |
|
if not os.path.exists(all_data_dir): |
|
os.makedirs(all_data_dir) |
|
|
|
|
|
|
|
all_data_file = os.path.join(all_data_dir, f'licenses-{date}.csv') |
|
licenses.to_csv(all_data_file, index=False) |
|
manager.create_log(f'Finished collecting licenses data.') |
|
return licenses |
|
|
|
|
|
def save_all_licenses(data_dir, version='latest'): |
|
"""Save all of the licenses to a single CSV file.""" |
|
|
|
|
|
all_licenses = [] |
|
states = ALGORITHMS.keys() |
|
for state in states: |
|
state_data_dir = os.path.join(data_dir, state) |
|
state_data_file = os.path.join(state_data_dir, f'licenses-{state}-{version}.csv') |
|
if os.path.exists(state_data_file): |
|
state_data = pd.read_csv(state_data_file) |
|
all_licenses.append(state_data) |
|
print('Aggregated data for', state.upper()) |
|
else: |
|
print(f'No data for {state.upper()}.') |
|
|
|
|
|
if all_licenses: |
|
licenses = pd.concat(all_licenses) |
|
all_data_file = os.path.join(data_dir, f'all/licenses-all-{version}.csv') |
|
licenses.to_csv(all_data_file, index=False) |
|
print('Saved all licenses to', all_data_file) |
|
return licenses |
|
|
|
|
|
else: |
|
print('No licenses to save.') |
|
return None |
|
|
|
|
|
|
|
|
|
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 = {'d': '../data', 'env_file': '../../../.env'} |
|
|
|
|
|
data_dir = args.get('d', args.get('data_dir')) |
|
env_file = args.get('env_file') |
|
|
|
|
|
|
|
|
|
|
|
data_dir = '../data' |
|
all_licenses = save_all_licenses(data_dir) |
|
print('Saved %i licenses.' % len(all_licenses)) |
|
|
|
|
|
|
|
|