|
""" |
|
Cannabis Licenses | Get Montana 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: 9/30/2022 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Collect Montana cannabis license data. |
|
|
|
Data Source: |
|
|
|
- Montana Department of Revenue | Cannabis Control Division |
|
URL: <https://mtrevenue.gov/cannabis/#CannabisLicenses> |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import os |
|
from time import sleep |
|
|
|
|
|
from bs4 import BeautifulSoup |
|
from cannlytics.utils import camel_to_snake |
|
from cannlytics.utils.constants import DEFAULT_HEADERS |
|
import pdfplumber |
|
import requests |
|
|
|
|
|
|
|
DATA_DIR = '../data/mt' |
|
ENV_FILE = '../.env' |
|
|
|
|
|
STATE = 'MT' |
|
MONTANA = { |
|
'retailers': { |
|
'url': 'https://mtrevenue.gov/?mdocs-file=60245', |
|
'columns': ['city', 'dba', 'license_type', 'phone'] |
|
}, |
|
'processors': {'url': 'https://mtrevenue.gov/?mdocs-file=60250'}, |
|
'cultivators': {'url': 'https://mtrevenue.gov/?mdocs-file=60252'}, |
|
'labs': {'url': 'https://mtrevenue.gov/?mdocs-file=60248'}, |
|
'transporters': {'url': 'https://mtrevenue.gov/?mdocs-file=72489'}, |
|
} |
|
|
|
|
|
data_dir = DATA_DIR |
|
pdf_dir = f'{data_dir}/pdfs' |
|
|
|
|
|
if not os.path.exists(data_dir): os.makedirs(data_dir) |
|
if not os.path.exists(pdf_dir): os.makedirs(pdf_dir) |
|
|
|
|
|
timestamp = datetime.now().isoformat()[:19].replace(':', '-') |
|
outfile = f'{pdf_dir}/mt-retailers-{timestamp}.pdf' |
|
response = requests.get(MONTANA['retailers']['url'], headers=DEFAULT_HEADERS) |
|
with open(outfile, 'wb') as pdf: |
|
pdf.write(response.content) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rows = [] |
|
skip_lines = ['GOVERNOR ', 'DIRECTOR ', 'Cannabis Control Division', |
|
'Licensed Dispensary locations', 'Please note', 'registered ', |
|
'City Location Name Sales Type Phone Number', 'Page '] |
|
doc = pdfplumber.open(outfile) |
|
for page in doc.pages: |
|
text = page.extract_text() |
|
lines = text.split('\n') |
|
for line in lines: |
|
skip = False |
|
for skip_line in skip_lines: |
|
if line.startswith(skip_line): |
|
skip = True |
|
break |
|
if skip: |
|
continue |
|
rows.append(line) |
|
|
|
|
|
licensees = [] |
|
for row in rows: |
|
|
|
|
|
if '(' not in row: |
|
continue |
|
|
|
obs = {} |
|
if 'Adult Use' in row: |
|
parts = row.split('Adult Use') |
|
obs['license_type'] = 'Adult Use' |
|
else: |
|
parts = row.split('Medical Only') |
|
obs['license_type'] = 'Medical Only' |
|
obs['dba'] = parts[0].strip() |
|
obs['phone'] = parts[-1].strip() |
|
licensees.append(obs) |
|
|
|
|
|
cities = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
url = 'https://dojmt.gov/wp-content/uploads/2011/05/mvmtcitiescountieszips.pdf' |
|
|
|
|
|
for i, licensee in enumerate(licensees): |
|
dba = licensee['dba'] |
|
city_found = False |
|
for city in cities: |
|
city_name = city.upper() |
|
if city_name in dba: |
|
licensees[i]['dba'] = dba.replace(city_name, '').strip() |
|
licensees[i]['city'] = city |
|
city_found = True |
|
break |
|
if not city_found: |
|
print("Couldn't identify city:", dba) |
|
|
|
|
|
|
|
|
|
|
|
|