-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI Weather App.py
More file actions
186 lines (157 loc) · 7.05 KB
/
Copy pathAPI Weather App.py
File metadata and controls
186 lines (157 loc) · 7.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#API Weather App
import sys
import requests
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QLineEdit, QPushButton, QVBoxLayout
from PyQt5.QtCore import Qt
class WeatherApp(QWidget):
def __init__(self):
super().__init__()
self.city_label = QLabel("Enter City Name:",self)
self.city_input = QLineEdit(self)
self.get_weather_button = QPushButton("Get Weather", self)
self.set_temperature_label = QLabel(self)
self.emoji_label = QLabel(self)
self.description_label = QLabel(self)
self.initUI()
def initUI(self):
self.setWindowTitle("Weather App")
vbox = QVBoxLayout()
vbox.addWidget(self.city_label)
vbox.addWidget(self.city_input)
vbox.addWidget(self.get_weather_button)
vbox.addWidget(self.set_temperature_label)
vbox.addWidget(self.emoji_label)
vbox.addWidget(self.description_label)
self.setLayout(vbox)
self.city_label.setAlignment(Qt.AlignCenter)
self.city_input.setAlignment(Qt.AlignCenter)
self.set_temperature_label.setAlignment(Qt.AlignCenter)
self.emoji_label.setAlignment(Qt.AlignCenter)
self.description_label.setAlignment(Qt.AlignCenter)
self.city_label.setObjectName("city_label")
self.city_input.setObjectName("city_input")
self.set_temperature_label.setObjectName("set_temperature_label")
self.emoji_label.setObjectName("emoji_label")
self.description_label.setObjectName("description_label")
self.get_weather_button.setObjectName("get_weather_button")
self.setStyleSheet("""
QPushButton, QLabel{
font-family: Arial;
}
QLabel#city_label{
font-size: 40px;
font-style: italic;
}
QLineEdit#city_input{
font-size: 40px;
padding: 10px 20px;
}
QPushButton#get_weather_button{
font-size: 30px;
font-weight: bold;
}
QLabel#set_temperature_label{
font-size: 75px;
}
QLabel#emoji_label{
font-size: 100px;
}
QLabel#description_label{
font-size: 40px;
}""")
self.get_weather_button.clicked.connect(self.get_weather)
def get_weather(self):
api_key = "f135fd5560660bbf17c1d3f4b35fa52f"
city = self.city_input.text()
limit = 1
if not city: # If city box is exmpty prompts user to enter a city
self.display_error("Please enter a city")
try:
geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={city}&limit={limit}&appid={api_key}"
geo_response = requests.get(geo_url)
geo_data = geo_response.json()
lat = geo_data[0]["lat"]
lon = geo_data[0]["lon"]
weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}"
weather_response = requests.get(weather_url)
weather_data = weather_response.json()
weather_response.raise_for_status()
if weather_data["cod"] == 200:
self.display_weather(weather_data)
else:
self.display_error(weather_data["message"])
except requests.exceptions.ConnectionError:
self.display_error("ConnectionError \n Check your internet connection")
except requests.exceptions.RequestException:
match weather_response.status_code:
case 400:
self.display_error("Bad request: \n please check input")
case 401:
self.display_error("Unauthorized: \n unvalid API key")
case 403:
self.display_error("Forbidden: \n Acess is denied")
case 404:
self.display_error("Not found: \n City not found")
case 500:
self.display_error("Internal Server Error: \n Please try again later")
case 502:
self.display_error("Bad Gateway: \n Invalid response from the Server")
case 503:
self.display_error("Service Unavailable: \n Server is down")
case 504:
self.display_error("Gateway Timeout: \n No Response From The Server")
case _:
self.display_error("Uknown error occured")
except requests.exceptions.HTTPError:
self.display_error("HTTP Error Occured")
except requests.exceptions.Timeout:
self.display_error("Timeout Occured: \n The request timed out")
except requests.exceptions.TooManyRedirects:
self.display_error("Too many redirects: \n check the url")
except IndexError:
self.display_error(f"Index Error: City: \n {city} is not found")
except Exception as e:
self.display_error(f"Unexpected Error: {e}")
def display_error(self,message):
self.set_temperature_label.setStyleSheet("font-size: 20px")
self.set_temperature_label.setText(message)
self.emoji_label.clear()
self.description_label.clear()
def display_weather(self,weather_data):
temp_k = weather_data["main"]["temp"]
temp_f = (temp_k-273.15)*9/5 + 32
weather_id = weather_data["weather"][0]["id"]
self.emoji_label.setText(self.get_weather_emoji(weather_id))
self.set_temperature_label.setStyleSheet("font-size: 75px")
self.set_temperature_label.setText(f"{temp_f:.0f}℉")
weather_description = weather_data["weather"][0]["description"]
self.description_label.setText(f"{weather_description}")
@staticmethod
def get_weather_emoji(weather_id):
if 200 <= weather_id <= 232:
return "⛈"
elif 300 <= weather_id <= 321:
return "☁︎"
elif 500 <= weather_id <= 531:
return "⛆"
elif 600 <= weather_id <= 622:
return "❅"
elif 701 <= weather_id <= 741:
return "🌫️"
elif weather_id == 762:
return "🌋"
elif weather_id == 771:
return "💨"
elif weather_id == 781:
return "🌪️"
elif weather_id == 800:
return "☼"
elif 801 <= weather_id <= 804:
return "⛅︎"
else:
return ""
if __name__ == "__main__":
app = QApplication(sys.argv)
weather_app = WeatherApp()
weather_app.show()
sys.exit(app.exec_())