forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherapp.py
191 lines (165 loc) · 6.7 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
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
import sys # importing system
import requests # for making requests to an API
from PyQt5.QtWidgets import (QWidget, QLabel, QApplication, QLineEdit, QPushButton, QVBoxLayout) #importing widgets
from PyQt5.QtCore import Qt # for alignment
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("Just Another Weather App (JAWA)")
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)
self.city_input.setAlignment(Qt.AlignCenter)
self.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.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("""
QWidget {
background: #FAF0E6;
font-family: 'Georgia';
}
QLabel#city_label {
font-size: 30px;
font-weight: 300;
color: #000000;
margin-bottom: 15px;
}
QLineEdit#city_input {
font-size: 20px;
background-color: #A0522D;
border: none;
color: #F5FFFA;
}
QPushButton#get_weather_button {
font-size: 20px;
padding: 10px 20px;
background: #8B4513;
color: #000000;
border: none;
border-radius: 24px;
margin-top: 10px;
margin-bottom: 20px;
}
QLabel#temperature_label {
font-size: 80px;
color: #000000;
text-align: center;
margin-bottom: 10px;
}
QLabel#emoji_label {
font-size: 80px;
font-family: Apple Color Emoji;
text-align: center;
margin-bottom: 20px;
}
QLabel#description_label {
font-size: 80px;
font-weight: 300;
text-align: center;
color: #000000;
margin-bottom: 30px;
}
""")
self.get_weather_button.clicked.connect(self.get_weather)
def get_weather(self):
api_key = " "
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:\nAccess is 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(f"HTTP error occurred:\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 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
weather_id = data["weather"][0]["id"]
# Using title() to capitalize the first letter of each word in the description
weather_description = data["weather"][0]["description"].title()
self.temperature_label.setText(f"{temperature_c:.0f}°C")
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_())