1
1
import requests
2
2
from datetime import datetime
3
- from Util_Functions import wind_degree_to_direction , unix_timestamp_to_localtime
3
+ from Util_Functions import wind_degree_to_direction , unix_timestamp_to_localtime , convert_temperature
4
4
5
5
6
6
# Function to fetch weather data from OpenWeatherMap API
@@ -25,7 +25,7 @@ def fetch_weather(api_key, location):
25
25
26
26
27
27
# Function to write weather information to a text file
28
- def write_to_file (weather_data ):
28
+ def write_to_file (weather_data , temperature_unit ):
29
29
try :
30
30
# Opening the file "weatherinfo.txt" in write mode
31
31
with open ("weatherinfo.txt" , "w+" ) as f :
@@ -35,15 +35,16 @@ def write_to_file(weather_data):
35
35
# Writing header information to the file
36
36
if "name" in weather_data and "sys" in weather_data and "country" in weather_data ["sys" ]:
37
37
f .write ("-------------------------------------------------------------\n " )
38
- f .write (f"Weather Stats for - { weather_data ['name' ]} | { weather_data ['sys' ]['country' ]} | { date_time } \n " )
38
+ f .write (f"Weather Stats for - { weather_data ['name' ]} | { weather_data ['sys' ]['country' ]} "
39
+ f"| { date_time } \n " )
39
40
f .write ("-------------------------------------------------------------\n " )
40
41
41
42
# Writing temperature information to the file
42
43
if "main" in weather_data and "temp" in weather_data ["main" ]:
43
44
f .write (
44
- "\t Current temperature is : {:.2f} °C \n " . format (
45
- weather_data ["main" ]["temp" ] - 273.15
46
- )
45
+ "\t Current temperature is : "
46
+ + convert_temperature ( weather_data ["main" ]["temp" ], temperature_unit )
47
+ + " \n "
47
48
)
48
49
49
50
# Writing weather description information to the file
@@ -105,9 +106,14 @@ def main():
105
106
"You can obtain your API key by signing up at https://home.openweathermap.org/users/sign_up"
106
107
)
107
108
108
- # Prompting the user to input API key and city name
109
+ # Prompting the user to input API key, city name, and
109
110
api_key = input ("Please enter your OpenWeatherMap API key: " )
110
111
location = input ("Enter the city name: " )
112
+ temperature_unit = input ("Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: " )
113
+
114
+ if not (temperature_unit .upper () == "C" or temperature_unit .upper () == "F" ):
115
+ print ("Temperature unit must either be 'C' or be 'F'." )
116
+ return
111
117
112
118
# Fetching weather data using the provided API key and location
113
119
weather_data = fetch_weather (api_key , location )
@@ -123,15 +129,14 @@ def main():
123
129
return
124
130
125
131
# Writing weather information to file
126
- write_to_file (weather_data )
132
+ write_to_file (weather_data , temperature_unit )
127
133
128
134
# Printing weather information to console
129
135
print ("Current City : " + weather_data ['name' ] + ', ' +
130
136
weather_data ['sys' ]['country' ])
131
137
print (
132
- "Current temperature is: {:.2f} °C" .format (
133
- weather_data ["main" ]["temp" ] - 273.15
134
- )
138
+ "Current temperature is: "
139
+ + convert_temperature (weather_data ["main" ]["temp" ], temperature_unit )
135
140
)
136
141
print ("Current weather desc : " + weather_data ["weather" ][0 ]["description" ])
137
142
print ("Current Humidity :" , weather_data ["main" ]["humidity" ], "%" )
0 commit comments