1
1
import requests
2
2
from 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
+ )
4
8
5
9
6
10
def fetch_weather (api_key , location ):
@@ -49,17 +53,29 @@ def write_to_file(weather_data, temperature_unit):
49
53
date_time = datetime .now ().strftime ("%d %b %Y | %I:%M:%S %p" )
50
54
51
55
# 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
+ )
57
71
58
72
# Writing temperature information to the file
59
73
if "main" in weather_data and "temp" in weather_data ["main" ]:
60
74
f .write (
61
75
"\t Current temperature is : "
62
- + convert_temperature (weather_data ["main" ]["temp" ], temperature_unit )
76
+ + convert_temperature (
77
+ weather_data ["main" ]["temp" ], temperature_unit
78
+ )
63
79
+ "\n "
64
80
)
65
81
@@ -90,20 +106,38 @@ def write_to_file(weather_data, temperature_unit):
90
106
# Writing wind direction information to the file
91
107
if "wind" in weather_data and "deg" in weather_data ["wind" ]:
92
108
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
+ )
95
113
96
114
# 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
+ ):
98
120
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
+ )
101
127
102
128
# 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
+ ):
104
134
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
+ )
107
141
108
142
# Printing confirmation message after writing to file
109
143
print ("Weather information written to weatherinfo.txt" )
@@ -127,7 +161,9 @@ def main():
127
161
# Prompting the user to input API key, city name, and temperature unit
128
162
api_key = input ("Please enter your OpenWeatherMap API key: " )
129
163
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
+ )
131
167
132
168
if not (temperature_unit .upper () == "C" or temperature_unit .upper () == "F" ):
133
169
print ("Temperature unit must either be 'C' or be 'F'." )
@@ -152,20 +188,35 @@ def main():
152
188
write_to_file (weather_data , temperature_unit )
153
189
154
190
# 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
+ )
157
197
print (
158
198
"Current temperature is: "
159
199
+ convert_temperature (weather_data ["main" ]["temp" ], temperature_unit )
160
200
)
161
201
print ("Current weather desc : " + weather_data ["weather" ][0 ]["description" ])
162
202
print ("Current Humidity :" , weather_data ["main" ]["humidity" ], "%" )
163
203
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
+ )
169
220
else :
170
221
# Printing error message if weather data fetching fails
171
222
print ("Failed to fetch weather data. Please check your input and try again." )
0 commit comments