Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import pyowm as weather
- import time as timer
- from datetime import *
- import smtplib as smtp
- import imaplib
- from email.mime.text import MIMEText as m_email
- from email.parser import Parser
- import os
- import sys
- import email
- import errno
- import mimetypes
- # 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:
- global msg
- msg = m_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:
- global password
- password = file.read()
- s = smtp.SMTP('smtp.live.com',587)
- s.ehlo()
- s.starttls()
- s.ehlo()
- s.login('weather_bot@hotmail.com', password)
- def send_forecast():
- today = datetime.today()
- if today.hour == 6 and today.minute == 0 and today.second == 0:
- s.send_message(msg)
- print("Forecast has been sent at", str(datetime.now()))
- elif today.hour == 12 and today.minute == 0 and today.second == 0:
- s.send_message(msg)
- print("Forecast has been sent at", str(datetime.now()))
- elif today.hour == 20 and today.minute == 0 and today.second == 0:
- s.send_message(msg)
- print("Forecast has been sent at", str(datetime.now()))
- def send_email(to): # For an individual receiver
- # Used as a trigger when the bot receives an e-mail
- message = msg
- message['Subject'] = "Your Forecast for London, UK"
- message['From'] = msg_from
- message['To'] = to
- s.send_message(message)
- # def read_email():
- # mail = imaplib.IMAP4_SSL('imap.outlook.com')
- # mail.login('weather_bot@hotmail.com', password)
- # mail.list()
- # mail.select('inbox')
- # result, data = mail.search(None, "ALL")
- # ids = data[0]
- # id_list = ids.split()
- # latest_email_id = id_list[-1]
- # email_body = mail.fetch(latest_email_id, "(UID BODY[TEXT])")
- # email_from = mail.fetch(latest_email_id, "(BODY[HEADER.FIELDS (FROM)])")
- # result, data = mail.fetch(latest_email_id, "(RFC822)")
- # raw_email = data[0][1]
- # print(raw_email)
- # msg = email.message_from_string(raw_email)
- # print(msg['From'])
- # print(msg.get_payload(decode=True))
- # Bot Loop
- def main():
- while True:
- send_forecast()
- #read_email()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement