File size: 4,020 Bytes
124701c
 
 
 
 
 
 
 
c0464cb
124701c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0464cb
124701c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1352c88
 
 
 
c0464cb
124701c
 
 
 
 
 
 
 
 
c0464cb
 
 
124701c
 
 
 
c0464cb
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
"""
Cannabis Licenses | Get New Jersey 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: 8/17/2023
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE>

Description:

    Collect New Jersey cannabis license data.

Data Source:

    - New Jersey Cannabis Regulatory Commission
    URL: <https://data.nj.gov/stories/s/ggm4-mprw>

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

# External imports.
import pandas as pd
import requests


# Specify where your data lives.
DATA_DIR = '../data/nj'

# Specify state-specific constants.
STATE = 'NJ'
NEW_JERSEY = {
    'licensing_authority_id': 'NJCRC',
    'licensing_authority': 'New Jersey Cannabis Regulatory Commission',
    'retailers': {
        'columns': {
            'name': 'business_dba_name',
            'address': 'premise_street_address',
            'town': 'premise_city',
            'state': 'premise_state',
            'zip_code': 'premise_zip_code',
            'county': 'premise_county',
            'phone_number': 'business_phone',
            'type': 'license_type',
        }
    }
}


def get_licenses_nj(
        data_dir: Optional[str] = None,
        **kwargs,
    ):
    """Get New Jersey cannabis license data."""

    # Get retailer data.
    url = 'https://data.nj.gov/resource/nv37-s2zn.json'
    response = requests.get(url)
    data = pd.DataFrame(response.json())

    # Parse the website.
    data['business_website'] = data['website'].apply(lambda x: x['url'])

    # Parse the GIS coordinates.
    data['premise_longitude'] = data['dispensary_location'].apply(
        lambda x: x['coordinates'][0]
    )
    data['premise_latitude'] = data['dispensary_location'].apply(
        lambda x: x['coordinates'][1]
    )

    # Standardize the data.
    timestamp = datetime.now().isoformat()
    drop_cols = ['dispensary_location', 'location', 'website']
    data.drop(columns=drop_cols, inplace=True)
    data.rename(columns=NEW_JERSEY['retailers']['columns'], inplace=True)
    data['business_legal_name'] = data['business_dba_name']
    data['licensing_authority_id'] = NEW_JERSEY['licensing_authority_id']
    data['licensing_authority'] = NEW_JERSEY['licensing_authority']
    data['license_designation'] = 'Adult-Use'
    data['premise_state'] = STATE
    data['license_status_date'] = None
    data['license_term'] = None
    data['issue_date'] = None
    data['expiration_date'] = None
    data['business_owner_name'] = None
    data['business_structure'] = None
    data['business_email'] = None
    data['activity'] = None
    data['parcel_number'] = None
    data['business_image_url'] = None
    data['id'] = None
    data['license_number'] = None
    data['license_status'] = None
    data['data_refreshed_date'] = timestamp

    # Convert certain columns from upper case title case.
    cols = ['premise_city', 'premise_county', 'premise_street_address']
    for col in cols:
        data[col] = data[col].apply(lambda x: x.title().strip())

    # Save and return the data.
    if data_dir is not None:
        if not os.path.exists(data_dir): os.makedirs(data_dir)
        date = timestamp[:10]
        data.to_csv(f'{data_dir}/licenses-{STATE.lower()}-{date}.csv', index=False)
        data.to_csv(f'{data_dir}/licenses-{STATE.lower()}-latest.csv', index=False)
    return data


# === Test ===
# [✓] Tested: 2023-08-13 by Keegan Skeate <keegan@cannlytics>
if __name__ == '__main__':

    # Support command line usage.
    import argparse
    try:
        arg_parser = argparse.ArgumentParser()
        arg_parser.add_argument('--d', '--data_dir', dest='data_dir', type=str)
        args = arg_parser.parse_args()
    except SystemExit:
        args = {'d': DATA_DIR}

    # Get licenses, saving them to the specified directory.
    data_dir = args.get('d', args.get('data_dir'))
    data = get_licenses_nj(data_dir)