Advertisement
Guest User

Untitled

a guest
Jul 4th, 2017
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import pyowm as weather
  2. import time as timer
  3. from datetime import *
  4. import smtplib as smtp
  5. from email.mime.text import MIMEText as email
  6.  
  7. # Weather Forecast Bot
  8. # Author: Hugo Nascimento
  9.  
  10. KEY = weather.OWM('dacf348d593f6fed98857e7b168ebc3b')  
  11.  
  12. forecast = KEY.daily_forecast("London, uk")
  13. tomorrow = weather.timeutils.tomorrow()
  14.  
  15. # Forecast
  16.  
  17. tommorrow_forecast = "Unknown"
  18.  
  19. if forecast.will_be_sunny_at(tomorrow) == True:
  20.     tommorrow_forecast = "Sunny"
  21.  
  22. if forecast.will_be_rainy_at(tomorrow) == True:
  23.     tommorrow_forecast = "Rainy"
  24.  
  25. if forecast.will_be_cloudy_at(tomorrow) == True:
  26.     tommorrow_forecast = "Cloudy"
  27.  
  28. if forecast.will_be_snowy_at(tomorrow) == True:
  29.     tommorrow_forecast = "Snowy"
  30.  
  31. today_forecast = KEY.weather_at_place('London,uk')
  32. get_forecast = today_forecast.get_weather()
  33.  
  34. # Weather details
  35.  
  36. weather_wind = get_forecast.get_wind()                  
  37. weather_wind_speed = get_forecast.get_wind()['speed']
  38.  
  39.  
  40. weather_humidity = get_forecast.get_humidity()              
  41.  
  42. weather_temperature = get_forecast.get_temperature('celsius')
  43. weather_temperature_max = get_forecast.get_temperature('celsius')['temp_max']
  44. weather_temperature_temp = get_forecast.get_temperature('celsius')['temp']
  45. weather_temperature_min = get_forecast.get_temperature('celsius')['temp_min']
  46.  
  47. # Forecast Message
  48.  
  49. 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)
  50.  
  51. # Send E-mail:
  52.  
  53. msg = email(forecast_msg)
  54.  
  55. msg_from = "weather_bot@hotmail.com"
  56.  
  57. msg_to = ["cristina.mp.goncalves@hotmail.com","pedropestana1976@hotmail.com","supremedarkman.mc@gmail.com"]
  58.  
  59. msg['Subject'] = "Your Forecast for London, UK"
  60. msg['From'] = msg_from
  61. msg['To'] = ",".join(msg_to)
  62.  
  63. with open('C:\py\weatherbot\pswrd.txt','r') as file:
  64.     password = file.read()
  65.  
  66. s =  smtp.SMTP('smtp.live.com',587)
  67. s.ehlo()
  68. s.starttls()
  69. s.ehlo()
  70. s.login('weather_bot@hotmail.com', password)
  71.  
  72. # Bot Loop
  73.  
  74. while True:
  75.  
  76.     today = datetime.today()
  77.  
  78.     if today.hour == 6 and today.minute == 0 or today.hour == 12 and today.minute == 0:
  79.  
  80.         s.send_message(msg)
  81.         print("Forecast has been sent at", str(datetime.now()))
  82.         continue
  83.  
  84.     else : continue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement