/
main.py
70 lines (52 loc) · 2.31 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from datetime import time
from utils import error, get_email_credentials, get_github_actions_url, get_receivers, debug
from gmail import Gmail
from forecast import Forecast
from location import Location
from exec_tracker import ExecTracker
SEND_EMAIL_HOUR = 5 # AM local time
def send_forecast_message(gmail, receiver, message):
subject, msgPlain, msgHtml = message
gmail.send(receiver, subject, msgPlain, msgHtml)
def main():
debug(get_github_actions_url())
sender, password = get_email_credentials()
receivers = get_receivers()
if len(receivers) == 0: error('No receivers imported.')
gmail = Gmail(sender, password)
# use password from email to encrypt exec tracker file
track = ExecTracker(password)
for locationName, emails in receivers.items():
location = Location(locationName)
executed_today = track.script_executed_today(location)
time_to_send_email = location.get_local_time().hour >= SEND_EMAIL_HOUR
if executed_today:
debug('Script already executed today, no need to fetch forecast.')
if not time_to_send_email:
debug(f'Its not time to send an email. Waiting for {SEND_EMAIL_HOUR} A.M. local time.')
if not executed_today and time_to_send_email:
""" mark execution time only when time to send en email because
we don't want to exceed the number of free OpenWeather API calls """
debug("Script not executed today and its time to send en email.")
debug('Mark executed time.')
track.mark_exec_time(location)
forecast = Forecast(location)
if forecast.rain_today():
debug('It will rain today.')
message = forecast.get_forecast_message()
for email in emails:
debug('Sending email message.')
send_forecast_message(gmail, email, message)
else:
debug('It will not rain today.')
debug('Closing execution timetable.')
track.close()
debug('Checking if timetable is modified')
if track.is_timetable_modified():
debug('Timetable modified')
track.add_and_commit_timetable()
else:
debug('Timetable not modified')
debug("Application finished.")
if __name__ == "__main__":
main()