File size: 11,003 Bytes
124701c
 
 
 
 
 
 
 
1352c88
124701c
 
 
 
 
 
 
 
1352c88
 
124701c
1352c88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
Cannabis Licenses | Get New Mexico 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/6/2022
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>

Description:

    Collect New Mexico cannabis license data.

Data Source:

    - New Mexico Regulation and Licensing Department | Cannabis Control Division
    URL: <https://nmrldlpi.force.com/bcd/s/public-search-license?division=CCD&language=en_US>

"""
# Standard imports.
from datetime import datetime
import os
from time import sleep
from typing import Optional

# External imports.
from cannlytics.data.gis import geocode_addresses, search_for_address
from dotenv import dotenv_values
import pandas as pd

# Selenium imports.
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  # Adds chromedriver binary to path.
except ImportError:
    pass # Otherwise, ChromeDriver should be in your path.


# Specify where your data lives.
DATA_DIR = '../data/nm'
ENV_FILE = '../.env'

# Specify state-specific constants.
STATE = 'NM'
NEW_MEXICO = {
    'licensing_authority_id': 'NMCCD',
    'licensing_authority': 'New Mexico Cannabis Control Division',
    'licenses_url': 'https://nmrldlpi.force.com/bcd/s/public-search-license?division=CCD&language=en_US',
}


def get_licenses_nm(
        data_dir: Optional[str] = None,
        env_file: Optional[str] = '.env',
    ):
    """Get New Mexico cannabis license data."""

    # Create directories if necessary.
    if not os.path.exists(data_dir): os.makedirs(data_dir)

    # Initialize Selenium and specify options.
    service = Service()
    options = Options()
    options.add_argument('--window-size=1920,1200')

    # DEV: Run with the browser open.
    options.headless = False

    # PRODUCTION: Run with the browser closed.
    # options.add_argument('--headless')
    # options.add_argument('--disable-gpu')
    # options.add_argument('--no-sandbox')

    # Initiate a Selenium driver.
    driver = webdriver.Chrome(options=options, service=service)

    # Load the license page.
    driver.get(NEW_MEXICO['licenses_url'])

    # FIXME: Wait for the page to load by waiting to detect the image.
    # try:
    #     el = (By.CLASS_NAME, 'slds-radio--faux')
    #     WebDriverWait(driver, 15).until(EC.presence_of_element_located(el))
    # except TimeoutException:
    #     print('Failed to load page within %i seconds.' % (30))
    sleep(5)

    # Get the main content and click "License Type" raido.
    content = driver.find_element(by=By.CLASS_NAME, value='siteforceContentArea')
    radio = content.find_element(by=By.CLASS_NAME, value='slds-radio--faux')
    radio.click()
    sleep(2)

    # Select retailers.
    # TODO: Also get "Cannabis Manufacturer", "Cannabis Producer", and
    # "Cannabis Producer Microbusiness".
    search = content.find_element(by=By.ID, value='comboboxId-40')
    search.click()
    choices = content.find_elements(by=By.CLASS_NAME, value='slds-listbox__item')
    for choice in choices:
        if choice.text == 'Cannabis Retailer':
            choice.click()
            sleep(2)
            break

    # Click the search button.
    search = content.find_element(by=By.CLASS_NAME, value='vlocity-btn')
    search.click()
    sleep(2)

    # Iterate over all of the pages.
    # Wait for the table to load, then iterate over the pages.
    sleep(5)
    data = []
    iterate = True
    while(iterate):

        # Get all of the licenses.
        items = content.find_elements(by=By.CLASS_NAME, value='block-container')
        for item in items[3:]:
            text = item.text
            if not text:
                continue
            values = text.split('\n')
            data.append({
                'license_type': values[0],
                'license_status': values[1],
                'business_legal_name': values[2],
                'address': values[-1],
                'details_url': '',
            })

        # Get the page number and stop at the last page.
        # FIXME: This doesn't correctly break!
        par = content.find_elements(by=By.TAG_NAME, value='p')[-1].text
        page_number = int(par.split(' ')[2])
        total_pages = int(par.split(' ')[-2])
        if page_number == total_pages:
            iterate = False

        # Otherwise, click the next button.
        buttons = content.find_elements(by=By.TAG_NAME, value='button')
        for button in buttons:
            if button.text == 'Next Page':
                button.click()
                sleep(5)
                break

    # Search for each license name, 1 by 1, to get details.
    retailers = pd.DataFrame(columns=['business_legal_name'])
    for i, licensee in enumerate(data):

        # Skip recorded rows.
        if len(retailers.loc[retailers['business_legal_name'] == licensee['business_legal_name']]):
            continue

        # Click the "Business Name" search field.
        content = driver.find_element(by=By.CLASS_NAME, value='siteforceContentArea')
        radio = content.find_elements(by=By.CLASS_NAME, value='slds-radio--faux')[1]
        radio.click()
        sleep(1)

        # Enter the `business_legal_name` into the search.
        search_field = content.find_element(by=By.CLASS_NAME, value='vlocity-input')
        search_field.clear()
        search_field.send_keys(licensee['business_legal_name'])

        # Click the search button.
        search = content.find_element(by=By.CLASS_NAME, value='vlocity-btn')
        search.click()

        # FIXME: Wait for the table to load.
        # WebDriverWait(content, 5).until(EC.presence_of_element_located((By.CLASS_NAME, 'slds-button_icon')))
        sleep(1.5)

        # Click the "Action" button to get to the details page.
        # FIXME: There can be multiple search candidates!
        action = content.find_element(by=By.CLASS_NAME, value='slds-button_icon')
        try:
            action.click()
        except:
            continue # FIXME: Formally check if "No record found!".

        # FIXME: Wait for the details page to load.
        el = (By.CLASS_NAME, 'body')
        WebDriverWait(driver, 5).until(EC.presence_of_element_located(el))

        # Get the page
        page = driver.find_element(by=By.CLASS_NAME, value='body')

        # FIXME: Wait for the details to load!
        # el = (By.TAG_NAME, 'vlocity_ins-omniscript-step')
        # WebDriverWait(page, 5).until(EC.presence_of_element_located(el))
        sleep(1.5)

        # Get all of the details!
        fields = [
            'license_number',
            'license_status',
            'issue_date',
            'expiration_date',
            'business_owner_name',
        ]
        values = page.find_elements(by=By.CLASS_NAME, value='field-value')
        if len(values) > 5:
            for j, value in enumerate(values[:5]):
                data[i][fields[j]] = value.text
            for value in values[5:]:
                data[i]['business_owner_name'] += f', {value.text}'
        else:
            for j, value in enumerate(values):
                data[i][fields[j]] = value.text

        # Create multiple entries for each address!!!
        premises = page.find_elements(by=By.CLASS_NAME, value='block-header')
        for premise in premises:
            values = premise.text.split('\n')
            licensee['address'] = values[0].replace(',', ', ')
            licensee['license_number'] = values[2]
            retailers = pd.concat([retailers, pd.DataFrame([licensee])])

        # Click the "Back to Search" button.
        back_button = page.find_element(by=By.CLASS_NAME, value='vlocity-btn')
        back_button.click()
        sleep(1)

    # End the browser session.
    service.stop()

    # Standardize the data, restricting to "Approved" retailers.
    retailers = retailers.loc[retailers['license_status'] == 'Active']
    retailers = retailers.assign(
        business_email=None,
        business_structure=None,
        licensing_authority_id=NEW_MEXICO['licensing_authority_id'],
        licensing_authority=NEW_MEXICO['licensing_authority'],
        license_designation='Adult-Use',
        license_status_date=None,
        license_term=None,
        premise_state=STATE,
        parcel_number=None,
        activity=None,
        business_image_url=None,
        business_website=None,
        business_phone=None,
        id=retailers['license_number'],
        business_dba_name=retailers['business_legal_name'],
    )

    # Get the refreshed date.
    retailers['data_refreshed_date'] = datetime.now().isoformat()

    # Geocode licenses.
    # FIXME: This is not working as intended. Perhaps try `search_for_address`?
    config = dotenv_values(env_file)
    api_key = config['GOOGLE_MAPS_API_KEY']
    retailers = geocode_addresses(retailers, api_key=api_key, address_field='address')
    retailers['premise_street_address'] = retailers['formatted_address'].apply(
        lambda x: x.split(',')[0] if STATE in str(x) else x
    )
    retailers['premise_city'] = retailers['formatted_address'].apply(
        lambda x: x.split(', ')[1].split(',')[0] if STATE in str(x) else x
    )
    retailers['premise_zip_code'] = retailers['formatted_address'].apply(
        lambda x: x.split(', ')[2].split(',')[0].split(' ')[-1] if STATE in str(x) else x
    )
    drop_cols = ['state', 'state_name', 'address', 'formatted_address',
        'details_url']
    gis_cols = {
        'county': 'premise_county',
        'latitude': 'premise_latitude',
        'longitude': 'premise_longitude'
    }
    retailers.drop(columns=drop_cols, inplace=True)
    retailers.rename(columns=gis_cols, inplace=True)

    # Save and return the data.
    if data_dir is not None:
        if not os.path.exists(data_dir): os.makedirs(data_dir)
        timestamp = datetime.now().isoformat()[:19].replace(':', '-')
        retailers.to_csv(f'{data_dir}/retailers-{STATE.lower()}-{timestamp}.csv', index=False)
    return retailers


# === 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_nm(data_dir, env_file=env_file)