11import requests
22from 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
44
55
66# Function to fetch weather data from OpenWeatherMap API
@@ -25,7 +25,7 @@ def fetch_weather(api_key, location):
2525
2626
2727# 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 ):
2929 try :
3030 # Opening the file "weatherinfo.txt" in write mode
3131 with open ("weatherinfo.txt" , "w+" ) as f :
@@ -35,15 +35,16 @@ def write_to_file(weather_data):
3535 # Writing header information to the file
3636 if "name" in weather_data and "sys" in weather_data and "country" in weather_data ["sys" ]:
3737 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 " )
3940 f .write ("-------------------------------------------------------------\n " )
4041
4142 # Writing temperature information to the file
4243 if "main" in weather_data and "temp" in weather_data ["main" ]:
4344 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 "
4748 )
4849
4950 # Writing weather description information to the file
@@ -105,9 +106,14 @@ def main():
105106 "You can obtain your API key by signing up at https://home.openweathermap.org/users/sign_up"
106107 )
107108
108- # Prompting the user to input API key and city name
109+ # Prompting the user to input API key, city name, and
109110 api_key = input ("Please enter your OpenWeatherMap API key: " )
110111 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
111117
112118 # Fetching weather data using the provided API key and location
113119 weather_data = fetch_weather (api_key , location )
@@ -123,15 +129,14 @@ def main():
123129 return
124130
125131 # Writing weather information to file
126- write_to_file (weather_data )
132+ write_to_file (weather_data , temperature_unit )
127133
128134 # Printing weather information to console
129135 print ("Current City : " + weather_data ['name' ] + ', ' +
130136 weather_data ['sys' ]['country' ])
131137 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 )
135140 )
136141 print ("Current weather desc : " + weather_data ["weather" ][0 ]["description" ])
137142 print ("Current Humidity :" , weather_data ["main" ]["humidity" ], "%" )
0 commit comments