-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGencoWeatherApp.py
More file actions
232 lines (210 loc) · 8.56 KB
/
GencoWeatherApp.py
File metadata and controls
232 lines (210 loc) · 8.56 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
######################
# Wheather API App #
######################
from sys import argv
import requests
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout, QPushButton
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
class WheatherApp(QWidget):
def __init__(self):
super().__init__()
self.cityinput = QLineEdit(self)
self.weatherbutton = QPushButton("Get Weather", self)
self.temperature = QLabel(self)
self.emoji = QLabel(self)
self.description = QLabel(self)
self.setWindowIcon(QIcon("weather.ico"))
self.initUI()
def initUI(self):
self.setGeometry(700, 300, 500, 600)
self.setWindowTitle("Genco Weather API App")
self.cityinput.setPlaceholderText("Enter a city")
vbox = QVBoxLayout()
vbox.addWidget(self.cityinput)
vbox.addWidget(self.weatherbutton)
vbox.addWidget(self.temperature)
vbox.addWidget(self.emoji)
vbox.addWidget(self.description)
self.setLayout(vbox)
self.cityinput.setAlignment(Qt.AlignCenter)
self.temperature.setAlignment(Qt.AlignCenter)
self.emoji.setAlignment(Qt.AlignCenter)
self.description.setAlignment(Qt.AlignCenter)
self.succesfonts()
self.weatherbutton.clicked.connect(self.getWeather)
def succesfonts(self):
self.weatherbutton.setObjectName("button")
self.emoji.setObjectName("emoji")
self.description.setStyleSheet("""
color: black;
font-size: 50px;
""")
self.temperature.setStyleSheet("""
color: black;
font-size: 80px;
""")
self.setStyleSheet("""
WheatherApp {
background-color: #e9eba0;
}
QLabel {
font-weight: bold;
font-family: calibri;
}
QLabel#emoji {
font-size: 110px;
font-family: Segoe UI emoji;
}
QLineEdit {
font-weight: bold;
font-size: 30px;
height: 50%;
font-family: calibri;
border: 10px solid;
border-radius: 20px;
margin-left: 30px;
margin-right: 30px;
}
QPushButton {
font-weight: bold;
font-size: 30px;
font-family: calibri;
border: 10px solid;
border-radius: 20px;
margin-left: 30px;
margin-right: 30px;
}
QPushButton#button {
background-color: #6e6c3c;
}
QPushButton#button:hover {
background-color: #91905a;
color: white;
}
""")
def getWeather(self):
api_key = "" # Enter your unique API key here
city = self.cityinput.text()
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
if data["cod"] == 200: # code: 200 -> response succesful
self.displayWeather(data)
except requests.exceptions.HTTPError:
match response.status_code:
case 400: # Bad Request
self.errorfonts()
self.emoji.setText("❔")
self.temperature.setText("Bad Request")
self.description.setText("Please check your input")
case 401: #Unauthorized
self.errorfonts()
self.emoji.setText("❌")
self.temperature.setText("Unauthorized")
self.description.setText("Invalid API key")
case 403: #Forbidden
self.errorfonts()
self.emoji.setText("🚫")
self.temperature.setText("Forbidden")
self.description.setText("Access denied")
case 404: #Not Found
self.errorfonts()
self.emoji.setText("❔")
self.temperature.setText("Not Found")
self.description.setText("Please check your input")
case 500: # Internal Server Error
self.errorfonts()
self.emoji.setText("🛑")
self.temperature.setText("Internal Server Error")
self.description.setText("Please try again later")
case 502: # Bad Gateway
self.errorfonts()
self.emoji.setText("⭕")
self.temperature.setText("Bad Getway")
self.description.setText("Invalid response from server")
case 503: # Service Unavailable
self.errorfonts()
self.emoji.setText("❗")
self.temperature.setText("Service Unavailable")
self.description.setText("Server is down")
case 504: # Gateway Timeout
self.errorfonts()
self.emoji.setText("⏰")
self.temperature.setText("Gateway Timeout")
self.description.setText("No response from server")
case _:
self.errorfonts()
self.emoji.setText("❓")
self.temperature.setText("Unknown HTTP Error")
self.description.setText(f"An HTTP error occured:\n{requests.exceptions.HTTPError}")
except requests.exceptions.ConnectionError:
self.errorfonts()
self.emoji.setText("🛜")
self.temperature.setText("Connection Error")
self.description.setText("Please check your internet connection")
except requests.exceptions.Timeout:
self.errorfonts()
self.emoji.setText("⏰")
self.temperature.setText("Timeout Error")
self.description.setText("The request timed out")
except requests.exceptions.TooManyRedirects:
self.errorfonts()
self.emoji.setText("🏋🏻♂️")
self.temperature.setText("Timeout Error")
self.description.setText("The request timed out")
except requests.exceptions.RequestException: # It can be any error of request process #4a1212
self.errorfonts()
self.emoji.setText("❗")
self.temperature.setText("Unknown error")
self.description.setText(f"An error occured:\n{requests.exceptions.RequestException}")
def errorfonts(self):
self.temperature.setStyleSheet("""
color: #4a1212;
font-size: 80px;
""")
self.description.setStyleSheet("""
color: #4a1212;
font-size: 30px;
""")
def displayWeather(self, data):
self.succesfonts()
temperature_kelvin = data["main"]["temp"] # {main: {'temp': >float<, 'feels_like': ...
temperature_celcius = temperature_kelvin - 273.15
#temperature_fahrenheit = (temperature_kelvin * 9/5) - 459.67
weatherdescription = data["weather"][0]["description"]
self.description.setText(weatherdescription)
self.temperature.setText(f"{temperature_celcius:.1f}°C")
wheaterid = data["weather"][0]["id"]
self.emoji.setText(self.chooseEmoji(wheaterid))
@staticmethod
def chooseEmoji(weather_id: int) -> str:
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(argv)
window = WheatherApp()
window.show()
app.exec_()