-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherv2.0.py
72 lines (61 loc) · 2.87 KB
/
weatherv2.0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
class WeatherDietAdvisor:
def __init__(self, weather_api_key, weather_api_url, open_weather_map_city_id, nutrition_api_key, nutrition_api_base_url):
self.weather_api_key = weather_api_key
self.weather_api_url = weather_api_url
self.city_id = open_weather_map_city_id
self.nutrition_api_key = nutrition_api_key
self.nutrition_api_base_url = nutrition_api_base_url
def get_current_temperature(self):
url = f"{self.weather_api_url}?id={self.city_id}&appid={self.weather_api_key}"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if data.get('main'):
return data.get('main', {}).get('temp') - 273.15
else:
print("Error: 'main' key not found in weather data")
return None
else:
print(f"Error: API request failed with status code {response.status_code}")
return None
def suggest_diet(self):
temperature = self.get_current_temperature()
suggestion = ""
if temperature is not None:
temperature_category = self.get_temperature_category(temperature)
nutrition_suggestions = self.get_nutrition_suggestions(temperature_category)
suggestion = f"Based on the weather ({temperature:.1f}°C), here are some dietary suggestions:\n"
for item in nutrition_suggestions:
suggestion += f"- {item['name']}\n"
else:
suggestion = "Unable to determine weather conditions. Please try again later."
return suggestion
def get_temperature_category(self, temperature):
if temperature > 35:
return "hot"
elif temperature < 20:
return "cold"
else:
return "moderate"
def get_nutrition_suggestions(self, temperature_category):
nutrition_api_url = f"{self.nutrition_api_base_url}?q={temperature_category}&app_id=facecedd&app_key=<use your api key here>"
# print(nutrition_api_url)
response = requests.get(nutrition_api_url)
if response.status_code == 200:
data = response.json()
suggestions = []
for hit in data.get('hits', []):
suggestions.append({'name': hit['recipe']['label']})
return suggestions
else:
print(f"Error: Nutrition API request failed with status code {response.status_code}")
return []
weather_api_key = "<use your api key here>"
weather_api_url = "https://api.openweathermap.org/data/2.5/weather"
city_id = 2988507
nutrition_api_key = "<use your api key here>"
nutrition_api_base_url = "https://api.edamam.com/search"
advisor = WeatherDietAdvisor(weather_api_key, weather_api_url, city_id, nutrition_api_key, nutrition_api_base_url)
suggestion = advisor.suggest_diet()
print(suggestion)