|
""" |
|
Get California cannabis lab results |
|
Copyright (c) 2023 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Candace O'Sullivan-Sutherland <https://github.com/candy-o> |
|
Created: 5/25/2023 |
|
Updated: 5/30/2023 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Archive California cannabis lab result data. |
|
|
|
Data Sources: |
|
|
|
- [Glass House Farms Strains](https://glasshousefarms.org/strains/) |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import os |
|
from time import sleep |
|
|
|
|
|
from cannlytics.data.coas.coas import CoADoc |
|
from bs4 import BeautifulSoup |
|
from cannlytics.utils.constants import DEFAULT_HEADERS |
|
import pandas as pd |
|
import requests |
|
|
|
|
|
|
|
GLASS_HOUSE_FARMS = { |
|
'business_dba_name': 'Glass House Farms', |
|
'business_website': 'https://glasshousefarms.org', |
|
'business_image_url': 'https://glassfarms.wpenginepowered.com/wp-content/uploads/2021/10/new-ghf-menu.svg', |
|
'producer_license_number': 'CCL18-0000512', |
|
'producer_latitude': 34.404930, |
|
'producer_longitude': -119.518250, |
|
'producer_street_address': '5601 Casitas Pass Rd, Carpinteria, CA 93013', |
|
'producer_city': 'Carpinteria', |
|
'producer_county': 'Santa Barbara', |
|
'producer_state': 'CA', |
|
'lab_results_url': 'https://glasshousefarms.org/strains/', |
|
|
|
'license_status': 'Active', |
|
'license_status_date': '2023-04-07T00:00:00', |
|
'license_term': 'Annual', |
|
'license_type': 'Cultivation - Small Mixed-Light Tier 1', |
|
'license_designation': 'Medicinal', |
|
'issue_date': '2019-03-11T00:00:00', |
|
'expiration_date': '2024-03-11T00:00:00', |
|
'licensing_authority_id': 'CCL', |
|
'licensing_authority': 'CalCannabis Cultivation Licensing (CCL)', |
|
'business_legal_name': 'Mission Health Associates, Inc. dba Glass House Farms', |
|
'business_owner_name': 'Graham Farrar, Kyle Kazan', |
|
'business_structure': 'Corporation', |
|
'business_phone': '(805) 252-5755', |
|
'parcel_number': '001-060-042', |
|
} |
|
|
|
|
|
STRAIN_TYPES = { |
|
'sativa': {'sativa_percentage': 1.0, 'indica_percentage': 0.0}, |
|
'sativaDominant': {'sativa_percentage': 0.75, 'indica_percentage': 0.25}, |
|
'hybrid': {'sativa_percentage': 0.5, 'indica_percentage': 0.5}, |
|
'indica': {'sativa_percentage': 0.0, 'indica_percentage': 1.0}, |
|
'indicaDominant': {'sativa_percentage': 0.25, 'indica_percentage': 0.75}, |
|
'cbd': {'sativa_percentage': 0.0, 'indica_percentage': 0.0}, |
|
'cbdt': {'sativa_percentage': 0.0, 'indica_percentage': 0.0}, |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_glass_house_farms_lab_results( |
|
data_dir: str, |
|
pdf_dir: str, |
|
overwrite=False |
|
): |
|
"""Get lab results published by Glass House Farms. |
|
Data points: |
|
β image_url |
|
β strain_id |
|
β strain_name |
|
β strain_type |
|
β indica_percentage |
|
β sativa_percentage |
|
β strain_url |
|
β lineage |
|
β lab_result_id |
|
β coa_url |
|
""" |
|
|
|
|
|
license_number = GLASS_HOUSE_FARMS['producer_license_number'] |
|
license_pdf_dir = os.path.join(pdf_dir, license_number) |
|
if not os.path.exists(license_pdf_dir): |
|
os.makedirs(license_pdf_dir) |
|
|
|
|
|
url = 'https://glasshousefarms.org/strains/' |
|
response = requests.get(url, headers=DEFAULT_HEADERS) |
|
soup = BeautifulSoup(response.content, 'html.parser') |
|
|
|
|
|
observations = [] |
|
strains = soup.find_all(class_='item') |
|
for strain in strains: |
|
obs = {} |
|
|
|
|
|
img_tag = strain.find('img') |
|
obs['image_url'] = img_tag['src'] |
|
|
|
|
|
strain_type = strain.find('h5').text |
|
obs['strain_type'] = strain_type |
|
|
|
|
|
strain_name = strain.find('h4').text |
|
strain_name = strain_name.replace('\n', '').replace(strain_type, '').strip() |
|
obs['strain_name'] = strain_name |
|
|
|
|
|
strain_url = strain.find('a', class_='exp')['href'] |
|
obs['strain_url'] = strain_url |
|
|
|
|
|
obs['strain_id'] = strain_url.rstrip('/').split('/')[-1] |
|
|
|
|
|
wave = strain.find('div', class_='wave') |
|
wave_class = wave.get('class') |
|
wave_class = [cls for cls in wave_class if cls != 'wave'] |
|
if wave_class: |
|
for cls in wave_class: |
|
if cls in STRAIN_TYPES: |
|
obs['indica_percentage'] = STRAIN_TYPES[cls]['indica_percentage'] |
|
obs['sativa_percentage'] = STRAIN_TYPES[cls]['sativa_percentage'] |
|
break |
|
|
|
|
|
observations.append(obs) |
|
|
|
|
|
strain_data = pd.DataFrame(observations) |
|
|
|
|
|
date = datetime.now().strftime('%Y-%m-%d') |
|
outfile = os.path.join(data_dir, f'ca-strains-glass-house-{date}.xlsx') |
|
strain_data.to_excel(outfile, index=False) |
|
|
|
|
|
lab_results = [] |
|
for obs in observations: |
|
|
|
|
|
sleep(3.33) |
|
response = requests.get(obs['strain_url'] , headers=DEFAULT_HEADERS) |
|
soup = BeautifulSoup(response.content, 'html.parser') |
|
|
|
|
|
try: |
|
content = soup.find('div', class_='content') |
|
divs = content.find_all('div', class_='et_pb_column') |
|
except: |
|
print('No content found:', obs['strain_url']) |
|
continue |
|
try: |
|
lineage = divs[2].text.split('Lineage')[1].replace('\n', '').strip() |
|
obs['lineage'] = lineage.split(' x ') |
|
except: |
|
print('No lineage found:', obs['strain_url']) |
|
obs['lineage'] = [] |
|
|
|
|
|
pdf_links = [] |
|
for link in soup.find_all('a'): |
|
href = link.get('href') |
|
if href and href.endswith('.pdf'): |
|
pdf_links.append(href) |
|
|
|
|
|
for link in pdf_links: |
|
lab_result_id = link.split('/')[-1].split('.')[0] |
|
result = {'coa_url': link, 'lab_result_id': lab_result_id} |
|
lab_results.append({**GLASS_HOUSE_FARMS, **obs, **result}) |
|
|
|
|
|
for lab_result in lab_results: |
|
lab_result_id = lab_result['lab_result_id'] |
|
outfile = os.path.join(license_pdf_dir, f'{lab_result_id}.pdf') |
|
if os.path.exists(outfile) and not overwrite: |
|
continue |
|
sleep(1) |
|
response = requests.get(lab_result['coa_url'], headers=DEFAULT_HEADERS) |
|
with open(outfile, 'wb') as pdf: |
|
pdf.write(response.content) |
|
print('Downloaded: %s' % outfile) |
|
|
|
|
|
results = pd.DataFrame(lab_results) |
|
date = datetime.now().strftime('%Y-%m-%d') |
|
outfile = os.path.join(data_dir, f'ca-result-urls-glass-house-{date}.xlsx') |
|
results.to_excel(outfile, index=False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
data_dir = 'D://data/california/results/datasets' |
|
pdf_dir = 'D://data/california/results/pdfs' |
|
|
|
|
|
all_results = get_glass_house_farms_lab_results(data_dir, pdf_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|