Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyowm as weather
- import time as timer
- from datetime import *
- import smtplib as smtp
- from email.mime.text import MIMEText as email
- # Weather Forecast Bot
- # Author: Hugo Nascimento
- KEY = weather.OWM('dacf348d593f6fed98857e7b168ebc3b')
- forecast = KEY.daily_forecast("London, uk")
- tomorrow = weather.timeutils.tomorrow()
- # Forecast
- tommorrow_forecast = "Unknown"
- if forecast.will_be_sunny_at(tomorrow) == True:
- tommorrow_forecast = "Sunny"
- if forecast.will_be_rainy_at(tomorrow) == True:
- tommorrow_forecast = "Rainy"
- if forecast.will_be_cloudy_at(tomorrow) == True:
- tommorrow_forecast = "Cloudy"
- if forecast.will_be_snowy_at(tomorrow) == True:
- tommorrow_forecast = "Snowy"
- today_forecast = KEY.weather_at_place('London,uk')
- get_forecast = today_forecast.get_weather()
- # Weather details
- weather_wind = get_forecast.get_wind()
- weather_wind_speed = get_forecast.get_wind()['speed']
- weather_humidity = get_forecast.get_humidity()
- weather_temperature = get_forecast.get_temperature('celsius')
- weather_temperature_max = get_forecast.get_temperature('celsius')['temp_max']
- weather_temperature_temp = get_forecast.get_temperature('celsius')['temp']
- weather_temperature_min = get_forecast.get_temperature('celsius')['temp_min']
- # Forecast Message
- forecast_msg = " Today's Forecast in London:\n \n Wind Speed: {} mph \n Humidity: {} % \n Max. Temperature: {} ºC \n Temperature Right Now: {} ºC \n Min. Temperature: {} ºC \n \n Tomorrow will be a {} day!".format(weather_wind_speed, weather_humidity, weather_temperature_max, weather_temperature_temp, weather_temperature_min,tommorrow_forecast)
- # Send E-mail:
- msg = email(forecast_msg)
- msg_from = "weather_bot@hotmail.com"
- msg_to = ["cristina.mp.goncalves@hotmail.com","pedropestana1976@hotmail.com","supremedarkman.mc@gmail.com"]
- msg['Subject'] = "Your Forecast for London, UK"
- msg['From'] = msg_from
- msg['To'] = ",".join(msg_to)
- with open('C:\py\weatherbot\pswrd.txt','r') as file:
- password = file.read()
- s = smtp.SMTP('smtp.live.com',587)
- s.ehlo()
- s.starttls()
- s.ehlo()
- s.login('weather_bot@hotmail.com', password)
- # Bot Loop
- while True:
- today = datetime.today()
- if today.hour == 6 and today.minute == 0 or today.hour == 12 and today.minute == 0:
- s.send_message(msg)
- print("Forecast has been sent at", str(datetime.now()))
- continue
- else : continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement