Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
160 lines (146 loc) · 5.94 KB
/
Copy pathmain.py
File metadata and controls
160 lines (146 loc) · 5.94 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
import sys
import requests
from PyQt5.QtWidgets import QApplication, QWidget, 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.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)
# Centering the text inside labels and inputs
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("""
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: 50px;
}
""")
self.get_weather_button.clicked.connect(self.get_weather)
def get_weather(self):
api_key = "c69b77cf2779353973237f4c18b88f1a"
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 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 server")
case 503:
self.display_error("Service Unavailable\nServer is down")
case 504:
self.display_error("Gateway Timeout\nNo response from server")
case _:
self.display_error(f"HTTP error occured\n{http_error}")
except requests.exceptions.ConnectionError:
self.display_error("Connection Error\nPlease check 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;")
temp_k = data["main"]["temp"]
temp_c = temp_k - 273.15
temp_f = (temp_k * 9 / 5) - 459.67
weather_id = data["weather"][0]["id"]
weather_description = data["weather"][0]["description"]
self.temperature_label.setText(f"Temperature: {temp_f:.0f}°F")
self.emoji_label.setText(self.get_weather_emoji(weather_id))
self.description_label.setText(weather_description )
#print(temp_c, temp_f,temp_k)
@staticmethod
def get_weather_emoji(weather_id):
if weather_id >= 200 and 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_())