From cf6e6d64064cb2f50f0de86631daf35acf9267bd Mon Sep 17 00:00:00 2001 From: anela Date: Fri, 21 Apr 2023 14:01:00 -0700 Subject: [PATCH] Removed save_image helper function --- lottery_data_scraper/util.py | 41 ------------------------------------ 1 file changed, 41 deletions(-) diff --git a/lottery_data_scraper/util.py b/lottery_data_scraper/util.py index cf7c4c1..1b05ab2 100644 --- a/lottery_data_scraper/util.py +++ b/lottery_data_scraper/util.py @@ -1,13 +1,9 @@ -import logging import base64 import os -import re import requests from tempfile import gettempdir -logger = logging.getLogger(__name__) - def fetch_html(url): """ Helper to fetch and cache html responses. @@ -49,40 +45,3 @@ def fetch_html(url): with open(filepath, "w+") as f: f.write(html) return html - - -def save_image(state, filename, url, headers=None): - """ - Takes an abbreviates for a state, filename(game_id), url of image location, and headers - - The function: - -parses the URL for the filetype - -establishes the image directory - -locates or create a filepath for images - -writes image info to file - """ - headers = headers or {} - extension = re.search(r"\.([^\.\?]*)($|[^\.]+$)", url).group(1) - IMAGE_DIR = os.getenv( - "IMAGE_DIR", - os.path.realpath(os.path.join(os.getenv("HOME"), ".data/assets/images")), - ) - IMAGE_DIR = f"{IMAGE_DIR}/{state}" - dirpath = IMAGE_DIR - if not os.path.exists(dirpath): - os.makedirs(dirpath) - filename = f"{filename}.{extension}" - filepath = os.path.realpath(os.path.join(dirpath, filename)) - try: - r = requests.get(url, stream=True, headers=headers) - except Exception as e: - logger.warn("Unable to download {}.\n{}".format(url, e)) - return None - if r.status_code == 200: - with open(filepath, "wb") as f: - for chunk in r: - f.write(chunk) - else: - logger.warn("Unable to download {}. {} - {}".format(url, r.status_code, r)) - return None - return "{}/{}".format(state, filename)