Advertisement
Guest User

Untitled

a guest
Jul 13th, 2017
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import pyowm as weather
  4. import time as timer
  5. from datetime import *
  6. import smtplib as smtp
  7. import imaplib
  8. from email.mime.text import MIMEText as m_email
  9. from email.parser import Parser
  10. import os
  11. import sys
  12. import email
  13. import errno
  14. import mimetypes
  15.  
  16. # Weather Forecast Bot
  17. # Author: Hugo Nascimento
  18.  
  19. KEY = weather.OWM('dacf348d593f6fed98857e7b168ebc3b')  
  20.  
  21. forecast = KEY.daily_forecast("London, uk")
  22. tomorrow = weather.timeutils.tomorrow()
  23.  
  24. # Forecast
  25.  
  26. tommorrow_forecast = "Unknown"
  27.  
  28. if forecast.will_be_sunny_at(tomorrow) == True:
  29.     tommorrow_forecast = "Sunny"
  30.  
  31. if forecast.will_be_rainy_at(tomorrow) == True:
  32.     tommorrow_forecast = "Rainy"
  33.  
  34. if forecast.will_be_cloudy_at(tomorrow) == True:
  35.     tommorrow_forecast = "Cloudy"
  36.  
  37. if forecast.will_be_snowy_at(tomorrow) == True:
  38.     tommorrow_forecast = "Snowy"
  39.  
  40. today_forecast = KEY.weather_at_place('London,uk')
  41. get_forecast = today_forecast.get_weather()
  42.  
  43. # Weather details
  44.  
  45. weather_wind = get_forecast.get_wind()                  
  46. weather_wind_speed = get_forecast.get_wind()['speed']
  47.  
  48.  
  49. weather_humidity = get_forecast.get_humidity()              
  50.  
  51. weather_temperature = get_forecast.get_temperature('celsius')
  52. weather_temperature_max = get_forecast.get_temperature('celsius')['temp_max']
  53. weather_temperature_temp = get_forecast.get_temperature('celsius')['temp']
  54. weather_temperature_min = get_forecast.get_temperature('celsius')['temp_min']
  55.  
  56. # Forecast Message
  57.  
  58. 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)
  59.  
  60. # Send E-mail:
  61.  
  62. global msg
  63. msg = m_email(forecast_msg)
  64.  
  65. msg_from = "weather_bot@hotmail.com"
  66.  
  67. msg_to = ["cristina.mp.goncalves@hotmail.com","pedropestana1976@hotmail.com","supremedarkman.mc@gmail.com"]
  68.  
  69. msg['Subject'] = "Your Forecast for London, UK"
  70. msg['From'] = msg_from
  71. msg['To'] = ",".join(msg_to)
  72.  
  73. with open('C:\py\weatherbot\pswrd.txt','r') as file:
  74.     global password
  75.     password = file.read()
  76.  
  77. s =  smtp.SMTP('smtp.live.com',587)
  78. s.ehlo()
  79. s.starttls()
  80. s.ehlo()
  81. s.login('weather_bot@hotmail.com', password)
  82.  
  83. def send_forecast():
  84.  
  85.     today = datetime.today()
  86.  
  87.     if today.hour == 6 and today.minute == 0 and today.second == 0:
  88.  
  89.         s.send_message(msg)
  90.         print("Forecast has been sent at", str(datetime.now()))
  91.  
  92.     elif today.hour == 12 and today.minute == 0 and today.second == 0:
  93.  
  94.         s.send_message(msg)
  95.         print("Forecast has been sent at", str(datetime.now()))
  96.  
  97.     elif today.hour == 20 and today.minute == 0 and today.second == 0:
  98.  
  99.         s.send_message(msg)
  100.         print("Forecast has been sent at", str(datetime.now()))
  101.  
  102. def send_email(to): # For an individual receiver
  103.                     # Used as a trigger when the bot receives an e-mail
  104.     message = msg
  105.     message['Subject'] = "Your Forecast for London, UK"
  106.     message['From'] = msg_from
  107.     message['To'] = to
  108.  
  109.     s.send_message(message)
  110.  
  111.  
  112. # def read_email():
  113.  
  114. #   mail = imaplib.IMAP4_SSL('imap.outlook.com')
  115. #   mail.login('weather_bot@hotmail.com', password)
  116. #   mail.list()
  117. #   mail.select('inbox')
  118.  
  119. #   result, data = mail.search(None, "ALL")
  120.  
  121. #   ids = data[0]
  122. #   id_list = ids.split()
  123.  
  124. #   latest_email_id = id_list[-1]
  125.  
  126. #   email_body = mail.fetch(latest_email_id, "(UID BODY[TEXT])")
  127. #   email_from = mail.fetch(latest_email_id, "(BODY[HEADER.FIELDS (FROM)])")
  128.  
  129. #   result, data = mail.fetch(latest_email_id, "(RFC822)")
  130.  
  131. #   raw_email = data[0][1]
  132.  
  133. #   print(raw_email)
  134.  
  135. #   msg = email.message_from_string(raw_email)
  136.  
  137. #   print(msg['From'])
  138. #   print(msg.get_payload(decode=True))
  139.  
  140.  
  141. # Bot Loop
  142.  
  143. def main():
  144.  
  145.     while True:
  146.  
  147.         send_forecast()
  148.         #read_email()
  149.  
  150.    
  151. if __name__ == '__main__':
  152.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement