|
1 | 1 | import requests
|
2 | 2 | from datetime import datetime
|
3 |
| -from Util_Functions import wind_degree_to_direction |
| 3 | +from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime |
4 | 4 |
|
5 | 5 |
|
6 | 6 | # Function to fetch weather data from OpenWeatherMap API
|
@@ -33,9 +33,10 @@ def write_to_file(location, weather_data):
|
33 | 33 | date_time = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
|
34 | 34 |
|
35 | 35 | # Writing header information to the file
|
36 |
| - f.write("-------------------------------------------------------------\n") |
37 |
| - f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} | {date_time}\n") |
38 |
| - f.write("-------------------------------------------------------------\n") |
| 36 | + if "name" in weather_data and "sys" in weather_data and "country" in weather_data["sys"]: |
| 37 | + f.write("-------------------------------------------------------------\n") |
| 38 | + f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} | {date_time}\n") |
| 39 | + f.write("-------------------------------------------------------------\n") |
39 | 40 |
|
40 | 41 | # Writing temperature information to the file
|
41 | 42 | if "main" in weather_data and "temp" in weather_data["main"]:
|
@@ -75,6 +76,18 @@ def write_to_file(location, weather_data):
|
75 | 76 | "\tCurrent wind direction : " +
|
76 | 77 | wind_degree_to_direction(weather_data["wind"]["deg"]) + " \n")
|
77 | 78 |
|
| 79 | + # Writing sunrise local time to the file |
| 80 | + if "sys" in weather_data and "sunrise" in weather_data["sys"] and "timezone" in weather_data: |
| 81 | + f.write( |
| 82 | + "\tToday's sunrise time : " + |
| 83 | + unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]) + " \n") |
| 84 | + |
| 85 | + # Writing sunset local time to the file |
| 86 | + if "sys" in weather_data and "sunset" in weather_data["sys"] and "timezone" in weather_data: |
| 87 | + f.write( |
| 88 | + "\tToday's sunset time : " + |
| 89 | + unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]) + " \n") |
| 90 | + |
78 | 91 | # Printing confirmation message after writing to file
|
79 | 92 | print("Weather information written to weatherinfo.txt")
|
80 | 93 |
|
@@ -116,6 +129,10 @@ def main():
|
116 | 129 | print("Current Humidity :", weather_data["main"]["humidity"], "%")
|
117 | 130 | print("Current wind speed :", weather_data["wind"]["speed"], "kmph")
|
118 | 131 | print("Current wind direction:", wind_degree_to_direction(weather_data["wind"]["deg"]))
|
| 132 | + print("Today's sunrise time :", |
| 133 | + unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"])) |
| 134 | + print("Today's sunset time :", |
| 135 | + unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"])) |
119 | 136 | else:
|
120 | 137 | # Printing error message if weather data fetching fails
|
121 | 138 | print("Failed to fetch weather data. Please check your input and try again.")
|
|
0 commit comments