Skip to content

Commit

Permalink
wrote software outline on main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan-Sell committed Oct 28, 2024
1 parent 27b71fc commit b5356ce
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
Binary file removed dump.rdb
Binary file not shown.
17 changes: 17 additions & 0 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
37 changes: 35 additions & 2 deletions src/weather_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
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)

0 comments on commit b5356ce

Please sign in to comment.