diff --git a/dump.rdb b/dump.rdb deleted file mode 100644 index d0ac255..0000000 Binary files a/dump.rdb and /dev/null differ diff --git a/src/main.py b/src/main.py index 6868d73..b27b368 100644 --- a/src/main.py +++ b/src/main.py @@ -17,6 +17,23 @@ def main(): + """ + STEPS: + 1. User inputs location + 2. Check if location is in Redis + 3. + + a. If location is in Redis: + i. Retrieve data from Redis. + + b. If location is NOT in Redis: + i. Fetch data from API. + ii. Extract the necessary data from JSON into a WeatherData + iii. Save WeatherData to Redis. The city will be lower case. + d. + + """ + pprint(get_weather("forecast", "Santa Monica", "us", api_key)) # pprint(fetch_weather_api("forecast", "Baltimore", "us", api_key)) diff --git a/src/weather_cache.py b/src/weather_cache.py index 77c18c3..45dfda4 100644 --- a/src/weather_cache.py +++ b/src/weather_cache.py @@ -7,12 +7,20 @@ # Connect to Redis try: - r = redis.Redis(host="localhost", port=6379, db=0) + redis_client = redis.Redis(host="localhost", port=6379, db=0) print("Connected to Redis successfully.") except Exception as e: print(f"Error connecting to Redis: {e}") + +def check_if_cache_key_exists(cache_key: str) -> bool: + try: + return redis_client.exists(cache_key) == 1 + except Exception as e: + print(f"Error checking if cach key exists in Redis: {e}") + return False + def get_weather(endpoint: str, location: str, unit: str, api_key: str) -> dict: try: # Check if weather data is already in Redis @@ -40,4 +48,29 @@ def get_weather(endpoint: str, location: str, unit: str, api_key: str) -> dict: except Exception as e: print(f"An error occurred in get_weather: {e}") - return None \ No newline at end of file + return None + + +@dataclass +class WeatherData: + city: str + address: str + datetime_str: str + conditions: str + temperature: float + humidity: float + uv_index: float + heat_index: float + + def to_json(self) -> str: + """Convert data class to JSON string for Redis storage.""" + return json.dumps(self.__dict__) + + + @staticmethod + def from_json(data: str): + return WeatherData(**json.loads(data)) + + +def extract_relevant_data(data: dict, data_loc: list, vars: list) + \ No newline at end of file