|
""" |
|
Get Florida cannabis lab results | Flowery |
|
Copyright (c) 2023-2024 Cannlytics |
|
|
|
Authors: |
|
Keegan Skeate <https://github.com/keeganskeate> |
|
Created: 5/18/2023 |
|
Updated: 4/28/2024 |
|
License: <https://github.com/cannlytics/cannlytics/blob/main/LICENSE> |
|
|
|
Description: |
|
|
|
Archive Florida cannabis lab result data for the Flowery. |
|
|
|
Data Sources: |
|
|
|
- [The Flowery](https://support.theflowery.co) |
|
|
|
""" |
|
|
|
from datetime import datetime |
|
import os |
|
from time import sleep |
|
|
|
|
|
from cannlytics.data.web import initialize_selenium |
|
from cannlytics.utils.constants import DEFAULT_HEADERS |
|
import pandas as pd |
|
import requests |
|
|
|
|
|
from selenium.webdriver.common.by import By |
|
from selenium.webdriver.support import expected_conditions as EC |
|
|
|
|
|
def get_results_the_flowery( |
|
data_dir: str, |
|
slug = 'the-flowery', |
|
producer_license_number = 'MMTC-2019-0020', |
|
lists_url = 'https://support.theflowery.co/hc/en-us/sections/7240468576283-Drop-Information', |
|
overwrite = False, |
|
): |
|
"""Get lab results published by The Flowery on the public web.""" |
|
|
|
|
|
driver = initialize_selenium() |
|
|
|
|
|
coa_lists = [] |
|
driver.get(lists_url) |
|
links = driver.find_elements(by=By.TAG_NAME, value='a') |
|
for link in links: |
|
if 'COAs' in link.text: |
|
coa_lists.append(link.get_attribute('href')) |
|
|
|
|
|
coa_urls = [] |
|
for coa_list in coa_lists: |
|
driver = initialize_selenium() |
|
driver.get(coa_list) |
|
links = driver.find_elements(by=By.TAG_NAME, value='a') |
|
for link in links: |
|
href = link.get_attribute('href') |
|
if href and href.endswith('.pdf'): |
|
coa_urls.append(href) |
|
driver.close() |
|
driver.quit() |
|
|
|
|
|
driver.close() |
|
driver.quit() |
|
|
|
|
|
datasets_dir = os.path.join(data_dir, '.datasets') |
|
if not os.path.exists(datasets_dir): |
|
os.makedirs(datasets_dir) |
|
|
|
|
|
date = datetime.now().isoformat()[:19].replace(':', '-') |
|
df = pd.DataFrame(coa_urls) |
|
df.to_excel(f'{datasets_dir}/fl-lab-result-urls-{slug}-{date}.xlsx', index=False) |
|
print('Saved %i lab result URLs for %s' % (len(df), slug)) |
|
|
|
|
|
pdf_dir = os.path.join(datasets_dir, 'pdfs') |
|
if not os.path.exists(pdf_dir): |
|
os.makedirs(pdf_dir) |
|
|
|
|
|
license_pdf_dir = os.path.join(pdf_dir, producer_license_number) |
|
if not os.path.exists(license_pdf_dir): |
|
os.makedirs(license_pdf_dir) |
|
|
|
|
|
for coa_url in coa_urls: |
|
sample_id = coa_url.split('/')[-1].split('.')[0] |
|
batch_id = coa_url.split('/')[-2] |
|
outfile = os.path.join(license_pdf_dir, f'{batch_id}-{sample_id}.pdf') |
|
if os.path.exists(outfile) and not overwrite: |
|
continue |
|
sleep(0.3) |
|
response = requests.get(coa_url, headers=DEFAULT_HEADERS) |
|
with open(outfile, 'wb') as pdf: |
|
pdf.write(response.content) |
|
print('Downloaded: %s' % outfile) |
|
|
|
|
|
|
|
|
|
return df |
|
|
|
|
|
def get_product_results_the_flowery( |
|
data_dir: str, |
|
overwrite = False, |
|
**kwargs, |
|
): |
|
"""Get product results from The Flowery website.""" |
|
|
|
driver = initialize_selenium() |
|
|
|
|
|
observations = [] |
|
categories = ['Flower', 'Concentrates', 'Pre-Rolls', 'Vaporizers', 'Tinctures'] |
|
for category in categories: |
|
|
|
|
|
url = f'https://theflowery.co/shop?categories[]={category}' |
|
driver.get(url) |
|
sleep(3.33) |
|
|
|
|
|
divs = driver.find_elements(by=By.CSS_SELECTOR, value='a.s-shop-product-card') |
|
for div in divs: |
|
|
|
|
|
product_name = div.find_element(by=By.CLASS_NAME, value='title').text |
|
|
|
|
|
product_image = div.find_element(by=By.TAG_NAME, value='img').get_attribute('src') |
|
|
|
|
|
product_price = float(div.find_element(by=By.CLASS_NAME, value='full-price').text.strip('$')) |
|
|
|
|
|
strain_type = div.find_element(by=By.CLASS_NAME, value='sort').text |
|
|
|
|
|
product_url = div.get_attribute('href') |
|
|
|
|
|
obs = { |
|
'product_name': product_name, |
|
'image_url': product_image, |
|
'product_price': product_price, |
|
'strain_type': strain_type, |
|
'product_url': product_url |
|
} |
|
observations.append(obs) |
|
|
|
|
|
|
|
for i, obs in enumerate(observations): |
|
coa_url = '' |
|
driver.get(obs['product_url']) |
|
sleep(3.33) |
|
links = driver.find_elements(by=By.TAG_NAME, value='a') |
|
for link in links: |
|
if link.get_attribute('href') and '.pdf' in link.get_attribute('href'): |
|
coa_url = link.get_attribute('href') |
|
break |
|
observations[i]['coa_url'] = coa_url |
|
print('Found COA URL: %s' % coa_url) |
|
|
|
|
|
driver.close() |
|
|
|
|
|
license_pdf_dir = os.path.join(data_dir, '.datasets', 'pdfs', 'MMTC-2019-0020') |
|
for obs in observations: |
|
coa_url = obs['coa_url'] |
|
|
|
|
|
sample_id = coa_url.split('/')[-1].split('.')[0] |
|
|
|
|
|
outfile = os.path.join(license_pdf_dir, f'{sample_id}.pdf') |
|
if os.path.exists(outfile) and not overwrite: |
|
print('Cached: %s' % outfile) |
|
continue |
|
sleep(0.3) |
|
|
|
|
|
|
|
|
|
response = requests.get(coa_url, headers=DEFAULT_HEADERS) |
|
with open(outfile, 'wb') as pdf: |
|
pdf.write(response.content) |
|
print('Downloaded: %s' % outfile) |
|
sleep(3.33) |
|
|
|
|
|
|
|
|
|
the_flowery = {'business_dba_name': 'The Flowery'} |
|
observations = [{**the_flowery, **x} for x in observations] |
|
|
|
|
|
date = datetime.now().isoformat()[:19].replace(':', '-') |
|
data = pd.DataFrame(observations) |
|
data.to_excel(f'{data_dir}/the-flowery-lab-result-urls-{date}.xlsx', index=False) |
|
print('Saved %i lab result URLs for The Flowery.' % len(data)) |
|
return data |
|
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
DATA_DIR = 'D://data/florida/results' |
|
|
|
|
|
try: |
|
the_flowery_products = get_product_results_the_flowery(DATA_DIR) |
|
except Exception as e: |
|
print('ERROR:', e) |
|
try: |
|
the_flowery_coas = get_results_the_flowery(DATA_DIR) |
|
except Exception as e: |
|
print('ERROR:', e) |
|
|