From d3cae5fd851b6dd5b10095a50566f8ab84232f88 Mon Sep 17 00:00:00 2001 From: Aditya Mathur <61025539+AdityaMathur25@users.noreply.github.com> Date: Sat, 8 Oct 2022 16:21:41 +0530 Subject: [PATCH] Weather App Signed-off-by: Aditya Mathur <61025539+AdityaMathur25@users.noreply.github.com> --- Weather Tkinter/app.py | 77 +++++++++++++++++++++++++++++++++++++++ Weather Tkinter/readme.md | 2 + 2 files changed, 79 insertions(+) create mode 100644 Weather Tkinter/app.py create mode 100644 Weather Tkinter/readme.md diff --git a/Weather Tkinter/app.py b/Weather Tkinter/app.py new file mode 100644 index 0000000..d7c804e --- /dev/null +++ b/Weather Tkinter/app.py @@ -0,0 +1,77 @@ +import PySimpleGUI as sg +from bs4 import BeautifulSoup as bs +import requests + +def get_weather_data(location): + 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" + url = f'https://www.google.com/search?q=weather+{location.replace(" ","")}' + session = requests.Session() + session.headers['User-Agent'] = USER_AGENT + html = session.get(url) + + # create a new soup + 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 + +weather_column = sg.Column([ + [sg.Image('',key = '-IMAGE-', background_color = '#FFFFFF',)]], + key = '-LEFT-', + background_color = '#FFFFFF') + +info_column = sg.Column([ + [sg.Text('', key = '-LOCATION-', font = 'Calibri 30', background_color = '#FF0000', pad = 0, visible = False)], + [sg.Text('', key = '-TIME-', font = 'Calibri 16', background_color = '#000000', text_color = '#FFFFFF', pad = 0, visible = False)], + [sg.Text('', key = '-TEMP-', font = 'Calibri 16', pad = (0,10), background_color = '#FFFFFF', text_color = '#000000', justification = 'center', visible = False)] + ],key = '-RIGHT-', + background_color = '#FFFFFF') + +main_layout = [ + [sg.Input(key = '-INPUT-',expand_x = True),sg.Button('submit', button_color = '#000000')], + [weather_column,info_column] +] + +sg.theme('reddit') +window = sg.Window('Weather', main_layout) + +while True: + event, values = window.read() + if event == sg.WIN_CLOSED: + break + + if event == 'submit': + 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'): + # window['-IMAGE-'].update('symbols/sun.png') + + # # part sun + # if weather in ('Partly Sunny','Mostly Sunny','Partly cloudy','Mostly cloudy','Cloudy','Overcast'): + # window['-IMAGE-'].update('symbols/part sun.png') + + # # rain + # if weather in ('Rain','Chance of Rain','Light Rain','Showers','Scattered Showers','Rain and Snow','Hail'): + # window['-IMAGE-'].update('symbols/rain.png') + + # # thunder + # if weather in ('Scattered Thunderstorms','Chance of Storm','Storm','Thunderstorm','Chance of TStorm'): + # window['-IMAGE-'].update('symbols/thunder.png') + + # # foggy + # if weather in ('Mist','Dust','Fog','Smoke','Haze','Flurries'): + # window['-IMAGE-'].update('symbols/fog.png') + + # # snow + # if weather in ('Freezing Drizzle','Chance of Snow','Sleet','Snow','Icy','Snow Showers'): + # window['-IMAGE-'].update('symbols/snow.png') + + +window.close() \ No newline at end of file diff --git a/Weather Tkinter/readme.md b/Weather Tkinter/readme.md new file mode 100644 index 0000000..8c464fb --- /dev/null +++ b/Weather Tkinter/readme.md @@ -0,0 +1,2 @@ +This is a Simple Weather App You Just Need To Enter The Correct Place Name And it will display its weather . +![image](https://user-images.githubusercontent.com/61025539/194692495-201c509b-59c4-40ac-acbd-f4b2b0fd372f.png)