11import requests
22from datetime import datetime
3- from Util_Functions import wind_degree_to_direction , unix_timestamp_to_localtime , convert_temperature
3+ from Util_Functions import (
4+ wind_degree_to_direction ,
5+ unix_timestamp_to_localtime ,
6+ convert_temperature ,
7+ )
48
59
610def fetch_weather (api_key , location ):
@@ -49,17 +53,29 @@ def write_to_file(weather_data, temperature_unit):
4953 date_time = datetime .now ().strftime ("%d %b %Y | %I:%M:%S %p" )
5054
5155 # Writing header information to the file
52- if "name" in weather_data and "sys" in weather_data and "country" in weather_data ["sys" ]:
53- f .write ("-------------------------------------------------------------\n " )
54- f .write (f"Weather Stats for - { weather_data ['name' ]} | { weather_data ['sys' ]['country' ]} "
55- f"| { date_time } \n " )
56- f .write ("-------------------------------------------------------------\n " )
56+ if (
57+ "name" in weather_data
58+ and "sys" in weather_data
59+ and "country" in weather_data ["sys" ]
60+ ):
61+ f .write (
62+ "-------------------------------------------------------------\n "
63+ )
64+ f .write (
65+ f"Weather Stats for - { weather_data ['name' ]} | { weather_data ['sys' ]['country' ]} "
66+ f"| { date_time } \n "
67+ )
68+ f .write (
69+ "-------------------------------------------------------------\n "
70+ )
5771
5872 # Writing temperature information to the file
5973 if "main" in weather_data and "temp" in weather_data ["main" ]:
6074 f .write (
6175 "\t Current temperature is : "
62- + convert_temperature (weather_data ["main" ]["temp" ], temperature_unit )
76+ + convert_temperature (
77+ weather_data ["main" ]["temp" ], temperature_unit
78+ )
6379 + "\n "
6480 )
6581
@@ -90,20 +106,38 @@ def write_to_file(weather_data, temperature_unit):
90106 # Writing wind direction information to the file
91107 if "wind" in weather_data and "deg" in weather_data ["wind" ]:
92108 f .write (
93- "\t Current wind direction : " +
94- wind_degree_to_direction (weather_data ["wind" ]["deg" ]) + " \n " )
109+ "\t Current wind direction : "
110+ + wind_degree_to_direction (weather_data ["wind" ]["deg" ])
111+ + " \n "
112+ )
95113
96114 # Writing sunrise local time to the file
97- if "sys" in weather_data and "sunrise" in weather_data ["sys" ] and "timezone" in weather_data :
115+ if (
116+ "sys" in weather_data
117+ and "sunrise" in weather_data ["sys" ]
118+ and "timezone" in weather_data
119+ ):
98120 f .write (
99- "\t Today's sunrise time : " +
100- unix_timestamp_to_localtime (weather_data ["sys" ]["sunrise" ], weather_data ["timezone" ]) + " \n " )
121+ "\t Today's sunrise time : "
122+ + unix_timestamp_to_localtime (
123+ weather_data ["sys" ]["sunrise" ], weather_data ["timezone" ]
124+ )
125+ + " \n "
126+ )
101127
102128 # Writing sunset local time to the file
103- if "sys" in weather_data and "sunset" in weather_data ["sys" ] and "timezone" in weather_data :
129+ if (
130+ "sys" in weather_data
131+ and "sunset" in weather_data ["sys" ]
132+ and "timezone" in weather_data
133+ ):
104134 f .write (
105- "\t Today's sunset time : " +
106- unix_timestamp_to_localtime (weather_data ["sys" ]["sunset" ], weather_data ["timezone" ]) + " \n " )
135+ "\t Today's sunset time : "
136+ + unix_timestamp_to_localtime (
137+ weather_data ["sys" ]["sunset" ], weather_data ["timezone" ]
138+ )
139+ + " \n "
140+ )
107141
108142 # Printing confirmation message after writing to file
109143 print ("Weather information written to weatherinfo.txt" )
@@ -127,7 +161,9 @@ def main():
127161 # Prompting the user to input API key, city name, and temperature unit
128162 api_key = input ("Please enter your OpenWeatherMap API key: " )
129163 location = input ("Enter the city name: " )
130- temperature_unit = input ("Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: " )
164+ temperature_unit = input (
165+ "Enter the temperature unit. 'C' for Celsius and 'F' for Fahrenheit: "
166+ )
131167
132168 if not (temperature_unit .upper () == "C" or temperature_unit .upper () == "F" ):
133169 print ("Temperature unit must either be 'C' or be 'F'." )
@@ -152,20 +188,35 @@ def main():
152188 write_to_file (weather_data , temperature_unit )
153189
154190 # Printing weather information to console
155- print ("Current City : " + weather_data ['name' ] + ', ' +
156- weather_data ['sys' ]['country' ])
191+ print (
192+ "Current City : "
193+ + weather_data ["name" ]
194+ + ", "
195+ + weather_data ["sys" ]["country" ]
196+ )
157197 print (
158198 "Current temperature is: "
159199 + convert_temperature (weather_data ["main" ]["temp" ], temperature_unit )
160200 )
161201 print ("Current weather desc : " + weather_data ["weather" ][0 ]["description" ])
162202 print ("Current Humidity :" , weather_data ["main" ]["humidity" ], "%" )
163203 print ("Current wind speed :" , weather_data ["wind" ]["speed" ], "kmph" )
164- print ("Current wind direction:" , wind_degree_to_direction (weather_data ["wind" ]["deg" ]))
165- print ("Today's sunrise time :" ,
166- unix_timestamp_to_localtime (weather_data ["sys" ]["sunrise" ], weather_data ["timezone" ]))
167- print ("Today's sunset time :" ,
168- unix_timestamp_to_localtime (weather_data ["sys" ]["sunset" ], weather_data ["timezone" ]))
204+ print (
205+ "Current wind direction:" ,
206+ wind_degree_to_direction (weather_data ["wind" ]["deg" ]),
207+ )
208+ print (
209+ "Today's sunrise time :" ,
210+ unix_timestamp_to_localtime (
211+ weather_data ["sys" ]["sunrise" ], weather_data ["timezone" ]
212+ ),
213+ )
214+ print (
215+ "Today's sunset time :" ,
216+ unix_timestamp_to_localtime (
217+ weather_data ["sys" ]["sunset" ], weather_data ["timezone" ]
218+ ),
219+ )
169220 else :
170221 # Printing error message if weather data fetching fails
171222 print ("Failed to fetch weather data. Please check your input and try again." )
0 commit comments