Jan-31-2019, 08:42 PM
Hello all, just wanted to post my script. This is the first one I've written and I made it to use the system default voice to speak the weather info to you. It also pulls up two satellite images from NOAA. Not real sure how optimized it is, but it works well. if you care to use you'll need to modify it for your local weather etc.
If you do use it let me know what you think.... happy coding@!
If you do use it let me know what you think.... happy coding@!
# ---- National Weather System by Python: # ----- weather crawler # ----- using default system voice from user_agent import generate_user_agent from bs4 import BeautifulSoup import requests, urllib.request, datetime from pathlib import Path, os import win32com.client as wincl headers = {'User-Agent': generate_user_agent(device_type ='desktop', os = ('mac', 'linux'))} page_response = requests.get('https://forecast.weather.gov/MapClick.php?lat=32.47318000000007&lon=-100.40478999999999#.XC2EbM1MFEa/', timeout = 5, headers = headers) soup = BeautifulSoup(page_response.content, 'html.parser') current = soup.find(id='current_conditions-summary') weather = [] period = current.find(class_='myforecast-current').text temp_f = current.find(class_='myforecast-current-lrg').text temp_c = current.find(class_='myforecast-current-sm').text weather.append(period) weather.append(temp_f) weather.append(temp_c) table = soup.find('table') for row in table.find_all('tr'): key = (' '.join(td.text.strip() for td in row.find_all('td'))) weather.append(key) seven_day = soup.find(id='seven-day-forecast') forecast_items = seven_day.find_all(class_='tombstone-container') current = forecast_items[0] forecast = current.find(class_='period-name').text short_desc = current.find(class_='short-desc').text temp = current.select('p.temp.temp')[0].text weather.append(forecast) weather.append(short_desc) weather.append(temp) with open('current-weather.dat', 'w') as f: for info in weather: f.write(f'{info}\n') s = wincl.Dispatch('sapi.SpVoice') # usinig Cortana as system default voice currentTime = datetime.datetime.now() # get current time for Cortana currentTime.hour if currentTime.hour < 12: s.Speak("Good morning Dawwg,... welcome to your desktop.") else: if 12 <= currentTime.hour < 18: s.Speak("Good afternoon Dawwg,... welcome to your desktop.") else: s.Speak("Good evening Dawwg,... welcome to your desktop.") s.Speak("Here are the local weather conditions... for the Sweetwater area.") with open('current-weather.dat', 'r') as f: text = f.read() s.Speak (text) with open('current-weather.dat', 'w') as f: f.write(f'"no current info"') s.Speak("Here is the national weather radar") # grabing two satellite images & using default image viewer urllib.request.urlretrieve("http://dsx.weather.com/util/image/map/us_radar_plus_usen_1280x720.jpg", "us_radar_plus_usen_1280x720.jpg") urllib.request.urlretrieve("https://cdn.star.nesdis.noaa.gov/GOES16/ABI/CONUS/GEOCOLOR/2500x1500.jpg", "ABI-CONUS-GEOCOLOR-2500x1500.jpg") os.system(r"%USERPROFILE%/Documents/weather/us_radar_plus_usen_1280x720.jpg") os.system(r"%USERPROFILE%/Documents/weather/ABI-CONUS-GEOCOLOR-2500x1500.jpg")