Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -51,11 +51,23 @@ def get_pollen_data(lat, lon, days=5):
|
|
51 |
response = requests.get(url, params=params)
|
52 |
response.raise_for_status()
|
53 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
return data
|
55 |
except requests.exceptions.RequestException as e:
|
56 |
-
|
|
|
|
|
|
|
57 |
except json.JSONDecodeError as e:
|
58 |
return f"Error parsing response: {str(e)}"
|
|
|
|
|
59 |
|
60 |
def format_pollen_data(data):
|
61 |
"""Format pollen data for display"""
|
@@ -77,8 +89,29 @@ def format_pollen_data(data):
|
|
77 |
# Daily pollen data
|
78 |
daily_data = []
|
79 |
for day_info in data["dailyInfo"]:
|
80 |
-
date
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
formatted_date = date_obj.strftime("%B %d, %Y")
|
83 |
|
84 |
output.append(f"## 📅 {formatted_date}\n")
|
|
|
51 |
response = requests.get(url, params=params)
|
52 |
response.raise_for_status()
|
53 |
data = response.json()
|
54 |
+
|
55 |
+
# Debug: Print the structure to understand the API response
|
56 |
+
print(f"API Response keys: {data.keys()}")
|
57 |
+
if "dailyInfo" in data and len(data["dailyInfo"]) > 0:
|
58 |
+
print(f"First day info keys: {data['dailyInfo'][0].keys()}")
|
59 |
+
print(f"Date structure: {data['dailyInfo'][0].get('date', 'No date field')}")
|
60 |
+
|
61 |
return data
|
62 |
except requests.exceptions.RequestException as e:
|
63 |
+
error_msg = f"Error fetching pollen data: {str(e)}"
|
64 |
+
if hasattr(e, 'response') and e.response is not None:
|
65 |
+
error_msg += f" (Status: {e.response.status_code})"
|
66 |
+
return error_msg
|
67 |
except json.JSONDecodeError as e:
|
68 |
return f"Error parsing response: {str(e)}"
|
69 |
+
except Exception as e:
|
70 |
+
return f"Unexpected error: {str(e)}"
|
71 |
|
72 |
def format_pollen_data(data):
|
73 |
"""Format pollen data for display"""
|
|
|
89 |
# Daily pollen data
|
90 |
daily_data = []
|
91 |
for day_info in data["dailyInfo"]:
|
92 |
+
# Handle different date formats from the API
|
93 |
+
date_info = day_info.get("date", {})
|
94 |
+
|
95 |
+
if isinstance(date_info, dict):
|
96 |
+
# If date is a dict, try to extract the date string
|
97 |
+
if "year" in date_info and "month" in date_info and "day" in date_info:
|
98 |
+
year = date_info["year"]
|
99 |
+
month = date_info["month"]
|
100 |
+
day = date_info["day"]
|
101 |
+
date_obj = datetime(year, month, day)
|
102 |
+
else:
|
103 |
+
# Fallback to today's date if we can't parse
|
104 |
+
date_obj = datetime.now()
|
105 |
+
elif isinstance(date_info, str):
|
106 |
+
# If it's already a string, parse it
|
107 |
+
try:
|
108 |
+
date_obj = datetime.strptime(date_info, "%Y-%m-%d")
|
109 |
+
except ValueError:
|
110 |
+
date_obj = datetime.now()
|
111 |
+
else:
|
112 |
+
# Fallback
|
113 |
+
date_obj = datetime.now()
|
114 |
+
|
115 |
formatted_date = date_obj.strftime("%B %d, %Y")
|
116 |
|
117 |
output.append(f"## 📅 {formatted_date}\n")
|