-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
148 lines (129 loc) · 3.98 KB
/
main.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
import PySimpleGUI as sg
from bs4 import BeautifulSoup as bs
import requests
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
def get_weather_data(location: str):
url = f"https://www.google.com/search?q=weather+{location.replace(' ', '%20')}"
session = requests.Session()
session.headers["User-Agent"] = USER_AGENT
html = session.get(url)
soup = bs(html.text, "html.parser")
name = soup.find("div", attrs={"id": "wob_loc"}).text
time = soup.find("div", attrs={"id": "wob_dts"}).text
weather = soup.find("span", attrs={"id": "wob_dc"}).text
temp = soup.find("span", attrs={"id": "wob_tm"}).text
return name, time, weather, temp
sg.theme("reddit")
image_col = sg.Column([[sg.Image("", key="-IMAGE-", background_color="white")]])
info_col = sg.Column(
[
[
sg.Text(
"",
key="-LOCATION-",
font="Calibri 30",
background_color="red",
text_color="white",
pad=0,
visible=False,
)
],
[
sg.Text(
"",
key="-TIME-",
font="Calibri 16",
background_color="black",
text_color="white",
pad=0,
visible=False,
)
],
[
sg.Text(
"",
key="-TEMP-",
font="Calibri 16",
justification="center",
expand_x=False,
pad=(0, 10),
visible=False,
background_color="white",
text_color="black",
),
],
]
)
layout = [
[
sg.Input(key="-INPUT-", expand_x=True),
sg.Button("Enter", key="-ENTER-", button_color="black", border_width=0),
],
[image_col, info_col],
]
window = sg.Window("Weather", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == "-ENTER-":
name, time, weather, temp = get_weather_data(values["-INPUT-"])
window["-LOCATION-"].update(name, visible=True)
window["-TIME-"].update(time.split(" ")[0], visible=True)
window["-TEMP-"].update(f"{temp} \u2103 ({weather})", visible=True)
# sun
if weather in (
"Sun",
"Sunny",
"Clear",
"Clear with periodic clouds",
"Mostly sunny",
):
symbol_img = "symbols/sun.png"
# part sun
elif weather in (
"Partly Sunny",
"Mostly Sunny",
"Partly cloudy",
"Mostly cloudy",
"Cloudy",
"Overcast",
):
symbol_img = "symbols/part sun.png"
# rain
elif weather in (
"Rain",
"Chance of Rain",
"Light Rain",
"Showers",
"Scattered Showers",
"Rain and Snow",
"Hail",
):
symbol_img = "symbols/rain.png"
# thunder
elif weather in (
"Scattered Thunderstorms",
"Chance of Storm",
"Storm",
"Thunderstorm",
"Chance of TStorm",
):
symbol_img = "symbols/thunder.png"
# fog
elif weather in ("Mist", "Dust", "Fog", "Smoke", "Haze", "Flurries"):
symbol_img = "symbols/fog.png"
# snow
elif weather in (
"Freezing Drizzle",
"Chance of Snow",
"Sleet",
"Snow",
"Icy",
"Snow Showers",
):
symbol_img = "symbols/snow.png"
else:
symbol_img = ""
window["-IMAGE-"].update(symbol_img, visible=True)
window.close()