Spoon-assassin commited on
Commit
0495093
·
verified ·
1 Parent(s): 22367cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -35
app.py CHANGED
@@ -20,41 +20,41 @@ def date_to_friendly_format(date_string):
20
  friendly_date = date_obj.strftime("%a %b %d %Y")
21
  return friendly_date
22
 
23
- def meteo_weathercode_to_emoji(weathercode):
24
  """
25
- Converts a weather code to a corresponding emoji.
26
  """
27
- emoji = {
28
- 0: "☀️",
29
- 1: "🌤️",
30
- 2: "⛅️",
31
- 3: "☁️",
32
- 45: "🌫️",
33
- 48: "🌫️",
34
- 51: "🌦️",
35
- 53: "🌦️",
36
- 55: "🌧️",
37
- 56: "🌧️",
38
- 57: "🌧️",
39
- 61: "🌧️",
40
- 63: "🌧️",
41
- 65: "🌧️",
42
- 66: "🌧️",
43
- 67: "🌧️",
44
- 71: "🌩️",
45
- 73: "🌩️",
46
- 75: "🌩️",
47
- 77: "⛈️",
48
- 80: "🌦️",
49
- 81: "🌦️",
50
- 82: "🌦️",
51
- 85: "🌧️",
52
- 86: "🌧️",
53
- 95: "🌫️",
54
- 96: "🌫️",
55
- 99: "🌫️"
56
  }
57
- return emoji.get(weathercode, "")
58
 
59
  def get_city_coordinates(city_name: str):
60
  """
@@ -100,11 +100,11 @@ def get_weather_forecast(city: str, days: str=3) -> str:
100
  response.raise_for_status()
101
  forecast_data = response.json()
102
  if "daily" in forecast_data:
103
- output = []
104
  daily_date = forecast_data["daily"]
105
  for weather in zip(daily_date["time"], daily_date["weathercode"], daily_date["temperature_2m_max"], daily_date["temperature_2m_min"], daily_date["precipitation_sum"]):
106
- output.append(f'{date_to_friendly_format(weather[0])}: ({meteo_weathercode_to_emoji(weather[1])}) High: {weather[2]}°C, Low: {weather[3]}°C, Rain: {weather[4]}mm')
107
- return "\n".join(output)
108
  else:
109
  print("Error: No 'daily' data found in the API response.")
110
  return None
 
20
  friendly_date = date_obj.strftime("%a %b %d %Y")
21
  return friendly_date
22
 
23
+ def meteo_weathercode_to_string(weathercode):
24
  """
25
+ Converts a weather code to a corresponding string.
26
  """
27
+ string_lookup = {
28
+ 0: "Clear sky",
29
+ 1: "Mainly clear",
30
+ 2: "Partly cloudy",
31
+ 3: "Overcast",
32
+ 45: "Fog",
33
+ 48: "Depositing rime fog",
34
+ 51: "Light drizzle",
35
+ 53: "Moderate drizzle",
36
+ 55: "Dense drizzle",
37
+ 56: "Light freezing drizzle",
38
+ 57: "Dense freezing drizzle",
39
+ 61: "Slight rain",
40
+ 63: "Moderate rain",
41
+ 65: "Heavy rain",
42
+ 66: "Light freezing rain",
43
+ 67: "Heavy freezing rain",
44
+ 71: "Slight snow fall",
45
+ 73: "Moderate snow fall",
46
+ 75: "Heavy snow fall",
47
+ 77: "Snow grains",
48
+ 80: "Slight rain showers",
49
+ 81: "Moderate rain showers",
50
+ 82: "Heavy rain showers",
51
+ 85: "Slight snow showers",
52
+ 86: "Heavy snow showers",
53
+ 95: "Thunderstorm",
54
+ 96: "Thunderstorm with slight hail",
55
+ 99: "Thunderstorm with heavy hail"
56
  }
57
+ return string_lookup.get(weathercode, "Unknown")
58
 
59
  def get_city_coordinates(city_name: str):
60
  """
 
100
  response.raise_for_status()
101
  forecast_data = response.json()
102
  if "daily" in forecast_data:
103
+ csv = ["Date,Weather,High,Low,Rain"]
104
  daily_date = forecast_data["daily"]
105
  for weather in zip(daily_date["time"], daily_date["weathercode"], daily_date["temperature_2m_max"], daily_date["temperature_2m_min"], daily_date["precipitation_sum"]):
106
+ csv.append(f"{date_to_friendly_format(weather[0])},{meteo_weathercode_to_string(weather[1])},{weather[2]}°C,{weather[3]}°C,{weather[4]}mm")
107
+ return "\n".join(csv)
108
  else:
109
  print("Error: No 'daily' data found in the API response.")
110
  return None