-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherapp.py
166 lines (146 loc) · 6.06 KB
/
weatherapp.py
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
import sys
import requests
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel,
QLineEdit, QPushButton, QVBoxLayout)
from PyQt5.QtCore import Qt
import json
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.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.temperature_label)
vbox.addWidget(self.emoji_label)
vbox.addWidget(self.description_label)
self.setLayout(vbox)
self.city_label.setAlignment(Qt.AlignCenter) # type: ignore
self.city_input.setAlignment(Qt.AlignCenter) # type: ignore
self.temperature_label.setAlignment(Qt.AlignCenter) # type: ignore
self.emoji_label.setAlignment(Qt.AlignCenter) # type: ignore
self.description_label.setAlignment(Qt.AlignCenter) # type: ignore
self.city_label.setObjectName("city_label")
self.city_input.setObjectName("city_input")
self.get_weather_button.setObjectName("get_weather_button")
self.temperature_label.setObjectName("temperature_label")
self.emoji_label.setObjectName("emoji_label")
self.description_label.setObjectName("description_label")
self.setStyleSheet("""
QLabel,QPushButton{
font-family: Calibri;
}
QLabel#city_label{
font-size: 40px;
font-style: italic;
}
QLineEdit#city_input{
font-size: 40px;
}
QPushButton#get_weather_button{
font-size: 30px;
font-weight: bold;
}
QLabel#temperature_label{
font-size: 75px;
}
QLabel#emoji_label{
font-size: 100px;
font-family: Segoe UI emoji;
}
QLabel#description_label{
font-size: 100px;
}
""")
self.get_weather_button.clicked.connect(self.get_weather)
def get_weather(self):
api_key = "3a105656f705c61d29666be21d69119c"
city = self.city_input.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:
self.display_weather(data)
except requests.exceptions.HTTPError as http_error:
match response.status_code:
case 400:
self.display_error("Bad request:\nPlease check your input")
case 401:
self.display_error("Unauthorized:\nInvalid API key")
case 403:
self.display_error("Forbidden:\nAcess in denied")
case 404:
self.display_error("Not found:\nCity not found")
case 500:
self.display_error("Internal Server Error:\nPlease try again later")
case 502:
self.display_error("Bad Gateway:\nInvalid response from the server")
case 503:
self.display_error("Service Unavailable:\nServer is down")
case 504:
self.display_error("Gateway Timeout:\nNo response from the server")
case _:
self.display_error("HTTP error occured:\n{http_error}")
except requests.exceptions.ConnectionError:
self.display_error("Connection Error:\nCheck your internet connection")
except requests.exceptions.Timeout:
self.display_error("Timeout Error:\nThe request your timed out")
except requests.exceptions.TooManyRedirects:
self.display_error("Too many redirects:\nCheck the URL")
except requests.exceptions.RequestException as req_error:
self.display_error(f"Request Error:\n {req_error}")
def display_error(self, message):
self.temperature_label.setStyleSheet("font-size: 30px;")
self.temperature_label.setText(message)
self.emoji_label.clear()
self.description_label.clear()
def display_weather(self, data):
self.temperature_label.setStyleSheet("font-size: 75px;")
temperature_k = data["main"]["temp"]
temperature_c = temperature_k - 273.15
temperature_f = (temperature_k * 9/5) - 459.67
weather_id = data["weather"][0]["id"]
weather_description = data["weather"][0]["description"]
self.temperature_label.setText(f"{round(temperature_f)}ºF")
self.emoji_label.setText(self.get_weather_emoji(weather_id))
self.description_label.setText(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_())