|
""" |
|
Cannabis Licenses | Get Oregon Licenses |
|
Copyright (c) 2022 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 9/28/2022 |
|
Updated: 10/7/2022 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Collect Oregon cannabis license data. |
|
|
|
Data Source: |
|
|
|
- Oregon Liquor and Cannabis Commission |
|
URL: <https://www.oregon.gov/olcc/marijuana/pages/recreational-marijuana-licensing.aspx> |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import os |
|
from typing import Optional |
|
|
|
|
|
from dotenv import dotenv_values |
|
import pandas as pd |
|
import requests |
|
from cannlytics.data.gis import geocode_addresses |
|
|
|
|
|
|
|
DATA_DIR = '../data/or' |
|
ENV_FILE = '../.env' |
|
|
|
|
|
OREGON = { |
|
'licensing_authority_id': 'OLCC', |
|
'licensing_authority': 'Oregon Liquor and Cannabis Commission', |
|
'licenses': { |
|
'url': 'https://www.oregon.gov/olcc/marijuana/Documents/MarijuanaLicenses_Approved.xlsx', |
|
}, |
|
'retailers': { |
|
'url': 'https://www.oregon.gov/olcc/marijuana/Documents/Approved_Retail_Licenses.xlsx', |
|
'columns': { |
|
'TRADE NAME': 'business_dba_name', |
|
'POSTAL CITY': 'premise_city', |
|
'COUNTY': 'premise_county', |
|
'STREET ADDRESS': 'premise_street_address', |
|
'ZIP': 'premise_zip_code', |
|
'Med Grade': 'medicinal', |
|
'Delivery': 'delivery', |
|
}, |
|
'drop_columns': [ |
|
'medicinal', |
|
'delivery', |
|
], |
|
}, |
|
} |
|
|
|
def get_licenses_or( |
|
data_dir: Optional[str] = None, |
|
env_file: Optional[str] = '.env', |
|
|
|
|
|
): |
|
"""Get California cannabis license data.""" |
|
|
|
|
|
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) |
|
|
|
|
|
timestamp = datetime.now().isoformat()[:19].replace(':', '-') |
|
outfile = f'{file_dir}/retailers-or-{timestamp}.xlsx' |
|
response = requests.get(OREGON['retailers']['url']) |
|
with open(outfile, 'wb') as doc: |
|
doc.write(response.content) |
|
|
|
|
|
data = pd.read_excel(outfile, skiprows=3) |
|
data = data[:-1] |
|
data.rename(columns=OREGON['retailers']['columns'], inplace=True) |
|
|
|
|
|
|
|
|
|
data['licensing_authority_id'] = OREGON['licensing_authority_id'] |
|
data['licensing_authority'] = OREGON['licensing_authority'] |
|
data['license_status'] = 'Active' |
|
data['license_designation'] = 'Adult-Use' |
|
data['premise_state'] = 'OR' |
|
data.loc[data['medicinal'] == 'Yes', 'license_designation'] = 'Adult-Use and Medicinal' |
|
data['business_image_url'] = None |
|
data['license_status_date'] = None |
|
data['license_term'] = None |
|
data['issue_date'] = None |
|
data['expiration_date'] = None |
|
data['business_email'] = None |
|
data['business_owner_name'] = None |
|
data['business_structure'] = None |
|
data['business_website'] = None |
|
data['activity'] = None |
|
data['business_phone'] = None |
|
data['parcel_number'] = None |
|
data['business_legal_name'] = data['business_dba_name'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
data.drop(columns=['medicinal', 'delivery'], inplace=True) |
|
|
|
|
|
cols = ['business_dba_name', 'premise_city', 'premise_county', |
|
'premise_street_address'] |
|
for col in cols: |
|
data[col] = data[col].apply(lambda x: x.title().strip()) |
|
|
|
|
|
data.loc[:, 'premise_zip_code'] = data['premise_zip_code'].apply(lambda x: str(int(x))) |
|
|
|
|
|
df = pd.read_excel(outfile, index_col=None, usecols='C', header=1, nrows=0) |
|
header = df.columns.values[0] |
|
date = pd.to_datetime(header.split(' ')[-1]) |
|
data['data_refreshed_date'] = date.isoformat() |
|
|
|
|
|
license_file = f'{file_dir}/licenses-or-{timestamp}.xlsx' |
|
response = requests.get(OREGON['licenses']['url']) |
|
with open(license_file, 'wb') as doc: |
|
doc.write(response.content) |
|
licenses = pd.read_excel(license_file, skiprows=2) |
|
licenses['BUSINESS NAME'] = licenses['BUSINESS NAME'].apply( |
|
lambda x: str(x).title().strip(), |
|
) |
|
licenses = licenses.loc[licenses['LICENSE TYPE'] == 'Recreational Retailer'] |
|
data = pd.merge( |
|
data, |
|
licenses[['BUSINESS NAME', 'COUNTY', 'LICENSE NUMBER', 'LICENSE TYPE']], |
|
left_on=['business_dba_name', 'premise_county'], |
|
right_on=['BUSINESS NAME', 'COUNTY'], |
|
how='left', |
|
) |
|
|
|
|
|
data.drop_duplicates(subset='premise_street_address', inplace=True) |
|
columns = { |
|
'LICENSE NUMBER': 'license_number', |
|
'LICENSE TYPE': 'license_type', |
|
} |
|
data.rename(columns=columns, inplace=True) |
|
data.drop(columns=['BUSINESS NAME', 'COUNTY'], inplace=True) |
|
data['id'] = data['license_number'] |
|
|
|
|
|
config = dotenv_values(env_file) |
|
google_maps_api_key = config['GOOGLE_MAPS_API_KEY'] |
|
cols = ['premise_street_address', 'premise_city', 'premise_state', |
|
'premise_zip_code'] |
|
data['address'] = data[cols].apply( |
|
lambda row: ', '.join(row.values.astype(str)), |
|
axis=1, |
|
) |
|
data = geocode_addresses( |
|
data, |
|
api_key=google_maps_api_key, |
|
address_field='address', |
|
) |
|
drop_cols = ['state', 'state_name', 'county', 'address', 'formatted_address'] |
|
data.drop(columns=drop_cols, inplace=True) |
|
gis_cols = { |
|
'latitude': 'premise_latitude', |
|
'longitude': 'premise_longitude' |
|
} |
|
data.rename(columns=gis_cols, inplace=True) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if data_dir is not None: |
|
timestamp = datetime.now().isoformat()[:19].replace(':', '-') |
|
data.to_csv(f'{data_dir}/licenses-or-{timestamp}.csv', index=False) |
|
return data |
|
|
|
|
|
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_DIR, 'env_file': ENV_FILE} |
|
|
|
|
|
data_dir = args.get('d', args.get('data_dir')) |
|
env_file = args.get('env_file') |
|
get_licenses_or(data_dir, env_file=env_file) |
|
|