Beispiel #1
0
        def job():
            weather_dict, icon = get_weather_data('KLAX')
            weather_dict_ordered = OrderedDict(sorted(weather_dict.items()))

            email_file = "Email_File.html"
            create_html_report(weather_dict_ordered, icon, email_file)
            send_gmail(email_file)
Beispiel #2
0
def job():
    pprint(schedule.jobs)
    weather_dict, icon = get_weather_data('KLAX')
    print(weather_dict.items())
    print(sorted(weather_dict.items()))
    weather_dict_ordered = OrderedDict(sorted(weather_dict.items()))
    print(weather_dict_ordered)

    email_file = 'Email_File.html'
    create_html_report(weather_dict, icon, email_file)
    send_gmail(email_file)
def job():
    """Function that will be called based on the scheduled frequency"""
    pprint(schedule.jobs)
    weather_dict, icon = get_weather_data(
        'KNYC'
    )  #default station is KNYC, can be changed to any valid station code
    weather_dict_ordered = OrderedDict(sorted(weather_dict.items(
    )))  #dictionary must be ordered to align with our HTML template

    email_file = "Email_File.html"
    create_html_report(weather_dict_ordered, icon, email_file)
    send_gmail(email_file)

def create_html_report(data_dict, icon_url, html_file):
    alt_var = data_dict['weather']

    with open(html_file, mode='w') as outfile:
        outfile.write('\t<tr><td align="center">' +
                      datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
                      '</td></tr><br>\n')
        outfile.write('<img alt={} src={}>'.format(alt_var, icon_url))
        outfile.write('<br><span style="color:blue"><b>\tWeather Data:</b>\n')
        outfile.write('<br>')

        outfile.write('<html><table border=1>\n')
        # --------------------------------------------
        for key, value in data_dict.items():
            outfile.write(
                '<tr><td><b><span style="color:black">{:s}</b></td><td align="left"> \
                <span style="color:blue"><b>{:s}</b></td></tr>\n'.format(
                    key, value))
        # --------------------------------------------
        outfile.write('</table></html>\n')


# ===========================================
if __name__ == '__main__':
    from Get_Weather_Data import get_weather_data

    weather_dict, icon = get_weather_data('KLAX')
    create_html_report(weather_dict, icon, "Test_Email_File.html")
from datetime import datetime          
from GMAIL_PWD import GMAIL_PWD     

def send_gmail(msg_file):
    with open(msg_file, mode='rb') as message:              # Open report html file for reading
        msg = MIMEText(message.read(), 'html', 'html')      # Create html message
    
    msg['Subject'] = 'Hourly Weather {}'.format(datetime.now().strftime("%Y-%m-%d %H:%M"))
    msg['From'] = '*****@*****.**'
    msg['To'] = '[email protected], [email protected]'     # NO list!       
    
    server = smtplib.SMTP('smtp.gmail.com', port=587)
    server.ehlo()       # Extended Hello
    server.starttls()   # Put the SMTP connection in TLS (Transport Layer Security) mode.  
    server.ehlo()       # All SMTP commands that follow will be encrypted.
    server.login(os.environ['gmail'], os.environ['gpass'])
    server.send_message(msg)
    server.close()

#===========================================
if __name__ == '__main__':
    from Get_Weather_Data import get_weather_data
    from Create_Html_file import create_html_report 
    weather_dict, icon = get_weather_data('KCMH')    
    email_file = "C:/Users/csb003/Documents/NCHpython/PythonProjectsLynda/Section5 - Writing a Windows scheduling service/Section5_Video1_4/Test_Email_File.html"
    create_html_report(weather_dict, icon, email_file)
    send_gmail(email_file)
    
    
    
    
    #open the file in write mode
    with open(html_file, mode='w') as html_output:
        html_output.write('\t<tr><td align="center">' +
                          datetime.now().strftime("%Y-%m-%d %H:%M:%S") +
                          '</td></tr><br>\n')
        html_output.write('<img alt={} src={}>'.format(
            alt_var,
            icon_url))  #write the weather icon representing the weather
        html_output.write(
            '<br><span style="color:blue"><b>\tWeather Data:</b>\n'
        )  #write a title saying "Weather Data:"
        html_output.write('<br>')

        html_output.write('<html><table border=1>\n')

        #write the data into a table, using the dictionary where each key represents the type and the value represents the data point
        for key, value in data_dict.items():
            html_output.write(
                '<tr><td><b><span style="color:black">{:s}</b></td><td align="left"><span style="color:blue"><b>{:s}</b></td></tr>\n'
                .format(key, value))

        html_output.write('</table></html>\n')


#-----------------------------------------------------------
if __name__ == '__main__':
    from Get_Weather_Data import get_weather_data
    weather_dict, icon = get_weather_data()
    create_html_report(weather_dict, icon, "Email_File.html")