|
""" |
|
Cannabis Licenses | Get Arizona Licenses |
|
Copyright (c) 2022 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 9/27/2022 |
|
Updated: 10/7/2022 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Collect Arizona cannabis license data. |
|
|
|
Data Source: |
|
|
|
- Arizona Department of Health Services | Division of Licensing |
|
URL: <https://azcarecheck.azdhs.gov/s/?licenseType=null> |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
from dotenv import dotenv_values |
|
import os |
|
from time import sleep |
|
from typing import Optional |
|
|
|
|
|
from cannlytics.data.gis import geocode_addresses |
|
import pandas as pd |
|
import zipcodes |
|
|
|
|
|
from selenium import webdriver |
|
from selenium.webdriver.chrome.options import Options |
|
from selenium.webdriver.common.by import By |
|
from selenium.webdriver.chrome.service import Service |
|
from selenium.webdriver.support import expected_conditions as EC |
|
from selenium.webdriver.support.ui import WebDriverWait |
|
try: |
|
import chromedriver_binary |
|
except ImportError: |
|
pass |
|
|
|
|
|
|
|
DATA_DIR = '../data/az' |
|
ENV_FILE = '../.env' |
|
|
|
|
|
STATE = 'AZ' |
|
ARIZONA = { |
|
'licensing_authority_id': 'ADHS', |
|
'licensing_authority': 'Arizona Department of Health Services', |
|
'licenses_url': 'https://azcarecheck.azdhs.gov/s/?licenseType=null', |
|
} |
|
|
|
|
|
def county_from_zip(x): |
|
"""Find a county given a zip code. Returns `None` if no match.""" |
|
try: |
|
return zipcodes.matching(x)[0]['county'] |
|
except KeyError: |
|
return None |
|
|
|
|
|
def get_licenses_az( |
|
data_dir: Optional[str] = None, |
|
env_file: Optional[str] = '.env', |
|
): |
|
"""Get Arizona cannabis license data.""" |
|
|
|
|
|
if not os.path.exists(data_dir): os.makedirs(data_dir) |
|
|
|
|
|
service = Service() |
|
options = Options() |
|
options.add_argument('--window-size=1920,1200') |
|
|
|
|
|
|
|
|
|
|
|
options.add_argument('--headless') |
|
options.add_argument('--disable-gpu') |
|
options.add_argument('--no-sandbox') |
|
|
|
|
|
driver = webdriver.Chrome(options=options, service=service) |
|
|
|
|
|
driver.get(ARIZONA['licenses_url']) |
|
detect = (By.CLASS_NAME, 'slds-container_center') |
|
WebDriverWait(driver, 30).until(EC.presence_of_element_located(detect)) |
|
|
|
|
|
container = driver.find_element(by=By.CLASS_NAME, value='slds-container_center') |
|
|
|
|
|
more = True |
|
while(more): |
|
button = container.find_element(by=By.TAG_NAME, value='button') |
|
driver.execute_script('arguments[0].scrollIntoView(true);', button) |
|
button.click() |
|
counter = container.find_element(by=By.CLASS_NAME, value='count-text') |
|
more = int(counter.text.replace(' more', '')) |
|
|
|
|
|
data = [] |
|
els = container.find_elements(by=By.CLASS_NAME, value='map-list__item') |
|
for i, el in enumerate(els): |
|
|
|
|
|
count = i + 1 |
|
xpath = f'/html/body/div[3]/div[2]/div/div[2]/div[2]/div/div/c-azcc-portal-home/c-azcc-map/div/div[2]/div[2]/div[2]/div[{count}]/c-azcc-map-list-item/div' |
|
list_item = el.find_element(by=By.XPATH, value=xpath) |
|
body = list_item.find_element(by=By.CLASS_NAME, value='slds-media__body') |
|
divs = body.find_elements(by=By.TAG_NAME, value='div') |
|
name = divs[0].text |
|
legal_name = divs[1].text |
|
if not name: |
|
name = legal_name |
|
address = divs[3].text |
|
address_parts = address.split(',') |
|
parts = divs[2].text.split(' · ') |
|
|
|
|
|
link = divs[-1].find_element(by=By.TAG_NAME, value='a') |
|
href = link.get_attribute('href') |
|
|
|
|
|
obs = { |
|
'address': address, |
|
'details_url': href, |
|
'business_legal_name': legal_name, |
|
'business_dba_name': name, |
|
'business_phone': parts[-1], |
|
'license_status': parts[0], |
|
'license_type': parts[1], |
|
'premise_street_address': address_parts[0].strip(), |
|
'premise_city': address_parts[1].strip(), |
|
'premise_zip_code': address_parts[-1].replace('AZ ', '').strip(), |
|
} |
|
data.append(obs) |
|
|
|
|
|
retailers = pd.DataFrame(data) |
|
retailers = retailers.assign( |
|
business_email=None, |
|
business_owner_name=None, |
|
business_structure=None, |
|
business_image_url=None, |
|
business_website=None, |
|
id=retailers.index, |
|
licensing_authority_id=ARIZONA['licensing_authority_id'], |
|
licensing_authority=ARIZONA['licensing_authority'], |
|
license_designation='Adult-Use', |
|
license_number=None, |
|
license_status_date=None, |
|
license_term=None, |
|
premise_latitude=None, |
|
premise_longitude=None, |
|
premise_state=STATE, |
|
issue_date=None, |
|
expiration_date=None, |
|
parcel_number=None, |
|
activity=None, |
|
) |
|
|
|
|
|
cultivators = pd.DataFrame(columns=retailers.columns) |
|
manufacturers = pd.DataFrame(columns=retailers.columns) |
|
for index, row in retailers.iterrows(): |
|
|
|
|
|
driver.get(row['details_url']) |
|
detect = (By.CLASS_NAME, 'slds-container_center') |
|
WebDriverWait(driver, 30).until(EC.presence_of_element_located(detect)) |
|
container = driver.find_element(by=By.CLASS_NAME, value='slds-container_center') |
|
sleep(4) |
|
|
|
|
|
links = container.find_elements(by=By.TAG_NAME, value='a') |
|
for link in links: |
|
href = link.get_attribute('href') |
|
if href is None: continue |
|
if href.startswith('mailto'): |
|
business_email = href.replace('mailto:', '') |
|
col = retailers.columns.get_loc('business_email') |
|
retailers.iat[index, col] = business_email |
|
break |
|
|
|
|
|
for link in links: |
|
href = link.get_attribute('href') |
|
if href is None: continue |
|
if href.startswith('https://azdhs-licensing'): |
|
col = retailers.columns.get_loc('license_number') |
|
retailers.iat[index, col] = link.text |
|
break |
|
|
|
|
|
for link in links: |
|
href = link.get_attribute('href') |
|
if href is None: continue |
|
if href.startswith('https://maps.google.com/'): |
|
coords = href.split('=')[1].split('&')[0].split(',') |
|
lat_col = retailers.columns.get_loc('premise_latitude') |
|
long_col = retailers.columns.get_loc('premise_longitude') |
|
retailers.iat[index, lat_col] = float(coords[0]) |
|
retailers.iat[index, long_col] = float(coords[1]) |
|
break |
|
|
|
|
|
key = 'License Effective' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-text") |
|
col = retailers.columns.get_loc('issue_date') |
|
retailers.iat[index, col] = el.text |
|
|
|
|
|
key = 'License Expires' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-text") |
|
col = retailers.columns.get_loc('expiration_date') |
|
retailers.iat[index, col] = el.text |
|
|
|
|
|
key = 'Owner / License' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-text") |
|
col = retailers.columns.get_loc('expiration_date') |
|
retailers.iat[index, col] = el.text |
|
|
|
|
|
key = 'Services' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-rich-text") |
|
col = retailers.columns.get_loc('license_designation') |
|
retailers.iat[index, col] = el.text |
|
|
|
|
|
cultivator = retailers.iloc[index].copy() |
|
key = 'Offsite Cultivation Address' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-text") |
|
address = el.text |
|
if address: |
|
parts = address.split(',') |
|
cultivator['address'] = address |
|
cultivator['premise_street_address'] = parts[0] |
|
cultivator['premise_city'] = parts[1].strip() |
|
cultivator['premise_zip_code'] = parts[-1].replace(STATE, '').strip() |
|
cultivator['license_type'] = 'Offsite Cultivation' |
|
cultivators.append(cultivator, ignore_index=True) |
|
|
|
|
|
manufacturer = retailers.iloc[index].copy() |
|
key = 'Manufacture Address' |
|
el = container.find_element_by_xpath(f"//p[contains(text(),'{key}')]/following-sibling::lightning-formatted-text") |
|
address = el.text |
|
if address: |
|
parts = address.split(',') |
|
manufacturer['address'] = address |
|
manufacturer['premise_street_address'] = parts[0] |
|
manufacturer['premise_city'] = parts[1].strip() |
|
manufacturer['premise_zip_code'] = parts[-1].replace(STATE, '').strip() |
|
manufacturer['license_type'] = 'Offsite Cultivation' |
|
manufacturers.append(manufacturer, ignore_index=True) |
|
|
|
|
|
service.stop() |
|
retailers.drop(column=['address', 'details_url'], inplace=True) |
|
|
|
|
|
retailers['premise_county'] = retailers['premise_zip_code'].apply(county_from_zip) |
|
cultivators['premise_county'] = cultivators['premise_zip_code'].apply(county_from_zip) |
|
manufacturers['premise_county'] = manufacturers['premise_zip_code'].apply(county_from_zip) |
|
|
|
|
|
config = dotenv_values(env_file) |
|
api_key = config['GOOGLE_MAPS_API_KEY'] |
|
drop_cols = ['state', 'state_name', 'county', 'address', 'formatted_address'] |
|
gis_cols = {'latitude': 'premise_latitude', 'longitude': 'premise_longitude'} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
licenses = pd.concat([retailers, cultivators, manufacturers]) |
|
|
|
|
|
timestamp = datetime.now().isoformat() |
|
licenses['data_refreshed_date'] = timestamp |
|
retailers['data_refreshed_date'] = timestamp |
|
|
|
|
|
|
|
|
|
if data_dir is not None: |
|
timestamp = timestamp[:19].replace(':', '-') |
|
licenses.to_csv(f'{data_dir}/licenses-{STATE.lower()}-{timestamp}.csv', index=False) |
|
retailers.to_csv(f'{data_dir}/retailers-{STATE.lower()}-{timestamp}.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 = {'d': DATA_DIR, 'env_file': ENV_FILE} |
|
|
|
|
|
data_dir = args.get('d', args.get('data_dir')) |
|
env_file = args.get('env_file') |
|
data = get_licenses_az(data_dir, env_file=env_file) |
|
|
|
|