ClearCode

Weather app

Apr 1st, 2022
3,786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. import PySimpleGUI as sg
  2. from bs4 import BeautifulSoup as bs
  3. import requests
  4.  
  5. def get_weather_data(location):
  6.     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"
  7.     url = f'https://www.google.com/search?q=weather+{location.replace(" ","")}'
  8.     session = requests.Session()
  9.     session.headers['User-Agent'] = USER_AGENT
  10.     html = session.get(url)
  11.    
  12.     # create a new soup
  13.     soup = bs(html.text, "html.parser")
  14.     name = soup.find("div", attrs={'id': 'wob_loc'}).text
  15.     time = soup.find("div", attrs={'id': 'wob_dts'}).text
  16.     weather = soup.find("span", attrs={'id': 'wob_dc'}).text
  17.     temp = soup.find("span", attrs={'id': 'wob_tm'}).text
  18.     return name, time, weather, temp
  19.  
  20. weather_column = sg.Column([
  21.     [sg.Image('',key = '-IMAGE-', background_color = '#FFFFFF',)]],
  22.     key = '-LEFT-',
  23.     background_color = '#FFFFFF')
  24.  
  25. info_column = sg.Column([
  26.     [sg.Text('', key = '-LOCATION-', font = 'Calibri 30', background_color = '#FF0000', pad = 0, visible = False)],
  27.     [sg.Text('', key = '-TIME-', font = 'Calibri 16', background_color = '#000000', text_color = '#FFFFFF', pad = 0, visible = False)],
  28.     [sg.Text('', key = '-TEMP-', font = 'Calibri 16', pad = (0,10), background_color = '#FFFFFF', text_color = '#000000', justification = 'center', visible = False)]
  29.     ],key = '-RIGHT-',
  30.     background_color = '#FFFFFF')
  31.  
  32. main_layout = [
  33.     [sg.Input(key = '-INPUT-',expand_x = True),sg.Button('submit', button_color = '#000000')],
  34.     [weather_column,info_column]
  35. ]
  36.  
  37. sg.theme('reddit')
  38. window = sg.Window('Weather', main_layout)
  39.  
  40. while True:
  41.     event, values = window.read()
  42.     if event == sg.WIN_CLOSED:
  43.         break
  44.  
  45.     if event == 'submit':
  46.         name, time, weather, temp = get_weather_data(values['-INPUT-'])
  47.  
  48.         window['-LOCATION-'].update(name, visible = True)
  49.         window['-TIME-'].update(time.split(' ')[0], visible = True)
  50.         window['-TEMP-'].update(f'{temp} \u2103 ({weather})', visible = True)
  51.  
  52.         # sun
  53.         if weather in ('Sun','Sunny','Clear','Clear with periodic clouds', 'Mostly sunny'):
  54.             window['-IMAGE-'].update('symbols/sun.png')
  55.        
  56.         # part sun
  57.         if weather in ('Partly Sunny','Mostly Sunny','Partly cloudy','Mostly cloudy','Cloudy','Overcast'):
  58.             window['-IMAGE-'].update('symbols/part sun.png')
  59.        
  60.         # rain
  61.         if weather in ('Rain','Chance of Rain','Light Rain','Showers','Scattered Showers','Rain and Snow','Hail'):
  62.             window['-IMAGE-'].update('symbols/rain.png')
  63.        
  64.         # thunder
  65.         if weather in ('Scattered Thunderstorms','Chance of Storm','Storm','Thunderstorm','Chance of TStorm'):
  66.             window['-IMAGE-'].update('symbols/thunder.png')
  67.  
  68.         # foggy
  69.         if weather in ('Mist','Dust','Fog','Smoke','Haze','Flurries'):
  70.             window['-IMAGE-'].update('symbols/fog.png')
  71.        
  72.         # snow
  73.         if weather in ('Freezing Drizzle','Chance of Snow','Sleet','Snow','Icy','Snow Showers'):
  74.             window['-IMAGE-'].update('symbols/snow.png')
  75.  
  76.  
  77. window.close()
Advertisement
Add Comment
Please, Sign In to add comment